I want to add the IV (initialisation vector) of 16bytes in AES-256 algorithm end to end encryption

时间:2018-10-02 09:14:50

标签: java android encryption aes

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;
    }
}

0 个答案:

没有答案