Encryption and Decryption using Blowfish Error - Input length must be multiple of 8 when decrypting with padded cipher

后端 未结 3 1067
囚心锁ツ
囚心锁ツ 2021-02-06 19:59

I am able to encrypt data however when decrypting it i am getting the following error:

Error

HTTP Status 500 - Request processing failed         


        
相关标签:
3条回答
  • 2021-02-06 20:00

    If you run your encrypt and decrypt methods in a main method, it will work. But if the results of encrypt are put into a url and then the url parameter is decrypted, it will fail.

    After encryption, the byte array contains values that are outside the character set of URLS (non-ascii), so this value gets encoded when it is stuffed into a url. And you you receive a corrupted version for decryption.

    As an example, when I created a string from an encrypted byte array, it looked like this Ž¹Qêz¦ but if I put it into a URL it turns into Ž%0B¹Qêz¦.

    The fix, as suggested in other comments, is to add a encode / decode step. After encryption, the value should be encoded to a format which contains ascii characters. Base 64 is an excellent choice. So you return encrypted and encoded value in the url. When you receive the param, first decode then decrypt, and you'll get the original data.

    Here are some notes on the implementation.

    1. Use a library like commons codec. It is my weapon of choice, this class specifically http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html.

    2. In the class that does encryption and decryption, have a shared instance of Base64. To instantiate it use new Base64(true); this produces url safe strings.

    3. Your encrypt and decrypt method signatures should accept and return strings, not byte arrays.

    4. So the last line of your encrypt would become something like return base64.encodeToString(cipher.doFinal(Data.getBytes("UTF8"))); You can now safely pass the encrypted value in a url

    5. In your decrypt, you first step is to decode. So the first line would become something like byte[] encryptedData = base64.decodeBase64(encrypted);

    I just took your code and added some base 64 stuff, the result looks like this:

    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    
    import org.apache.commons.codec.binary.Base64;
    
    
    public class Test {
    
        private static String strkey ="Blowfish";
        private static Base64 base64 = new Base64(true);
    
         //encrypt using blowfish algorithm
        public static String encrypt(String Data)throws Exception{
    
            SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");
             Cipher cipher = Cipher.getInstance("Blowfish");
             cipher.init(Cipher.ENCRYPT_MODE, key);
    
             return base64.encodeToString(cipher.doFinal(Data.getBytes("UTF8")));
    
        }
    
        //decrypt using blow fish algorithm
        public static String decrypt(String encrypted)throws Exception{
            byte[] encryptedData = base64.decodeBase64(encrypted);
             SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");
             Cipher cipher = Cipher.getInstance("Blowfish");
             cipher.init(Cipher.DECRYPT_MODE, key);
             byte[] decrypted = cipher.doFinal(encryptedData);
             return new String(decrypted); 
    
        }
    
        public static void main(String[] args) throws Exception {
            String data = "will this work?";
            String encoded = encrypt(data);
            System.out.println(encoded);
            String decoded = decrypt(encoded);
            System.out.println(decoded);
        }
    }
    

    Hope this answers your questions.

    0 讨论(0)
  • 2021-02-06 20:08

    You can't create a String out of random (in this case encrypted) bytes like you're doing in the last line of your encrypt method - you need to create a Base64 encoded string instead (which you then need to decode back to a byte array in the decrypt method). Alternatively, just have your encrypt method return a byte array and have your decrypt method accept a byte array as its parameter.

    0 讨论(0)
  • 2021-02-06 20:11

    The problem is with the way you are creating String instances out of the raw encrypted byte[] data. You need to either use binhex encoding like that provided by javax.xml.bind.DatatypeConverter via the parseHexBinary and printHexBinary methods or base 64 using the parseBase64Binary and printBase64Binary methods of the same object.

    One other word of advice, never rely on the default mode and padding, always be explicit. Use something like Cipher.getInstance("Blowfish/CBC/PKCS5Padding") depending on what your needs are.

    0 讨论(0)
提交回复
热议问题