views:

62

answers:

2

Hi, I have following programme for encrypts a data.

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class Test {

    private static final String ALGORITHM = "AES";
    private static final byte[] keyValue = "ADBSJHJS12547896".getBytes();

    public static void main(String args[]) throws Exception {
        String encriptValue = encrypt("dude5");
        decrypt(encriptValue);

    }

    /**
     * @param args
     * @throws Exception
     */

    public static String encrypt(String valueToEnc) throws Exception {

        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.ENCRYPT_MODE, key);

        System.out.println("valueToEnc.getBytes().length "+valueToEnc.getBytes().length);
        byte[] encValue = c.doFinal(valueToEnc.getBytes());
        System.out.println("encValue length" + encValue.length);
        byte[] encryptedByteValue = new Base64().encode(encValue);
        String encryptedValue = encryptedByteValue.toString();
        System.out.println("encryptedValue " + encryptedValue);

        return encryptedValue;
    }

    public static String decrypt(String encryptedValue) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);

        byte[] enctVal = c.doFinal(encryptedValue.getBytes());
        System.out.println("enctVal length " + enctVal.length);

        byte[] decordedValue = new Base64().decode(enctVal);

        return decordedValue.toString();
    }

    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGORITHM);
        return key;
    }

}

Here I am getting the following out put with exception?

valueToEnc.getBytes().length 5
encValue length16
encryptedValue [B@aa9835
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)

Can some one explain me the cause? Why its only saying when decrypting that length should be 16. Doesn't it convert to 16 as like encrypting with the doFinal method.

And as the exception says "how to decrypting without padded cipher?"

Thank You.

A: 

Fundamentally, there is an asymmetry between your encrypt function and your decrypt function. When you encrypt you perform an AES encrypt and then a base64 encode, when you decrypt you don't first undo the base64 encoding step.

I think that there's something wrong with your base64 encoding as well as [ shouldn't appear in a base64 encoded string.

Looking at the documentation for org.apache.commons.codec.binary.Base64 you should be able to do this on encode:

String encryptedValue = Base64.encodeBase64String(encValue);

and this on decode:

byte[] encValue = Base64.decodeBase64(encryptedValue);
Charles Bailey
thers is no method as encodeBase64String charles
Harshana
@Harshana: What's this, then? http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html#encodeBase64String%28byte[]%29
Charles Bailey
A: 

The line

String encryptedValue = encryptedByteValue.toString();

is the problem. The type of encryptedByteValue is byte[] and calling toString on it isn't what you want to do there. Instead try

String encryptedValue = Base64.encodeToString(encValue);

Then use Base64.decodeBase64(encryptedValue) in decrypt. You must do that prior to attempting to decrypt though. You must undo the operations in the reverse order of the encrypt method.

laz
its just add there to return a String from the method..also in Base64 there is no such method encodeToString
Harshana
Regarding the toString, calling that method on an Array is almost never what you want to do. It returns the address in memory of the object, not a useful String representation. Regarding Base64, aren't you using this? http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html See the method here: http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html#encodeToString%28byte[]%29
laz
ive put commons-codec-1.2 and also try 1.3 jar but seems that method is not display know..
Harshana
It is in commons-code 1.4 according to that Javadoc I linked to.
laz