Caused by: java.security.KeyException: Bad Data

2k views Asked by At

This following code can encrypt data well but I am getting error when trying to decrypt the data.

public static void cryptoFunction() throws Exception
        {
            KeyStore store = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
            store.load(null);
            String alias = "alias";
            Certificate cert = store.getCertificate(alias);
            PublicKey pubKey = (PublicKey) cert.getPublicKey();
            PrivateKey privKey = (PrivateKey) store.getKey(alias, "123456".toCharArray());
            Cipher ecipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            Cipher dcipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

            ecipher.init(Cipher.ENCRYPT_MODE, pubKey);

            File userDir = new File("C:\\TestCryptoFiles");
            userDir.mkdir();

            File tmpdestFile = new File(userDir, "outFile.txt");
            File sourceFile = new File(userDir, "InFile.txt");

            int cipherMode = Cipher.ENCRYPT_MODE; //Cipher.DECRYPT_MODE

            byte[] buf = cipherMode == Cipher.ENCRYPT_MODE ? new byte[100]: new byte[128];
            int bufl;

            FileOutputStream outputWriter = new FileOutputStream(tmpdestFile);
            FileInputStream inputReader = new FileInputStream(sourceFile);         
            if(cipherMode == Cipher.ENCRYPT_MODE){
                while ((bufl = inputReader.read(buf)) != -1) {
                    byte[] encText = null;
                    encText = ecipher.doFinal(copyBytes(buf, bufl));
                    System.out.println(new String(encText));
                //  encText = dcipher.doFinal(encText);  // works well...
                    outputWriter.write(encText);
                }
            }else{
                while ((bufl = inputReader.read(buf)) != -1) {
                    byte[] encText = null;
                    encText = dcipher.doFinal(copyBytes(buf, bufl)); // throws exception Bad data...
                    System.out.println(new String(encText));
                    outputWriter.write(encText);
                }
            }
        }
     public static byte[] copyBytes(byte[] arr, int length) {
            byte[] newArr = null;
            if (arr.length == length)
                newArr = arr;
            else {
                newArr = new byte[length];
                for (int i = 0; i < length; i++) {
                    newArr[i] = (byte) arr[i];
                }
            }
            return newArr;
        }

My Stack Trace :

java.security.ProviderException: java.security.KeyException: Bad Data.    
        at sun.security.mscapi.RSACipher.doFinal(RSACipher.java:277)
        at sun.security.mscapi.RSACipher.engineDoFinal(RSACipher.java:301)
        at javax.crypto.Cipher.doFinal(DashoA13*..)
        at FileUploader.decrypt(FileUploader.java:223)
        at FileUploader$4.run(FileUploader.java:414)
        at java.security.AccessController.doPrivileged(Native Method)
        at FileUploader.encryptDecryptFile(FileUploader.java:371)
        at FileUploader.decryptFile(FileUploader.java:362)
        at FileUploader.openFileChooser(FileUploader.java:157)
        at FileUploader.<init>(FileUploader.java:115)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at sun.applet.AppletPanel.createApplet(Unknown Source)
        at sun.applet.AppletPanel.runLoader(Unknown Source)
        at sun.applet.AppletPanel.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: java.security.KeyException: Bad Data.

        at sun.security.mscapi.RSACipher.encryptDecrypt(Native Method)
        at sun.security.mscapi.RSACipher.doFinal(RSACipher.java:269)
        ... 19 more

Look at commented code for better understanding. Please help me where I am wrong.

1

There are 1 answers

0
Balasaheb On BEST ANSWER

Finally I find the solution, actually I have a RSA 2048 bits key and that uses 256 bytes hence, I just modify my peace of code like:

byte[] buf = cipherMode == Cipher.ENCRYPT_MODE ? new byte[100]: new byte[128];

is replaced with :

byte[] buf = cipherMode == Cipher.ENCRYPT_MODE ? new byte[100]: new byte[256];

128 bytes are produced by RSA 1024 bits key and its useful for that key.