RSA BadPaddingException in Java - encrypt in Android decrypt in JRE

青春壹個敷衍的年華 提交于 2019-12-03 14:02:28

问题


i found some other questions but i did not find a explaination what causes this. I wrote a function in Java to encrypt data, store it into a file and decrypt it loading the file again. As it is a asymetric encryption, i have a public and a private key that is directly stored in the code for testing purpose.

The problem seem to be a difference in the encryption itselfs as the function work on android as well as on a windows machine with java RE 7 installed. But it is not possible to encrypt the data from the Android on the JRE7 system using the same key and funktion with the result "bad padding exeption".

For Testing purposes, i just use a single block of data. As the en/decrypt works in each system, i am sure the problem is not the key. The final version will encode and decode binary data - so i just use bytes and don't have to worry about different charsets in Strings. After I understand what the reason is, i will decode the data in PHP with pureRSA.

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import android.util.Base64;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.BadPaddingException;
import javax.crypto.ShortBufferException;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.io.IOException;

public class test
{
    public static void main (String args[]) throws FileNotFoundException, FileNotFoundException,
            UnsupportedEncodingException, IOException, NoSuchAlgorithmException, InvalidKeySpecException,
            NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
            ShortBufferException, NoSuchProviderException, InvalidAlgorithmParameterException {

        Cipher CheckCipher = Cipher.getInstance("RSA");
        KeyFactory fact = KeyFactory.getInstance("RSA");
// one Block has 320 Bytes encrypted data or up to 309 Bytes decrypted data
        byte[] encryptedBuffer = new byte[320];
// public key
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger("348349717397217997016459044429876445798461822630613593827475516398929980499253048018915109820386668925158598090923917424889721436728681941788735398708226655486687058044818981687475046995966049152458371358433593409946449307673641432434645409125209056679079565878576322466469782078766479519331465759437687570449909733209874455595086928288016285095448723528960074392901021635076277130066403766286870175583368291319265676733584876043054873738027872260931903992491855282040202055048433338277681906526276056353076551640420301024861314931536846223479281788109044598118149113100016473709709779349227018363507068247312704031804719673776595496482605126340630418742698424978233098157782472641000027452801691972881854034867234891280067908910242015420737297077922221753325197819914227"), new BigInteger("65537"));
        RSAPublicKey publicKey = (RSAPublicKey) fact.generatePublic(pubKeySpec);
// private key
        byte[] encoded = Base64.decode("MIIF3gIBADANBgkqhkiG9w0BAQEFAASCBcgwggXEAgEAAoIBQQDNz13heSUvu7J4d+d1VJBkx6D31ZBcaF3AAA0AFYPPkkPfGF9FneTITk/ft7n1PCT9MzBRzCIFyPEkk93I4tLdP4POUDCRGC1uDBOKBkjo8hMYROt+DtK6vAafhvs/sVJVcMAxYEOZl9wG/SS9KEFU9NktM2pBfq5x747AfjVAJzZuQkL7Mfqejnby7CNgkSk0FFS4top/6EQoe+NeQj1zcDCQQ4/ze5sahc0CF7aaPsq1+aI17CCBh2PFUOvG9/YM5Hj7An2cDURChHr0OJjT8/QwqR49cSSS5P4yhaVxOQqszUAinV+r5FtLGo7iXSMnE/XYV0vpZqJHAD9cPyymcl2N7v1dKWBTmamEp7GsM6T7YnDtSJIeYt9cTi+0IFYMPAe+W93zgH2g/r+nmL/eZapgUD5Gi2ky1bHe0vwn8wIDAQABAoIBQBJ7JYYCt+kiZLNWqQ0rK9Aw8O5wWgdCQ/Di0EgKpox0KO4WpS9+LzYheiCvwd4YqYWnHBasSv3T/nt3X7oTTDYb5v0WzFWrIyE3qmWBjPiGDFrojXEDab7k9X9LwrIEfU0407lbWJapQVPLVYo63CsB1aKudEMvWgDXLy1v59475zJdduBRq5yjVGEZ/U9FmbqYfMcddTBpLf2o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CsI0OpKkhxSGYM9OW1tR8ceTe1Ho+IQKBoQDELzIIUG4ND9QuvgwJUW0ys4Xo1BX5wNWP77GasAEb9EYB1uF7Apcp8yfGax+pRuqd/t0e5HDcd4JOhwfcWHAZYQqEm6d9yxq+eE0o/junEIVEPggNgcq3xJTryhq7kpzduj8GDdbq8PjuMFouJezjYHnbcidqAgLNfofqkj9cxAn5xIg2jhMIJh8RUqFlNEp8EKulTlGVTA8sj8Lf2Z1B", Base64.DEFAULT);
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encoded);
        RSAPrivateKey debugprivateKey = (RSAPrivateKey) fact.generatePrivate(privateKeySpec);

// selftest (encrypt data)
        String OutPutEncryption = "debug.exponent.javatest2.txt";
        CheckCipher.init(Cipher.ENCRYPT_MODE, publicKey);
        DataOutputStream cryptFile = new DataOutputStream(new FileOutputStream(OutPutEncryption));
        byte[] decryptedBuffer = new byte[] {100, 110, 112, 99, 64}; // some data 5 bytes
        CheckCipher.doFinal(decryptedBuffer, 0, 5, encryptedBuffer);
        cryptFile.write(encryptedBuffer);
        cryptFile.close();

// decrypt data from selftext or external source
        String SourceFile = "debug.exponent.javatest2.txt";
        CheckCipher.init(Cipher.DECRYPT_MODE, debugprivateKey);
        DataInputStream readCryptFileDeb = new DataInputStream(new FileInputStream(SourceFile));
        readCryptFileDeb.read(encryptedBuffer);
        readCryptFileDeb.close();
        byte[] Test = CheckCipher.doFinal(encryptedBuffer);
        System.out.println(new String(Test));
    }
}

回答1:


Unfortunaltey I currently don't have a running android system to test it but I guess that different padding algorithms may be choosen.

So give it a try and explicitley choose the padding algorithm, like this:

Cipher CheckCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

Note that you also have to choose a block-chaining-mode in this case. In this case I've chosen "ECB" (=no block chaining), because I think this is the default.



来源:https://stackoverflow.com/questions/28394108/rsa-badpaddingexception-in-java-encrypt-in-android-decrypt-in-jre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!