使用Cipher类进行对字符串的加密解密,加密没有问题,解密的时候老是出现javax.crypto.IllegalBlockSizeException错误,请问怎么办啊?我粘的诺基亚网站上的代码。
好像要长是8的倍数。
但我不知道为什么
String str = new String(outputBytes, 0, 8);
inputBytes = str.getBytes();
inputBytes 的长度是7
代码如下:
        // Object of this class provides the functionality for
        // encryption and decryption.
        Cipher cipher;
        try {
            // parameter "DES" specifies type of cipher we want to create
            // through the factory method. It includes algorithm, mode and
            // padding. You can define only algorithm and in that case default
            // values will be used for mode and padding.
            cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        } catch (NoSuchAlgorithmException ex) {
            // requested cryptographic algorithm is not available
            printString(ex.toString());
            printString("Error! Snippets execution stopped.");
            return;
        } catch (NoSuchPaddingException ex) {
            // requested padding mechanism is not available in the environment.
            printString(ex.toString());
            printString("Error! Snippets execution stopped.");
            return;
        }
        // The lenght of keyString is 8. It's important characteristic
        // for encryption
        String keyString = "testtest";
        byte[] keyData = keyString.getBytes();
        // key object specifies a secret key
        // it uses to construct the secret key for our cipher object from a byte
        // array
        SecretKeySpec key = new SecretKeySpec(keyData, 0, keyData.length, "DES");
        try {
            // initializing with setting up the type of operation it's to be
            // used for. In this case - for encryption.
            cipher.init(Cipher.ENCRYPT_MODE, key);
        } catch (InvalidKeyException ex) {
            // given key object is inappropriate for initializing this cipher
            printString(ex.toString());
            printString("Error! Snippets execution stopped.");
            return;
        }
        int cypheredBytes = 0;
        printString("Source string: " + textToCrypting);
        byte[] inputBytes = textToCrypting.getBytes();
        byte[] outputBytes = new byte[100];
        try {
            // doFinal method encrypts data to outputBytes array.
            // for unknown or big counts of data update method more recomended
            // counts of crypted bytes saved inside cypheredBytes variable
            
            cypheredBytes = cipher.doFinal(inputBytes, 0, inputBytes.length,
                    outputBytes, 0);
        } catch (IllegalStateException ex) {
            printString(ex.toString());
            printString("Error! Snippets execution stopped.");
            return;        } catch (ShortBufferException ex) {
            printString(ex.toString());
            printString("Error! Snippets execution stopped.");
            return;
        } catch (IllegalBlockSizeException ex) {
            printString(ex.toString());
            printString("Error! Snippets execution stopped.");
            return;
        } catch (BadPaddingException ex) {
            printString(ex.toString());
            printString("Error! Snippets execution stopped.");
            return;
        }
        String str = new String(outputBytes, 0, cypheredBytes);
        inputBytes = str.getBytes();
        printString("Crypted string:" + str);
        try {
            cipher.init(Cipher.DECRYPT_MODE, key);
        } catch (InvalidKeyException ex) {
            printString(ex.toString());
            printString("Error! Snippets execution stopped.");
            return;
        }
        try {
            // decrypts data
            cypheredBytes = cipher.doFinal(inputBytes, 0, inputBytes.length,
                    outputBytes, 0);
        } catch (IllegalStateException ex) {
            printString(ex.toString());
            printString("Error! Snippets execution stopped.");
            return;
        } catch (ShortBufferException ex) {
            printString(ex.toString());
            printString("Error! Snippets execution stopped.");
            return;
        } catch (IllegalBlockSizeException ex) {
            printString(ex.toString());
            printString("Error! Snippets execution stopped.");
            return;
        } catch (BadPaddingException ex) {
            printString(ex.toString());
            printString("Error! Snippets execution stopped.");
            return;
        }
        str = new String(outputBytes, 0, cypheredBytes);
        printString("Decrypted string:" + str);
        printString(" Ok!   " + str);

解决方案 »

  1.   

    http://blog.csdn.net/jiangfeng861016/archive/2009/11/01/4755675.aspx
      

  2.   

    http://topic.csdn.net/u/20100512/19/c7dcc2fa-a561-4d81-9969-b31bea3bfacc.html这里有个 DES 的加密解密代码。你这代码抛出的异常也不知道是哪一行抛出来的!
      

  3.   

    我已经查出来了,就是在解密的时候 
    cypheredBytes = cipher.doFinal(inputBytes, 0,inputBytes.length,outputBytes, 0);
    这句话抛出的。原因是inputBytes.length为7。可我不知道该怎么解决啊
      

  4.   

    怎么突然那么多加解密帖?- -就是解密doFinal的时候被解密串非8的倍数,很好解决
    inputBytes之前已经定长了,重新申请一个byte[] inputBytes1 = new byte[cypheredBytes];
    try {
        str = new String(outputBytes, 0, cypheredBytes, "iso-8859-1");
        inputBytes1 = str.getBytes("iso-8859-1");
    } catch(UnsupportedEncodingException e) {
        e.printStackTrace();
    }
      

  5.   

    谢谢6楼,已经解决了,原来是要进行一下编码控制
    try {
      str = new String(outputBytes, 0, cypheredBytes, "iso-8859-1");
      inputBytes1 = str.getBytes("iso-8859-1");
    } catch(UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    这样就可以了,也不用再申请inputBytes了。也谢谢上面各位,结贴了
      

  6.   

    是的,犯了个概念错误,直接赋inputBytes