Android AES 256-bit Encrypt data

后端 未结 1 403
一生所求
一生所求 2020-12-23 23:56

So I\'ve seen a lot of examples, and done a lot of googling, and looked at examples on Stack Overflow... and I need help. I\'ve got an Android application and I\'m storing u

相关标签:
1条回答
  • 2020-12-24 00:31

    EDIT: While the code below is correct, what you have is doing basically the same thing, with the IV derived from the password, so you don't have to store it separately.

    Does your code work as expected? For the actual encryption/decryption you would want to use AES, most probably in CBC mode. Then you would need an IV, so it becomes something like this:

    ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    byte[] iv = new byte[IV_LENGTH];
    SecureRandom random = new SecureRandom();
    random.nextBytes(iv);
    ecipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv));
    byte[] enc = ecipher.doFinal(utf8);
    

    Whether it is secure depends on what you are using this for. The purpose of the salt is to make it harder to brute force the passphrase: if it's random the attacker cannot use pre-generated passphrase tables (passphrase->key). If you are not too worried about this sort of attack, you might leave it fixed. If you decide to make it random, just store it with the encrypted data. Same with the IV.

    0 讨论(0)
提交回复
热议问题