【问题标题】:Caused by: java.security.KeyException: Bad Data原因:java.security.KeyException:错误数据
【发布时间】:2013-12-10 09:45:05
【问题描述】:

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

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

查看注释代码以获得更好的理解。请帮助我哪里错了。

【问题讨论】:

  • 您希望密钥采用什么格式?仅当您计划导出密钥以在 Java 之外使用时,格式才重要 - 有关您打算如何使用密钥的信息会很有帮助。
  • @Duncan 我可以使用公钥加密数据,但我需要私钥来解密相同的数据,因此每当我传递该私钥进行解密时,它都会抛出 NullPointerException。
  • 有意思,能把堆栈跟踪加到问题里吗?
  • @Duncan 我添加了一个方法,在我编辑的问题中执行解密和堆栈跟踪。请帮帮我。
  • @Duncan 嗨,Duncan,我已经编辑了我的问题,因为我可以使用接收私钥解密数据。但是我在上面提到的问题出了点问题。

标签: java applet cryptography cryptoapi


【解决方案1】:

最后我找到了解决方案,实际上我有一个 RSA 2048 位密钥,因此使用 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 位密钥生成,它对该密钥有用。

【讨论】:

    猜你喜欢
    • 2017-05-22
    • 2016-02-26
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多