Sui.

帖子

分享您的知识。

Dominikus .
Jan 05, 2025
专家问答

How do I extract the Base64 from a .key file?

I'm a beginner and I'm trying to get the Base64 encoding from a .key file generated using sui keytool generate. Could someone guide me on how to confirm that the file content is indeed Base64 encoded and how to extract it if necessary?

  • Sui
  • SDKs and Developer Tools
2
4
分享
评论
.

答案

4
jakodelarin.
Aug 25 2025, 22:44

When you use the sui keytool generate command, the output is indeed in a Base64 format. The .key file generated contains Base64 encoded information, including the keypair. You typically don't need to extract Base64 from it separately unless you have specific needs for the Base64 string itself.

11
评论
.
tolexwills47.
Aug 3 2025, 11:52

.key 文件通常以原始字节或十六进制格式存储您的私钥,而不是 Base64. 要将其转换为 Base64(或者如果已经编码,则提取 Base64),首先需要检查其格式: • 如果是十六进制编码(Sui 密钥很常见):转换十六进制 → 原始字节 → Base64. • 如果已经是 Base64(以 MIIB 开头... 或者有 = padding):你可以直接使用,无需转换. • 如果是原始二进制:将字节直接编码为 Base64.

使用 Node.js 的示例:

const fs = require ('fs');

//读取 .key 文件 const keyData = fs.readFileSync ('mykey.key', 'utf8') .trim ();

//如果是十六进制 → 转换为 Base64 const base64Key = buffer.from (keyData, '十六进制') .toString ('base64'); console.log (base64Key);

使用 Python 的示例:

导入 base64

使用 open ('mykey.key', 'r') 作为 f: hex_data = f.read () .strip ()

base64_key = base64.b64encode(bytes.fromhex(十六进制数据)).decode () 打印(base64_key)

有了Base64字符串后,就可以将其导入到需要Base64密钥的工具或软件开发工具包中(例如,用于Sui钱包操作).

3
评论
.
elfDani.
Jan 6 2025, 10:17

如果您的密钥文件是使用类似的工具创建的sui keytool,则整个内容应采用 Base64 编码. 但是,如果您想确认或在其他地方将其用作 Base64 字符串,则可以像读取任何文本文件一样读取文件内容,因为它已经在 Base64 格式的字符串中.

2
评论
.
skywinder.
Jan 6 2025, 03:32

当你使用该sui keytool generate命令时,输出确实是Base64格式. 生成的 .key 文件包含 Base64 编码信息,包括密钥对. 除非你对 Base64 字符串本身有特殊需求,否则你通常不需要单独从中提取 Base64.

1
评论
.

你知道答案吗?

请登录并分享。