简体   繁体   English

将RSA Publickey转换为base64,反之亦然

[英]convert RSA Publickey to base64 and vice versa

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? 现在我想在编写时将publicKey转换为base64 ,并使用该base64解码来取回publicKey,该怎么办?

Generally if you want to store a file in base 64 you can simply encode the byte array. 通常,如果要将文件存储在base 64中,则可以简单地对字节数组进行编码。 You can even put a Base64 stream in between the ObjectOutputStream and FileOutputStream (helpfully provided by the Base64 class within Java 8). 您甚至可以在ObjectOutputStreamFileOutputStream之间放置一个Base64流(由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). 例如,您可以将其粘贴到在线ASN.1解码器中 (在线解码器本身会将其转换为十六进制,但也会解析base 64)。 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 / DER(这是一种通用格式,就像您可以用XML编码多种类型的文件一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM