Search code examples
javaencryptionaes

AES/CFB/NOPADDING Encrypt to Decrypt not working


can anyone guide me where this is going wrong? The encryption to decryption doesn't work in this scenario.

public static byte[] encrypt(String value)
  throws GeneralSecurityException {
KeyGenerator generator1 = KeyGenerator.getInstance("AES");
generator1.init(128);

Cipher cipher = Cipher.getInstance("AES/CFB/NOPADDING");
cipher.init(Cipher.ENCRYPT_MODE, generator1.generateKey(),
    new IvParameterSpec(new byte[16]));
return cipher.doFinal(value.getBytes(Charset.forName("UTF-8")));

}

public static String decrypt(byte[] encrypted) throws GeneralSecurityException {

KeyGenerator generator1 = KeyGenerator.getInstance("AES");
generator1.init(128);

Cipher cipher = Cipher.getInstance("AES/CFB/NOPADDING");
cipher.init(Cipher.DECRYPT_MODE, generator1.generateKey(),
    new IvParameterSpec(new byte[16]));
byte[] original = cipher.doFinal(encrypted);

return new String(original, Charset.forName("UTF-8"));

}


Solution

  • You are basically generating a new symmetric key for each operation (encryption/decryption). But symmetric encryption algorithms require the same key for encryption as for decryption of the same value. So you can instead try:

    import java.security.GeneralSecurityException;
    import java.util.Arrays;
    import java.util.Random;
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.IvParameterSpec;
    
    public class Main {
        
        private static byte[] encrypt(final SecretKey key,
                                      final IvParameterSpec iv,
                                      final byte[] value) throws GeneralSecurityException {
            final Cipher cipher = Cipher.getInstance("AES/CFB/NOPADDING");
            cipher.init(Cipher.ENCRYPT_MODE, key, iv);
            return cipher.doFinal(value);
        }
        
        private static byte[] decrypt(final SecretKey key,
                                      final IvParameterSpec iv,
                                      final byte[] encrypted) throws GeneralSecurityException {
            final Cipher cipher = Cipher.getInstance("AES/CFB/NOPADDING");
            cipher.init(Cipher.DECRYPT_MODE, key, iv);
            return cipher.doFinal(encrypted);
        }
        
        public static void main(final String[] args) throws GeneralSecurityException {
            //Generate the secret symmetric key once:
            final KeyGenerator generator = KeyGenerator.getInstance("AES");
            generator.init(128);
            final SecretKey k = generator.generateKey();
            
            //Generate the same IV once:
            final IvParameterSpec iv = new IvParameterSpec(new byte[16]);
            
            //Generate a random plain text to be tested:
            final byte[] value = new byte[32];
            new Random().nextBytes(value);
            
            System.out.println(Arrays.equals(value, decrypt(k, iv, encrypt(k, iv, value))));
        }
    }