gpt4 book ai didi

java - 将 CRYPT_RSA_PUBLIC_FORMAT_PKCS1 从 php 转换为 Java 中的 RSA 公钥

转载 作者:行者123 更新时间:2023-12-02 04:55:39 24 4
gpt4 key购买 nike

我正在尝试将 RSA 公钥从 PHP 发送到 Java(Android)。我的 PHP 代码如下所示:

function __construct() {
$rsa = new Crypt_RSA();
$rsa->setHash('sha1');
$rsa->setMGFHash('sha1');
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);
$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);

// Code to Generate the public key and private key

$keys = $rsa->createKey(1024);

extract($keys);

// Base 64 encode Public and Private key
$this->rsa = $rsa;
$this->keys = $keys;
$this->publicKeyBase = base64_encode($publickey);
$this->privateKeyBase = base64_encode($privatekey);
}

然后我将 $this->publicKeyBase 发送到我的 Android 应用程序。在 Java 中,我得到这样的编码字符串:

LS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tDQpNSUdKQW9HQkFMQzluUkdhVGsybzlJTW5YVW0vWWRVHMrTFplc09GUi9VYkU2K21hWDlwbGIwRW11RzZacHBMDQpoV2dRbUNBYmV6aW9Sc HZNL0lVZHZWczZ6ZmFKaDRGTnFaRXo0cWd0V0ovaFpUU2RudlFIMlI3cWF0TEY0c0ZSDQpDbWNNVDZBdnYvdDJnR1liMW4vY1lhb01ralNOd1RFdTJBSU45djg0Skk2ZWhmOGNST0RMQWdNQkFBRT0NCi0t LS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0=

解码后,它看起来像这样:

-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBALC9nRGaTk2o9IMnXUm/YdU0s+LZesOFR/UbE6+maX9plb0EmuG6ZppL
hWgQmCAbezioRpvM/IUdvVs6zfaJh4FNqZEz4qgtWJ/hZTSdnvQH2R7qatLF4sFR
CmcMT6Avv/t2gGYb1n/cYaoMkjSNwTEu2AIN9v84JI6ehf8cRODLAgMBAAE=
-----END RSA PUBLIC KEY-----

如何将其转换为可在 Android 应用程序中使用的公钥。在网上看到了很多例子,但它们不起作用,我的 java 代码(如下)似乎也不起作用。有什么可能的解决方案吗?

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 RSA PUBLIC KEY-----") != -1) {
while ((line = pemReader.readLine()) != null) {
if (line.indexOf("-----END RSA 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)));

}
<小时/>
new X509EncodedKeySpec(Base64.decode(content.toString(), Base64.DEFAULT)) throws an Invalid KeySpec Exception.

最佳答案

文档有点令人困惑,但是通过查看从第 950 行开始的方法 _convertPublicKey($n, $e)source code ,似乎 if $publicKeyFormat == PUBLIC_FORMAT_PKCS8 那么输出格式应该是与 Java 的 X509EncodedKeySpec 类兼容的格式。

关于java - 将 CRYPT_RSA_PUBLIC_FORMAT_PKCS1 从 php 转换为 Java 中的 RSA 公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28797482/

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