java 在Android中使用base64编码的公钥进行RSA加密
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2956647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RSA encrypt with base64 encoded public key in Android
提问by Suresh
How to do RSA encryption of byte array with base-64 encoded public key?
如何使用 base-64 编码的公钥对字节数组进行 RSA 加密?
After reading the couple of articles( of google search ) on how to do RSA encryption in Java, found the following snippet
在阅读了关于如何在 Java 中进行 RSA 加密的几篇文章(谷歌搜索)后,找到了以下片段
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;
}
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 编码字符串(126 个字符),如何使用编码字符串创建“公钥”并将其与密码一起使用。
采纳答案by President James K. Polk
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 X509EncodedKeySpecfrom 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 字符串可能是 X509EncodedKeySpec。我只能猜测。如果是这样,您应该对字符串进行 base64 解码以获得byte []. 然后X509EncodedKeySpec从 this构造一个byte []。然后创建一个 RSA 的实例KeyFactory,并使用generatePublic()这个 KeyFactory的方法获取一个PublicKey. 然后可以将此公钥传递给Cipher.init()。
Note: to perform base64 decoding use either the apache commons codec, or the Harder base64 decoder.
注意:要执行 base64 解码,请使用apache commons codec或Harder base64 解码器。
UPDATE March 8, 2017:
In better-late-than-never news, Java 8 now includes a Base64 encoding/decoding class, java.util.Base64
2017 年 3 月 8 日更新:最新消息是,Java 8 现在包含一个 Base64 编码/解码类, java.util.Base64
回答by Alex
this is how you can generate Public and Private key pair below is the function to store them on hard dist
这是您如何生成公钥和私钥对的方法 下面是将它们存储在硬盘上的功能
enter code here
public static void GenerateKeyPair()
{
try{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();
KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(),
RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(),
RSAPrivateKeySpec.class);
saveToFile("public.key", pub.getModulus(),
pub.getPublicExponent());
saveToFile("private.key", priv.getModulus(),
priv.getPrivateExponent());
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static void saveToFile(String fileName,
BigInteger mod, BigInteger exp) throws Exception {
ObjectOutputStream oout = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(fileName)));
try {
oout.writeObject(mod);
oout.writeObject(exp);
} catch (Exception e) {
throw new Exception("error", e);
} finally {
oout.close();
}
}

