我尝试获取PrivateKey.getEncode时出错
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我尝试获取PrivateKey.getEncode时出错相关的知识,希望对你有一定的参考价值。
我有此代码
RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(modulus, privateExponent);
KeyFactory factory = KeyFactory.getInstance("RSA");
PrivateKey priv = factory.generatePrivate(privateSpec);
byte[] tmp = priv.getEncode();
我得到错误
java.lang.IllegalStateException:无法执行android:onClick的方法
在我的应用程序中,我通过显示私钥和公钥演示了RSA算法。我在获取publickey的编码方面没有任何问题,但是当我尝试获取privatekey的编码时,出现此错误
public BigInteger getCoprime(BigInteger m, SecureRandom random)
int length = m.bitLength()-1;
BigInteger e = BigInteger.probablePrime(length,random);
while (! (m.gcd(e)).equals(BigInteger.ONE) )
e = BigInteger.probablePrime(length,random);
return e;
private void Generate()throws
NoSuchAlgorithmException,
InvalidKeyException,
NoSuchPaddingException,
IllegalBlockSizeException,
BadPaddingException,
InvalidKeySpecException
int keySize = 512;
SecureRandom random = new SecureRandom();
BigInteger p = BigInteger.probablePrime(keySize/2, random);
BigInteger q = BigInteger.probablePrime(keySize/2,random);
BigInteger modulus = p.multiply(q);
BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
BigInteger publicExponent = getCoprime(m,random);
BigInteger privateExponent = publicExponent.modInverse(m);
RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, publicExponent);
RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(modulus, privateExponent);
KeyFactory factory = KeyFactory.getInstance("RSA");
PublicKey pub = factory.generatePublic(spec);
PrivateKey priv = factory.generatePrivate(privateSpec);
EditText privateKey = (EditText)findViewById(R.id.editTextPrivateKeyy);
EditText publicKey = (EditText)findViewById(R.id.editTextPublicKeyy);
publicKey.setText(Hex.toHexString(pub.getEncoded()));
这是我显示密钥的方法
答案
请向我们详细介绍两个变量模数privateExponent。如果可能,请将其发布在github上,并让我知道链接
以上是关于我尝试获取PrivateKey.getEncode时出错的主要内容,如果未能解决你的问题,请参考以下文章
尝试使用 firebase 获取我的颤振应用程序的获取请求并出现错误