Created
March 4, 2014 20:22
-
-
Save stevenroose/9354814 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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