gpt4 book ai didi

java - Android XML RSA,错误 : java. security.InvalidKeyException:传递给 RSA 的未知 key 类型

转载 作者:行者123 更新时间:2023-11-29 05:02:39 25 4
gpt4 key购买 nike

我在使用 RSA 加密字符串时遇到问题。我的 RSA 是 XML 格式,它看起来像这样:

<RSAKeyValue><Modulus>lT8ykfyV0R8o3mJZZezLKTKJpYB90Pzvp0moLzh9CTGfgsxLKYiAl+YGaoRfQ7hVQos5UlLIONHWKPNco9kKcmL6EBJvFc8wqBnhX0p4ML2WSv1yDIRsm9XXra82WHIa3+fxK8bNUJHrucxmpr9pDRPdZGZkz+Q9s94FcOyFKbs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>

我正在尝试使用此类加密字符串:

import java.io.BufferedReader;
import java.io.StringReader;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.Security;
import javax.crypto.Cipher;
import org.bouncycastle.openssl.PEMReader;
import android.util.Base64;
import android.util.Log;

public class RsaEncryption {

private String publicKey;

public RsaEncryption(String publicKey)
{
this.publicKey = publicKey;

}


/*
* Function to encrypt the data.
*
*/

public String encrypt( String data ) throws Exception
{



Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");


byte[] keyBytes = Base64.decode( this.publicKey, 0 );

PublicKey publickey = strToPublicKey(new String(keyBytes));
cipher.init( Cipher.ENCRYPT_MODE , publickey );

// Base 64 encode the encrypted data
byte[] encryptedBytes = Base64.encode( cipher.doFinal(data.getBytes()), 0 );

return new String(encryptedBytes);


}


public static PublicKey strToPublicKey(String s)
{

PublicKey pbKey = null;
try {

BufferedReader br = new BufferedReader( new StringReader(s) );
PEMReader pr = new PEMReader(br);
Object obj = pr.readObject();

if( obj instanceof PublicKey )
{
pbKey = (PublicKey) pr.readObject();
}
else if( obj instanceof KeyPair )
{
KeyPair kp = (KeyPair) pr.readObject();
pbKey = kp.getPublic();
}
pr.close();

}
catch( Exception e )
{
Log.d("CIPHER", e.getMessage() );
}

return pbKey;
}

}

如你所见,我正在使用 bouncycaSTLe 的 jar 我得到的错误是:java.security.InvalidKeyException:传递给 RSA 的未知 key 类型

我不确定这部分

Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");

也许这是问题所在?如果是,那需要什么?

我做了几个小时的研究,但仍然没有找到解决方案...

提前致谢:)

最佳答案

Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");

maybe this is the problem?

不,不是。

OAEPWith<digest>And<mgf>Padding

表示在 PKCS1 中定义的最佳非对称加密填充方案, 其中<digest>应替换为消息摘要 算法和<mgf>通过掩码生成函数。示例:OAEPWithMD5AndMGF1PaddingOAEPWithSHA-512AndMGF1Padding .

引用 Standard NamesRFC 4055 .

问题出在您的公钥 生成中。由于您的 key 在 XML 中,并且采用 Base64 编码:

  • 首先您需要分离模数和指数。
  • 然后 Base64 解码模数和指数。

解码后你会得到模数和指数的字节数组,这样你就可以像下面的过程一样轻松地准备公钥对象:

BigInteger modBigInteger = new BigInteger(1, modulus);//modulus must be byte array
BigInteger exBigInteger = new BigInteger(1, exponent);//exp must be byte array

RSAPublicKeySpec spec = new RSAPublicKeySpec(modBigInteger, exBigInteger);
KeyFactory factory = KeyFactory.getInstance("RSA");
PublicKey publicKey = factory.generatePublic(spec);

关于java - Android XML RSA,错误 : java. security.InvalidKeyException:传递给 RSA 的未知 key 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31540927/

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