0

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 );
    }
}
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
  • With 16 bytes to encrypt it is the first time you use 2 blocks (because of padding). So likely there is some mismatch in the encryption mode between encryption and decryption. See also https://stackoverflow.com/questions/6258047/java-default-crypto-aes-behavior – Henry Dec 21 '18 at 06:58
  • Thanks for your response, but I have also tried "AES/ECB/PKCS5Padding" but my result is not coming correct. Can you please provide a brief more detail – user3655492 Dec 21 '18 at 07:18
  • Can you show us your decryption code? – Henry Dec 21 '18 at 07:29
  • Sure , to explain the complete scenario can you please whatsapp me this number +917986430992 or email me on kharbandakuber@gmail.com. – user3655492 Dec 21 '18 at 07:41
  • public static String decrypt(String strToDecrypt, String secret) { setKey(secret); // Cipher cipher = Cipher.getInstance("AES"); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, secretKey); System.out.println("Print:" + secret.toString()); return new String(cipher.doFinal(org.kobjects.base64.Base64.decode(strToDecrypt))); } Also, I am testing the result here online https://encode-decode.com/aes-128-cbc-encrypt-online/ – user3655492 Dec 21 '18 at 07:56
  • PKCS5Padding vs. NoPadding – Henry Dec 21 '18 at 07:58
  • ok, i tried with PKCS5Padding on decryption but still the result the encrypted string came half correct & the second half incorrect, if possible sir can i share the full source code on email please ,,, would be highly thankful my mail id is kharbandakuber@gmail.com... you can share me ur mail id here on my gmail – user3655492 Dec 21 '18 at 08:05
  • removed unnecessary imports, Improved grammar and formatting – Rohit Singh Dec 21 '18 at 10:06
  • Removed the unnecessary code but the result not coming correct for encryption using AES/CBC/PKCS5Padding....I have tried a lott – user3655492 Dec 21 '18 at 10:14
  • But the same code is working correctly for 15 digit number and for 16 digit half of the encrypted string is coming ok and the second half incorrec....I am testing @ https://encode-decode.com/aes-128-cbc-encrypt-online/ – user3655492 Dec 21 '18 at 10:22

0 Answers0