1

This code is without initialisation vector with simple AES-256 and its working perfect. But when i am comparing the iOS app it not same because they have to give the IV key compulsory so encryption does match in both app. When i tried to add that IV key in android i didn't get the output.

byte[] iv = c.getIV();
c.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));

But in this code i didn't get where to enter the key of 16bytes which is given by the iOS developer.

public class MainActivity extends AppCompatActivity {

    private EditText ed_encry;
    private EditText ed_decry;
    private Button btn_encry;
    private Button btn_decry;
    private TextView tv_key;
    private String outputString;
    String AES = "AES";
    private String password = "PGratikHadiaPGratikHadiaPGPGPGPG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed_encry = (EditText) findViewById(R.id.ed_encry);
        btn_encry = (Button) findViewById(R.id.btn_encry);
        btn_decry = (Button) findViewById(R.id.btn_decry);
        tv_key = (TextView) findViewById(R.id.tv_key);

        btn_encry.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    outputString = encrypt(ed_encry.getText().toString());
                    tv_key.setText(outputString);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        btn_decry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    outputString = decrypt(tv_key.getText().toString());
                    tv_key.setText(outputString);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private String decrypt(String data) throws Exception {
        SecretKeySpec key = generateKey(password);
        Cipher c = Cipher.getInstance(AES);
        c.init(Cipher.DECRYPT_MODE,key);
        byte[] decodedValue = Base64.decode(data, Base64.DEFAULT);
        byte[] decValue = c.doFinal(decodedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private String encrypt(String data) throws Exception {
        SecretKeySpec key = generateKey(password);
        Cipher c = Cipher.getInstance(AES);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(data.getBytes());
        String encryptedValue = Base64.encodeToString(encVal, Base64.DEFAULT);
        return encryptedValue;
    }

    private SecretKeySpec generateKey(String data) throws Exception {
        final MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] bytes = data.getBytes("UTF-8");
        digest.update(bytes, 0, bytes.length);
        byte[] key = digest.digest();
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        return secretKeySpec;
    }
}
rustyx
  • 80,671
  • 25
  • 200
  • 267
Shubham Sejpal
  • 3,556
  • 2
  • 14
  • 31
  • 1
    `Cipher c = Cipher.getInstance("AES");`. Don't do that. Always specify the full *algorithm/mode/padding* string. – President James K. Polk Oct 02 '18 at 13:39
  • 1
    `Cipher.getInstance("AES")` defaults to AES/ECB/PKCS5Padding. ECB mode does not use an IV. Check which mode you need first, and how the IV is stored. Also see [this](https://stackoverflow.com/questions/31036780/android-cryptography-api-not-generating-safe-iv-for-aes) – rustyx Oct 02 '18 at 19:53
  • ECB doesn't have an IV which is one of the reasons why it is bad. Also, if you want to check different implementations and still use randomly generated IVs, you need to encrypt in one, decrypt in the other and validate that the original and decrypted files were equal. Then you can repeat the process in the other direction. – Artjom B. Oct 02 '18 at 22:11
  • Ok @JamesKPolk for help it works for me by giving the full defaults value in getInstance("AES/ECB/PKCS5Padding"). – Shubham Sejpal Oct 03 '18 at 05:31
  • Ok @rustyx for help it works for me by giving the full defaults value in getInstance("AES/ECB/PKCS5Padding"). – Shubham Sejpal Oct 03 '18 at 05:31

0 Answers0