RSA加密使用Base64连接在机器人codeD公钥 [英] RSA encrypt with base64 encoded public key in Android

查看:218
本文介绍了RSA加密使用Base64连接在机器人codeD公钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

怎么办字节数组的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 或的the艰苦的base64德codeR

Note: to perform base64 decoding use either the apache commons codec, or the Harder base64 decoder.

这篇关于RSA加密使用Base64连接在机器人codeD公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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