0

I am encrypting the string using AES but it is not decrypting "1234567812345678" character back to plain text from encrypted string. For other text (like "Hello World") it is working fine. Also want to keep the encrypted code to accept only UTF-8 characters. I am using below code

import java.security.*;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class StrongAES 
{

    public static void main(String[] args) 
    {
         //listing all available cryptographic algorithms 

        /* for (Provider provider: Security.getProviders()) {
              System.out.println(provider.getName());
              for (String key: provider.stringPropertyNames())
                System.out.println("\t" + key + "\t" + provider.getProperty(key));
            }*/
        StrongAES saes = new StrongAES();
        String encrypt = saes.encrypt(new String("Bar12346Bar12346"),new String("1234567812345678"));
        //String encrypt = saes.encrypt(new String("Bar12346Bar12346"),new String("Hello world"));
        System.out.println(encrypt);
        String decrypt = saes.decrypt(new String("Bar12346Bar12346"), new String(encrypt));
        System.out.println(decrypt);
    }


    String encrypt(String key, String text) 
    {
        String encryptedText="";
        try{
         // Create key and cipher
        Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");


        // encrypt the text
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);
        byte[] encrypted = cipher.doFinal(text.getBytes());
        encryptedText = new String(encrypted);
       // System.out.println(encryptedText);
        }
        catch(Exception e) 
        {
            e.printStackTrace();
        }
        return encryptedText;

    }

    String decrypt(String key, String encryptedText)
    {
        String decryptedText="";
        try{
         // Create key and cipher
        Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");

        // decrypt the text
        cipher.init(Cipher.DECRYPT_MODE, aesKey);
        decryptedText = new String(cipher.doFinal(encryptedText.getBytes()));
       // System.out.println("Decrypted   "+decryptedText);

        }
        catch(Exception e) 
        {
            e.printStackTrace();
        }
        return decryptedText;

    }

}
Arpan Paliwal
  • 234
  • 1
  • 7
  • 20
  • Possible duplicate of [IllegalBlockSizeException when trying to encrypt and decrypt a string with AES](http://stackoverflow.com/questions/30383736/illegalblocksizeexception-when-trying-to-encrypt-and-decrypt-a-string-with-aes) – Artjom B. Dec 07 '15 at 08:56
  • (1) You're probably using ECB mode which is not very secure. (2) Always use a fully qualified cipher string like `Cipher.getInstance("AES/CBC/PKCS5Padding")` and generate a random IV. (3) You should add authentication to your ciphertexts either with HMAC or by using an authenticated mode like GCM or EAX. – Artjom B. Dec 07 '15 at 08:58

2 Answers2

1

You must keep bytes, not converting to String.

explanations in this post: Problems converting byte array to string and back to byte array

If you want a keep a String, think of convert to B64

String my_string=DatatypeConverter.printBase64Binary(byte_array);

and

byte [] byteArray=DatatypeConverter.parseBase64Binary(my_string);

you can also convert to Hexa (it's bigger)

Community
  • 1
  • 1
0

I have chenged my code to below. Used BASE64Encoder to convert incrypted byte to string same converted with BASE64Decoder while decrypting. And UTF-8 for each getByte.

import java.security.*;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class StrongAES 
{

    public static void main(String[] args) 
    {
        StrongAES saes = new StrongAES();
        String encrypt = saes.encrypt(new String("Bar12346Bar12346"),new String("1234567812345678"));
        System.out.println(encrypt);
        String decrypt = saes.decrypt(new String("Bar12346Bar12346"), new String(encrypt));
        System.out.println(decrypt);
    }


    String encrypt(String key, String text) 
    {
        String encryptedText="";
        try{
         // Create key and cipher
        Key aesKey = new SecretKeySpec(key.getBytes("utf-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        // encrypt the text
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);
        byte[] encrypted = cipher.doFinal(text.getBytes("utf-8"));
        BASE64Encoder encoder = new BASE64Encoder();
        encryptedText = encoder.encodeBuffer(encrypted);
        }
        catch(Exception e) 
        {
            e.printStackTrace();
        }
        return encryptedText;

    }

    String decrypt(String key, String encryptedText)
    {
        String decryptedText="";
        try{
         // Create key and cipher
        Key aesKey = new SecretKeySpec(key.getBytes("utf-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        // decrypt the text
        cipher.init(Cipher.DECRYPT_MODE, aesKey);
        BASE64Decoder decoder = new BASE64Decoder();
        decryptedText = new String(cipher.doFinal(decoder.decodeBuffer(encryptedText)));
        }
        catch(Exception e) 
        {
            e.printStackTrace();
        }
        return decryptedText;

    }

}
Arpan Paliwal
  • 234
  • 1
  • 7
  • 20