为什么我无法在Java环境中解码来自安卓设备的字符串?

huangapple go评论63阅读模式
英文:

Why i can not decode the string coming from Android devices with Java environment?

问题

我使用 Android 设备使用以下代码对字符串进行加密:

在 Android 端,密钥长度:2048

//初始化:需要一些时间
private void prepareEncrypt(String publicKey) throws Exception {
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey.getBytes()));
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PublicKey pubKey = keyFactory.generatePublic(keySpec);
    cp = Cipher.getInstance("RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
    cp.init(Cipher.ENCRYPT_MODE, pubKey);
}

//加密
private byte[] encryptByPublicKey(byte[] data) {
    byte[] result = new byte[0];
    try {
        result = cp.doFinal(data);
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    }

    return result;
}

得到密文 A。

然后,我尝试在 Eclipse 上使用 Java 解码 A:

public static byte[] decryptByPrivateKey(byte[] encrypted, byte[] privateKey) throws Exception {
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey));
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PrivateKey keyPrivate = kf.generatePrivate(keySpec);
    Cipher cp = Cipher.getInstance("RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
    cp.init(Cipher.DECRYPT_MODE, keyPrivate);
    byte[] arr = cp.doFinal(Base64.getDecoder().decode(encrypted));
    return arr;
}

我总是无法成功解码字符串。

javax.crypto.BadPaddingException: Decryption error
    at sun.security.rsa.RSAPadding.unpadOAEP(Unknown Source)
    at sun.security.rsa.RSAPadding.unpad(Unknown Source)
    at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)

如果 Android 端不更改 Cipher 对象上的 TRANSFORMATION 参数,例如:

RSA/ECB/NoPadding

RSA/ECB/PKCS1Padding

RSA/ECB/OAEPWithSHA-1AndMGF1Padding

RSA/ECB/OAEPWithSHA-224AndMGF1Padding

RSA/ECB/OAEPWithSHA-256AndMGF1Padding

RSA/ECB/OAEPWithSHA-384AndMGF1Padding

RSA/ECB/OAEPWithSHA-512AndMGF1Padding

如何成功解码密文 A?

英文:

I use Android deivces to encript a string with these code:

Android side , key length : 2048

//Init: need time
private void prepareEncrypt(String publicKey) throws Exception {
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey.getBytes()));
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PublicKey pubKey = keyFactory.generatePublic(keySpec);
    cp = Cipher.getInstance("RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
    cp.init(Cipher.ENCRYPT_MODE, pubKey);
}

//encrypt
private byte[] encryptByPublicKey(byte[] data) {
    //data = Base64.getEncoder().encode(data);
    byte[] result = new byte[0];
    try {
        result = cp.doFinal(data);
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    }

    return result;
}

Get a Ciphertext A.

Then i tried to decode the A with Java on elipse:

public static byte[] decryptByPrivateKey(byte[] encrypted, byte[] privateKey) throws Exception {
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey));
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PrivateKey keyPrivate = kf.generatePrivate(keySpec);
    Cipher cp = Cipher.getInstance("RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
    cp.init(Cipher.DECRYPT_MODE, keyPrivate);
    byte[] arr = cp.doFinal(Base64.getDecoder().decode(encrypted));
    return arr;
}

I always failed to decode the String.

javax.crypto.BadPaddingException: Decryption error
    at sun.security.rsa.RSAPadding.unpadOAEP(Unknown Source)
    at sun.security.rsa.RSAPadding.unpad(Unknown Source)
    at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)

If the Android do not change the TRANSFORMATION paremeter on object Cipher Like:

RSA/ECB/NoPadding

RSA/ECB/PKCS1Padding

RSA/ECB/OAEPWithSHA-1AndMGF1Padding

RSA/ECB/OAEPWithSHA-224AndMGF1Padding

RSA/ECB/OAEPWithSHA-256AndMGF1Padding

RSA/ECB/OAEPWithSHA-384AndMGF1Padding

RSA/ECB/OAEPWithSHA-512AndMGF1Padding

How to decode the Ciphertext A successfully?

答案1

得分: 1

OAEPParameterSpec oaepParameterSpecDec = new OAEPParameterSpec("SHA-512", "MGF1", new MGF1ParameterSpec("SHA-512"), PSource.PSpecified.DEFAULT);
...
cp.init(Cipher.DECRYPT_MODE, keyPrivate, oaepParameterSpecDec);

显然提供了填充参数。

英文:

Need this :

    OAEPParameterSpec oaepParameterSpecDec = new OAEPParameterSpec("SHA-512", "MGF1", new MGF1ParameterSpec("SHA-512"), PSource.PSpecified.DEFAULT);
    ...
    cp.init(Cipher.DECRYPT_MODE, keyPrivate, oaepParameterSpecDec);

obviously give the padding paremeters

huangapple
  • 本文由 发表于 2020年9月4日 11:46:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63734564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定