gpt4 book ai didi

php - 将 PHP RSA 公钥转换为 Android 公钥

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:53:27 24 4
gpt4 key购买 nike

我正在开发基于客户端服务器的应用程序。

我从哪里获得这种格式的公钥

enter image description here

因为我将它保存到字符串中。

现在我想在我的 Android(Java 代码)中使用这个 key ,我该如何使用它?

最佳答案

首先您需要从您提供的 pem 格式生成公钥,这是我的方法:

/**
*
* @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)));

}

下面是我如何使用它来读取(解码)使用提供的公钥签名的内容。

/**
*
* @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 - 将 PHP RSA 公钥转换为 Android 公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7037780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com