java - 由 : java. security.KeyException 引起:错误数据

标签 java applet cryptography cryptoapi

以下代码可以很好地加密数据,但在尝试解密数据时出现错误。

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

我的堆栈跟踪:

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

查看注释代码以更好地理解。请帮我解决我错的地方。

最佳答案

最后我找到了解决方案,实际上我有一个 RSA 2048 位 key ,并且使用 256 字节,因此,我只需修改我的代码,如下所示:

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

替换为:

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

128 字节由 RSA 1024 位 key 生成,并且对于该 key 很有用。

关于java - 由 : java. security.KeyException 引起:错误数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20490953/

相关文章:

java - JSP EL 和范围属性混淆

java - ClassNotFoundException : org. springframework.http.converter.json.MappingJacksonHttpMessageConverter

java - IE11 不断重定向到 Oracle Java 下载站点

java - jre 1.6 的小程序问题

php - 在 PHP/C++ 中加密自定义数据包

java - 如何从命令行运行 Netbeans IDE Java 桌面应用程序

java - 在 arangoDB 的 java 驱动程序中绑定(bind)集合

java - 是否有可能在 Android 或黑莓上运行小程序?

c# - 尝试实例化新的 RSACryptoServiceProvider 时出现 "Keyset does not exist"

java - 如何在 Java 中使用 AES 加密数据