RSA加密使用Base64连接在机器人codeD公钥机器人、公钥、RSA、codeD

2023-09-06 04:16:55 作者:梦断情殇

怎么办字节数组的RSA加密与基地-64连接codeD公钥?

How to do RSA encryption of byte array with base-64 encoded public key?

阅读夫妇就怎么做RSA加密在Java中的文章(谷歌搜索)后,发现下面的代码片断

After reading the couple of articles( of google search ) on how to do RSA encryption in Java, found the following snippet

public byte[] rsaEncrypt(byte[] data) {
  PublicKey pubKey = readKeyFromFile("/public.key");
  Cipher cipher = Cipher.getInstance("RSA");
  cipher.init(Cipher.ENCRYPT_MODE, pubKey);
  byte[] cipherData = cipher.doFinal(src);
  return cipherData;
}

我的公钥为Base64 EN codeD字符串(126个字符),如何创建公钥与EN codeD字符串,并使用它的密码。

I have public key as a base64 encoded string (126 characters), How do i create 'PublicKey' with the encoded string and use it with Cipher.

推荐答案

您的base64字符串可能是一个X509En codedKeySpec。我只能猜测。如果是这样,你应该去的Base64 code中的字符串来获得字节[] 。然后构建一个 X509En codedKeySpec 字节[] 。然后创建一个RSA 的KeyFactory 的实例,并使用此KeyFactory的 generatePublic()的方法来获得公钥。这个公共密钥可以被传递到 Cipher.init()

Your base64 string is possibly an X509EncodedKeySpec. I can only guess. If so, you should base64 decode the string to obtain a byte []. Then construct an X509EncodedKeySpec from this byte []. Then create an instance of an RSA KeyFactory, and use the generatePublic() method of this KeyFactory to obtain a PublicKey. This public key can then be passed to Cipher.init().

注意:执行的base64解码兼用的阿帕奇公地codeC 或的