我尝试获取PrivateKey.getEncode时出错

问题描述 投票:1回答:1

我有此代码

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()));
    }

这是我显示密钥的方法

java android rsa
1个回答
0
投票

请向我们详细介绍两个变量模数privateExponent。如果可能,请将其发布在github上,并让我知道链接

© www.soinside.com 2019 - 2024. All rights reserved.