Skip to content

Instantly share code, notes, and snippets.

@stevenroose
Created March 4, 2014 20:22
Show Gist options
  • Save stevenroose/9354814 to your computer and use it in GitHub Desktop.
Save stevenroose/9354814 to your computer and use it in GitHub Desktop.
EncryptedPrivateKey encrypt(Uint8List privKey, KeyParameter aesKey) {
if(privKey == null || aesKey == null) throw new ArgumentError();
Uint8List iv = new Uint8List(BLOCK_LENGTH);
// TODO fill iv with random bytes from securerandom
ParametersWithIV keyWithIv = new ParametersWithIV(aesKey, iv);
PaddedBlockCipher cipher = new PaddedBlockCipherImpl(new PKCS7Padding(), new CBCBlockCipher(new AESFastEngine()));
cipher.init(true, keyWithIv);
Uint8List encryptedKey = cipher.process(privKey);
return new EncryptedPrivateKey(encryptedKey, iv);
}
Uint8List decrypt(EncryptedPrivateKey encryptedPrivKey, KeyParameter aesKey) {
if(encryptedPrivKey == null || aesKey == null) throw new ArgumentError();
ParametersWithIV keyWithIv = new ParametersWithIV(aesKey, encryptedPrivKey.iv);
PaddedBlockCipher cipher = new PaddedBlockCipherImpl(new PKCS7Padding(), new CBCBlockCipher(new AESFastEngine()));
cipher.init(false, keyWithIv);
return cipher.process(encryptedPrivKey.encryptedKey);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment