将RSA Publickey转换为base64,反之亦然 [英] convert RSA Publickey to base64 and vice versa

查看:914
本文介绍了将RSA Publickey转换为base64,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从此函数生成的publicKey / privateKey对:

I have a publicKey/privateKey pair generated from this function:

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();
        }

    }

现在我想将publicKey转换为 base64 同时编写并使用该base64解码来获取publicKey,那怎么办?

Now I want to convert publicKey to base64 while writing and use that base64 decode to get publicKey back ,how can that be done?

推荐答案

通常,如果要将文件存储在base 64中,则可以简单地对字节数组进行编码。您甚至可以在 ObjectOutputStream FileOutputStream 之间放置Base64流(由Java 8中的Base64类提供) 。

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).

但是,公钥和私钥具有默认编码,可以使用其 getEncoded 方法进行访问:

However, public keys and private keys have default encodings which can be accessed using their getEncoded methods:

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);
}

这会将公钥保存在 SubjectPublicKeyInfo 格式,可由多种类型的软件和密码库读取和编写。

This saves the public key in SubjectPublicKeyInfo format, something that can be read and written by multiple types of software and cryptographic libraries.

例如,您可以将其粘贴到在线ASN.1解码器中(在线解码器本身会将其转换为十六进制,但会解析b以及64)。字节的格式称为ASN.1 / DER(这是一种通用格式,就像您可以用XML编码多种类型的文件一样)。

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).

这篇关于将RSA Publickey转换为base64,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆