转换PHP RSA公钥到Android的公钥 [英] Convert PHP RSA PublicKey into Android PublicKey
本文介绍了转换PHP RSA公钥到Android的公钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我工作的一个客户端服务器应用程序。
I am working on a client server based application.
如果我以这种形式获得公钥
Where I get PublicKey in this format
因为我救了它转换成String。
as I saved it into String.
现在我想用这个键在我的Android(Java的code),我该如何使用呢?
Now I want to use this key in my Android(Java code), how can I use this ?
推荐答案
首先,你需要生成你所提供的PEM格式的公共密钥,这里是我的方法做这样的:
First you need to generate the public key from the pem format you provided, here is my method for doing this:
/**
*
* @param PEMString -A file/string in .pem format with a generated RSA key (with "des3", using "openssl genrsa".)
* @param isFilePath - If it's a file path or a string
* @return java.security.PublicKey
* @throws IOException -No key found
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*
* @author hsigmond
*/
private static PublicKey getPublicKeyFromPemFormat(String PEMString,
boolean isFilePath) throws IOException, NoSuchAlgorithmException,
InvalidKeySpecException {
BufferedReader pemReader = null;
if (isFilePath) {
pemReader = new BufferedReader(new InputStreamReader(
new FileInputStream(PEMString)));
} else {
pemReader = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(PEMString.getBytes("UTF-8"))));
}
StringBuffer content = new StringBuffer();
String line = null;
while ((line = pemReader.readLine()) != null) {
if (line.indexOf("-----BEGIN PUBLIC KEY-----") != -1) {
while ((line = pemReader.readLine()) != null) {
if (line.indexOf("-----END PUBLIC KEY") != -1) {
break;
}
content.append(line.trim());
}
break;
}
}
if (line == null) {
throw new IOException("PUBLIC KEY" + " not found");
}
Log.i("PUBLIC KEY: ", "PEM content = : " + content.toString());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(new X509EncodedKeySpec(Base64.decode(content.toString(), Base64.DEFAULT)));
}
这里是我如何使用它来读取(德code)与提供的公共密钥签名的内容。
And here is how I use it to read (decode) the content signed with the public key provided.
/**
*
* @param PEMString -A file/string in .pem format with a generated RSA key (with "des3", using "openssl genrsa".)
* @param content
* @return String value of content Decoded
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws IOException
* @throws NoSuchProviderException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*
* @author hsigmond
*/
public static String getContentWithPublicKeyFromPemFormat(String PEMString,
String content,boolean isFilePath) throws NoSuchAlgorithmException,
InvalidKeySpecException, IOException, NoSuchProviderException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
PublicKey publicKey = getPublicKeyFromPemFormat(PEMString,isFilePath);
if (publicKey != null)
Log.i("PUBLIC KEY: ", "FORMAT : " + publicKey.getFormat()
+ " \ntoString : " + publicKey.toString());
byte[] contentBytes = Base64.decode(content, Base64.DEFAULT);
byte[] decoded = null;
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");//BC=BouncyCastle Provider
cipher.init(Cipher.DECRYPT_MODE, publicKey);
decoded = cipher.doFinal(contentBytes);
return new String(decoded, "UTF-8");
}
这篇关于转换PHP RSA公钥到Android的公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文