Java 将 Public 和私钥转换为字符串然后返回

Java Convert Public and private Key to string and then back

我有一个私有密钥和一个 public RSA 2048 位密钥,我需要将它们转换为字符串以将它们保存在 SQLite 数据库中,但我还需要一种方法将它们转换回 private/public key 当我在数据库中读取数据时,我如何在 Java?

中做到这一点

抱歉我的英语不好。

您可以使用Base64Encoder and Base64Decoder

Base64 encoding/decoding在需要将二进制数据序列化成String时非常有用,因为它产生的是一个good字符串,它只是ascii,或多或少没有什么特别之处字符.

    // You can turn your bytes into a string using encoder.
    String keyAsString = Base64.getEncoder().encodeToString(key);
    // ....
    ///

    // When you need your bytes back you call the decoder on the string.
    byte[] keyBytes = Base64.getDecoder().decode(keyAsString);

在低于 1.8 的 java 版本上,您可以使用 Apache Commons Codec