I have a code for AES encryption that is implemented below. And,it's working correctly up to 15 digit numbers but the same code is not showing correct result for 16 digits number. For example "4111111111111111". I don't know why this is happening, it is encrypting 16 digit number but when I am trying to decrypt it, it shows a wrong result
I have tried almost all algorithms but failed.
Thanks in advance.
public class AES {
private static SecretKeySpec secretKey;
private static byte[] key;
public static void setKey(String myKey)
{
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static String encrypt(String strToEncrypt, String secret)
{
try
{
setKey(secret);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.encodeToString((cipher.doFinal(strToEncrypt.getBytes("UTF-8"))),Base64.DEFAULT);
}
catch (Exception e)
{
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
My Activity code
public class MainActivity extends AppCompatActivity
{
private static final String Password = "ABCDEFGHIJKLMNOP";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String toEncode = "4111111111111111";
String enC ;
enC = AES.encrypt(toEncode, Password);
System.out.println("ENC: " + enC );
}
}