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
Add an AES encryption and decryption example #111
Open
stevenroose
wants to merge
1
commit into
PointyCastle:master
Choose a base branch
from
stevenroose:aes-example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Taking the hash of the plaintext for the IV might not be a good idea, as it leaks information about the plaintext. If the text is encrypted twice the IV will be the same. Might be better to use a PRNG. |
||
// 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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lower cased |
||
|
||
|
||
//////////////// | ||
// 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ dev_dependencies: | |
build_runner: ^0.10.0 | ||
build_test: ^0.10.2 | ||
build_web_compilers: ^0.4.0 | ||
hex: ">=0.1.1 <1.0.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use stdlib. |
||
matcher: ">=0.12.0 <0.13.0" | ||
test: ">=0.12.30 <1.4.0" | ||
analyzer: "0.32.4" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need, use standard library (included in
dart:convert
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't seem to find hex in here: https://api.dartlang.org/stable/2.0.0/dart-convert/dart-convert-library.html