Skip to content

Instantly share code, notes, and snippets.

@ytyng
Created March 31, 2020 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ytyng/59eaa333f3cdf46074cadfb5736161b8 to your computer and use it in GitHub Desktop.
Save ytyng/59eaa333f3cdf46074cadfb5736161b8 to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:encrypt/encrypt.dart' as encrypt;
import 'package:crypto/crypto.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyScaffold(),
);
}
class MyScaffold extends StatelessWidget {
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('My App'),
),
body: body(context));
Widget body(BuildContext context) {
final password = 'passwordpass';
final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
var encrypted = encryptString(sourcePlainText: plainText, password: password);
print('encrypted: ${encrypted.b64Cipher}');
print('iv: ${encrypted.b64IV}');
var decrypted = encrypted.decrypt(password);
print('decrypted: $decrypted');
var civ = CipherWithIV(
b64Cipher: 'HcW/1CqTA+ZagAsRu8OFtiXLaopteUdQJAQQum46Bq8Mho4d/wuCqXxHmyMY5RiMHPvrlKq0rNlUCFM0Ek9wvQ==',
b64IV: 'al1YZpr2w7InNndD6DDfRw==',
);
var decrypted2 = civ.decrypt('passwordpass');
return Text(decrypted2);
}
}
class CipherWithIV{
final String b64Cipher;
final String b64IV;
CipherWithIV({this.b64Cipher, this.b64IV});
String decrypt(String password) {
final iv = encrypt.IV.fromBase64(b64IV);
final encrypter = getEncrypter(password);
return encrypter.decrypt(
encrypt.Encrypted.fromBase64(b64Cipher), iv: iv);
}
static encrypt.Encrypter getEncrypter(String password) {
final hashedPassword = sha256.convert(utf8.encode(password));
final key = encrypt.Key(hashedPassword.bytes);
return encrypt.Encrypter(
encrypt.AES(key, mode: encrypt.AESMode.cbc));
}
}
CipherWithIV encryptString({String sourcePlainText, String password}) {
final iv = encrypt.IV.fromSecureRandom(16);
final encrypter = CipherWithIV.getEncrypter(password);
final encrypted = encrypter.encrypt(
sourcePlainText, iv: iv);
return CipherWithIV(
b64Cipher: encrypted.base64,
b64IV: iv.base64
);
}
import base64
from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
def pkcs7(source):
return source + chr(16 - len(source) % 16) * (16 - len(source) % 16)
def unpad(ct):
return ct[:-ct[-1]]
class CipherWithIV():
def __init__(self, *, b64cipher: str, b64iv: str):
self.b64cipher = b64cipher
self.b64iv = b64iv
def decrypt(self, password: str) -> str:
encryper = get_encrypter(password, base64.b64decode(
self.b64iv.encode('utf-8')))
return unpad(
encryper.decrypt(base64.b64decode(
self.b64cipher.encode('utf-8')))).decode('utf-8')
def get_encrypter(password: str, iv: bytes):
sha = SHA256.new()
sha.update(password.encode())
hashed_password = sha.digest()
return AES.new(hashed_password, AES.MODE_CBC, iv)
def encrypt_string(*, source_plain_text: str, password: str):
iv = Random.new().read(AES.block_size)
encrypter = get_encrypter(password, iv)
return CipherWithIV(
b64cipher=base64.b64encode(
encrypter.encrypt(pkcs7(source_plain_text))).decode('utf-8'),
b64iv=base64.b64encode(iv).decode('utf-8')
)
def main():
password = 'passwordpass'
plain_text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'
cipheriv = encrypt_string(source_plain_text=plain_text, password=password)
print(f'cipher: {cipheriv.b64cipher}')
print(f'iv: {cipheriv.b64iv}')
decrypted = cipheriv.decrypt(password)
print(f'decrypted: {decrypted}')
cipheriv2 = CipherWithIV(
b64cipher='/wcD/RVapLkpul2eQCDusPrJsF15ZWpkzkGRRYdQvyzAEZeJfxVyVqs1qt/w1Zf70KM6/1j0O+wgEuEXzyYSkw==',
b64iv='sdE5p0jzxEADLsssB1Hj1g=='
)
decrypted2 = cipheriv2.decrypt('passwordpass')
print(f'decrypted2: {decrypted2}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment