This repository has been archived by the owner on Dec 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an AES encryption and decryption example
- Loading branch information
1 parent
da19531
commit 9974354
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
library example.aes; | ||
|
||
import "dart:convert"; | ||
import "dart:typed_data"; | ||
|
||
import "package:hex/hex.dart"; | ||
import "package:pointycastle/pointycastle.dart"; | ||
|
||
void main() { | ||
// Key must be multiple of block size (16 bytes). | ||
var key = new Digest("SHA-256").process( | ||
utf8.encode("correct horse battery staple")); | ||
// Can be anything. | ||
var message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed d" | ||
"o eiusmod tempor incididunt ut labore et dolore magna aliqua."; | ||
// The initialization vector must be unique for every message, so it is a | ||
// good idea to use a message digest as the IV. | ||
// IV must be equal to block size (16 bytes). | ||
var iv = new Digest("SHA-256").process(utf8.encode(message)).sublist(0, 16); | ||
// The parameters your cipher will need. (PKCS7 does not need params.) | ||
CipherParameters params = new PaddedBlockCipherParameters( | ||
new ParametersWithIV(new KeyParameter(key), iv), null); | ||
|
||
print("Message: \n$message"); | ||
|
||
//////////////// | ||
// Encrypting // | ||
//////////////// | ||
// As for why you would need CBC mode and PKCS7 padding, consult the internet | ||
// (f.e. http://www.di-mgt.com.au/properpassword.html). | ||
BlockCipher encryptionCipher = new PaddedBlockCipher("AES/CBC/PKCS7"); | ||
encryptionCipher.init(true, params); | ||
Uint8List encrypted = encryptionCipher.process(utf8.encode(message)); | ||
|
||
print("Encrypted: \n" + HEX.encode(encrypted)); | ||
|
||
|
||
//////////////// | ||
// Decrypting // | ||
//////////////// | ||
BlockCipher decryptionCipher = new PaddedBlockCipher("AES/CBC/PKCS7"); | ||
decryptionCipher.init(false, params); | ||
String decrypted = utf8.decode(decryptionCipher.process(encrypted)); | ||
|
||
print("Decrypted: \n$decrypted"); | ||
} |
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