【问题标题】:RSA encryption Android and decryption in Java: javax.crypto.BadPaddingException: Decryption errorRSA加密Android和Java解密:javax.crypto.BadPaddingException:解密错误
【发布时间】:2016-03-29 19:32:16
【问题描述】:

我有一个使用 RSA 2048 位密钥加密文本的 Android 应用。

加密和解密在应用程序上运行良好。

我的问题是我试图在应用程序上加密,然后将密码发送到外部 Java 程序进行解密。

这是怎么做的:

public static PrivateKey getPrivateKey(String key) {
    try {
        /* Add PKCS#8 formatting */
        byte[] byteKey = Base64.getDecoder().decode(key.getBytes());
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(new ASN1Integer(0));
        ASN1EncodableVector v2 = new ASN1EncodableVector();
        v2.add(new ASN1ObjectIdentifier(PKCSObjectIdentifiers.rsaEncryption.getId()));
        v2.add(DERNull.INSTANCE);
        v.add(new DERSequence(v2));
        v.add(new DEROctetString(byteKey));
        ASN1Sequence seq = new DERSequence(v);
        byte[] privKey = seq.getEncoded("DER");

        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePrivate(keySpec);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public static String decrypt(String cipherString,PrivateKey privateKey){
    try{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] plainByte = cipher.doFinal(Base64.getDecoder().decode(cipherString));
        return Base64.getEncoder().encodeToString(plainByte);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

但是当我尝试解密密码时,我得到了错误:

javax.crypto.BadPaddingException: Decryption error
    at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
    at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
    at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
    at javax.crypto.Cipher.doFinal(Cipher.java:2165)
    at RSA.decrypt(RSA.java:94)
    at RSA.main(RSA.java:116)

我错过了什么?

更新 我在 Java 本身中重新生成了一对新的键并去掉了这部分:

/* Add PKCS#8 formatting */
byte[] byteKey = Base64.getDecoder().decode(key.getBytes());
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(new ASN1Integer(0));
ASN1EncodableVector v2 = new ASN1EncodableVector();
v2.add(new ASN1ObjectIdentifier(PKCSObjectIdentifiers.rsaEncryption.getId()));
v2.add(DERNull.INSTANCE);
v.add(new DERSequence(v2));
v.add(new DEROctetString(byteKey));
ASN1Sequence seq = new DERSequence(v);
byte[] privKey = seq.getEncoded("DER");

但我遇到了同样的错误!

【问题讨论】:

  • 我看到的是解密代码,但没有加密代码。

标签: java android encryption cryptography rsa


【解决方案1】:

您的填充没有设置或设置错误。

尝试将Cipher cipher = Cipher.getInstance("RSA"); 更改为Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

如果这不起作用,您可以查看 here 以了解其他填充模式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多