java 将 RSA 公钥转换为 base64,反之亦然
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41526826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert RSA Publickey to base64 and vice versa
提问by android_guy
I have a publicKey/privateKey pair generated from this function:
我有一个从此函数生成的 publicKey/privateKey 对:
public static void generateKey() {
try {
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
keyGen.initialize(2048);
final KeyPair key = keyGen.generateKeyPair();
File privateKeyFile = new File(PRIVATE_KEY_FILE);
File publicKeyFile = new File(PUBLIC_KEY_FILE);
// Create files to store public and private key
if (privateKeyFile.getParentFile() != null) {
privateKeyFile.getParentFile().mkdirs();
}
privateKeyFile.createNewFile();
if (publicKeyFile.getParentFile() != null) {
publicKeyFile.getParentFile().mkdirs();
}
publicKeyFile.createNewFile();
// Saving the Public key in a file
ObjectOutputStream publicKeyOS = new ObjectOutputStream(
new FileOutputStream(publicKeyFile));
publicKeyOS.writeObject(key.getPublic());
publicKeyOS.close();
// Saving the Private key in a file
ObjectOutputStream privateKeyOS = new ObjectOutputStream(
new FileOutputStream(privateKeyFile));
privateKeyOS.writeObject(key.getPrivate());
privateKeyOS.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Now I want to convert publicKey to base64
while writing and use that base64 decode to get publicKey back ,how can that be done?
现在我想base64
在编写时将 publicKey 转换为并使用该 base64 解码来获取 publicKey 回来,怎么做?
回答by Maarten Bodewes
Generally if you want to store a file in base 64 you can simply encode the byte array. You can even put a Base64 stream in between the ObjectOutputStream
and FileOutputStream
(helpfully provided by the Base64 class within Java 8).
通常,如果您想以 base 64 存储文件,您可以简单地对字节数组进行编码。您甚至可以在ObjectOutputStream
和之间放置一个 Base64 流FileOutputStream
(由 Java 8 中的 Base64 类提供)。
However, public keys and private keys have default encodings which can be accessed using their getEncoded
methods:
但是,公钥和私钥具有默认编码,可以使用它们的getEncoded
方法访问:
PublicKey publicKey = key.getPublic();
byte[] encodedPublicKey = publicKey.getEncoded();
String b64PublicKey = Base64.getEncoder().encodeToString(encodedPublicKey);
try (OutputStreamWriter publicKeyWriter =
new OutputStreamWriter(
new FileOutputStream(publicKeyFile),
StandardCharsets.US_ASCII.newEncoder())) {
publicKeyWriter.write(b64PublicKey);
}
This saves the public key in SubjectPublicKeyInfo
format, something that can be read and written by multiple types of software and cryptographic libraries.
这以SubjectPublicKeyInfo
格式保存公钥,可以由多种类型的软件和加密库读取和写入。
For instance, you can paste it in an online ASN.1 decoder(the online decoder will itself convert it to hex, but it will parse base 64 as well). The format of bytes are in so called ASN.1 / DER (which is a generic format, just like you can encode multiple types of files in XML).
例如,您可以将其粘贴到在线 ASN.1 解码器中(在线解码器本身会将其转换为十六进制,但它也会解析 base 64)。字节的格式是所谓的 ASN.1 / DER(这是一种通用格式,就像您可以在 XML 中对多种类型的文件进行编码一样)。