Created
November 6, 2019 13:04
-
-
Save long1eu/e518c969e01f8e1dd44c12b779a6c1bf to your computer and use it in GitHub Desktop.
AES/CBC/PKCS7Padding for dart using pointycastle
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
import 'dart:convert'; | |
import 'dart:typed_data'; | |
import 'package:flutter/material.dart'; | |
import 'package:pointycastle/api.dart'; | |
import 'package:pointycastle/block/aes_fast.dart'; | |
import 'package:pointycastle/block/modes/cbc.dart'; | |
import 'package:pointycastle/padded_block_cipher/padded_block_cipher_impl.dart'; | |
import 'package:pointycastle/paddings/pkcs7.dart'; | |
void main() { | |
runApp(new MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'AES256/CBC', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const EncryptionPage(), | |
); | |
} | |
} | |
class EncryptionPage extends StatefulWidget { | |
const EncryptionPage(); | |
@override | |
_EncryptionPageState createState() => _EncryptionPageState(); | |
} | |
class _EncryptionPageState extends State<EncryptionPage> { | |
String encrypted; | |
final TextEditingController controller = TextEditingController(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('AES256/CBC'), | |
), | |
body: ListView( | |
padding: const EdgeInsets.all(16.0), | |
children: <Widget>[ | |
Container( | |
child: TextField( | |
controller: controller, | |
), | |
), | |
RaisedButton( | |
child: Text('Encrypt'), | |
onPressed: () { | |
if (controller.text.isNotEmpty) { | |
setState(() { | |
encrypted = Encryption.encrypt(controller.text); | |
}); | |
} | |
}, | |
), | |
if (encrypted != null) | |
Container( | |
margin: const EdgeInsets.all(16.0), | |
child: SelectableText(encrypted), | |
), | |
if (encrypted != null) | |
RaisedButton( | |
child: Text('Decrypt'), | |
onPressed: () { | |
setState(() { | |
if (encrypted != null && encrypted.isNotEmpty) { | |
final String decrypt = Encryption.decrypt(encrypted); | |
controller.text = decrypt; | |
encrypted = null; | |
} | |
}); | |
}, | |
), | |
], | |
), | |
); | |
} | |
} | |
//Z"b~dERuX!q&zj-,>d}6aEw-_9&xJ^@8 | |
class Encryption { | |
static final Uint8List _key = new Uint8List.fromList(<int>[ | |
0x5a, | |
0x22, | |
0x62, | |
0x7e, | |
0x64, | |
0x45, | |
0x52, | |
0x75, | |
0x58, | |
0x21, | |
0x71, | |
0x26, | |
0x7a, | |
0x6a, | |
0x2d, | |
0x2c, | |
0x3e, | |
0x64, | |
0x7d, | |
0x36, | |
0x61, | |
0x45, | |
0x77, | |
0x2d, | |
0x5f, | |
0x39, | |
0x26, | |
0x78, | |
0x4a, | |
0x5e, | |
0x40, | |
0x38 | |
]); | |
static final Uint8List _iv = new Uint8List.fromList( | |
<int>[ | |
0xda, | |
0x39, | |
0xa3, | |
0xee, | |
0x5e, | |
0x6b, | |
0x4b, | |
0x0d, | |
0x32, | |
0x55, | |
0xbf, | |
0xef, | |
0x95, | |
0x60, | |
0x18, | |
0x90 | |
], | |
); | |
static String encrypt(String text) { | |
return base64Encode(encryptList(utf8.encode(text))); | |
} | |
static Uint8List encryptList(Uint8List data) { | |
final CBCBlockCipher cbcCipher = new CBCBlockCipher(new AESFastEngine()); | |
final ParametersWithIV<KeyParameter> ivParams = | |
new ParametersWithIV<KeyParameter>(new KeyParameter(_key), _iv); | |
final PaddedBlockCipherParameters<ParametersWithIV<KeyParameter>, Null> | |
paddingParams = | |
new PaddedBlockCipherParameters<ParametersWithIV<KeyParameter>, Null>( | |
ivParams, null); | |
final PaddedBlockCipherImpl paddedCipher = | |
new PaddedBlockCipherImpl(new PKCS7Padding(), cbcCipher); | |
paddedCipher.init(true, paddingParams); | |
try { | |
return paddedCipher.process(data); | |
} catch (e) { | |
print(e); | |
return null; | |
} | |
} | |
static String decrypt(String data) => | |
utf8.decode(decryptList(base64Decode(data))); | |
static Uint8List decryptList(Uint8List data) { | |
final CBCBlockCipher cbcCipher = new CBCBlockCipher(new AESFastEngine()); | |
final ParametersWithIV<KeyParameter> ivParams = | |
new ParametersWithIV<KeyParameter>(new KeyParameter(_key), _iv); | |
final PaddedBlockCipherParameters<ParametersWithIV<KeyParameter>, Null> | |
paddingParams = | |
new PaddedBlockCipherParameters<ParametersWithIV<KeyParameter>, Null>( | |
ivParams, null); | |
final PaddedBlockCipherImpl paddedCipher = | |
new PaddedBlockCipherImpl(new PKCS7Padding(), cbcCipher); | |
paddedCipher.init(false, paddingParams); | |
try { | |
return paddedCipher.process(data); | |
} catch (e) { | |
print(e); | |
return null; | |
} | |
} | |
} |
👍
Great solution
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use a passphrase without using the predefined _key?