encrypt 패키지는 PointyCastle 기반으로 Flutter 애플리케이션에서 양방향 암호화를 위한 고수준이고 사용하기 쉬운 API를 제공합니다. AES, RSA, DES와 같은 다양한 알고리즘을 지원하여 암호화 및 복호화 과정을 간소화합니다. 보안 데이터 처리가 필요한 개발자를 위해 설계되었으며, 여러 플랫폼 간에 민감한 정보를 안전하게 저장하고 전송할 수 있게 해줍니다. 낮은 수준의 암호화 복잡성을 추상화함으로써, 깊은 암호화 전문 지식이 없는 사람들도 안전한 코드 작성에 접근할 수 있도록 합니다.
사용 사례
안전한 데이터 저장
사용자 자격 증명 암호화
클라이언트와 서버 간의 안전한 통신
민감한 앱 데이터 보호
엔드 투 엔드 암호화 구현
토큰 기반 인증
주요 기능
AES, RSA 및 DES 지원
간단하고 직관적인 API
크로스 플랫폼 호환성
PointyCastle와의 통합
내장형 패딩 및 모드 옵션
유연한 키 및 IV 관리
적합한 대상
데이터 기밀성 요구 앱
암호화 초보 개발자
안전한 로컬 저장소가 필요한 프로젝트
민감한 사용자 정보를 처리하는 애플리케이션
보안 요구 사항이 있는 다중 플랫폼 앱
고려 사항
신중한 키 관리가 필요합니다
암호 해시에 적합하지 않습니다
PointyCastle의 안정성에 따라 달라집니다
적절한 오류 처리와 함께 사용되어야 합니다
보안 키 저장 솔루션과 가장 잘 어울립니다
소스 코드에 키를 하드코딩하지 마십시오
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
[!CAUTION]
I'm not being able to properly maintain this package, so it is being archived. Feel free to use some fork as an alternative.
encrypt
A set of high-level APIs over PointyCastle for two-way cryptography.
Looking for password hashing? Please, visit password.
Secure random
You can generate cryptographically secure random keys and IVs for you project.
$ secure-random --help
-l, --length The length of the bytes
(defaults to "32")
-b, --base Bytes represented as base 64 or base 16 (Hexdecimal)
(defaults to "64")
-h, --[no-]help Show this help message
Algorithms
Current status is:
AES with PKCS7 padding
RSA with PKCS1 and OAEP encoding
Salsa20
Signing
SHA256 with RSA
Usage
Symmetric
AES
import'package:encrypt/encrypt.dart';
voidmain() {
final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
final key = Key.fromUtf8('my 32 length key................');
final iv = IV.fromLength(16);
final encrypter = Encrypter(AES(key));
final encrypted = encrypter.encrypt(plainText, iv: iv);
final decrypted = encrypter.decrypt(encrypted, iv: iv);
print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elitprint(encrypted.base64); // R4PxiU3h8YoIRqVowBXm36ZcCeNeZ4s1OvVBTfFlZRdmohQqOpPQqD1YecJeZMAop/hZ4OxqgC1WtwvX/hP9mw==
}
Modes of operation
Default mode is SIC AESMode.sic, you can override it using the mode named parameter:
final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
Supported modes are:
CBC AESMode.cbc
CFB-64 AESMode.cfb64
CTR AESMode.ctr
ECB AESMode.ecb
OFB-64/GCTR AESMode.ofb64Gctr
OFB-64 AESMode.ofb64
SIC AESMode.sic
No/zero padding
To remove padding, pass null to the padding named parameter on the constructor:
final encrypter = Encrypter(AES(key, mode: AESMode.cbc, padding: null));
Salsa20
import'package:encrypt/encrypt.dart';
voidmain() {
final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
final key = Key.fromLength(32);
final iv = IV.fromLength(8);
final encrypter = Encrypter(Salsa20(key));
final encrypted = encrypter.encrypt(plainText, iv: iv);
final decrypted = encrypter.decrypt(encrypted, iv: iv);
print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elitprint(encrypted.base64); // CR+IAWBEx3sA/dLkkFM/orYr9KftrGa7lIFSAAmVPbKIOLDOzGwEi9ohstDBqDLIaXMEeulwXQ==
}
import'package:encrypt/encrypt.dart';
import'dart:convert';
voidmain() {
final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
final key = Key.fromUtf8('my32lengthsupersecretnooneknows1');
final b64key = Key.fromUtf8(base64Url.encode(key.bytes).substring(0,32));
// if you need to use the ttl feature, you'll need to use APIs in the algorithm itselffinal fernet = Fernet(b64key);
final encrypter = Encrypter(fernet);
final encrypted = encrypter.encrypt(plainText);
final decrypted = encrypter.decrypt(encrypted);
print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elitprint(encrypted.base64); // random cipher textprint(fernet.extractTimestamp(encrypted.bytes)); // unix timestamp
}
Asymmetric
RSA
import'dart:io';
import'package:encrypt/encrypt.dart';
import'package:pointycastle/asymmetric/api.dart';
voidmain() {
final publicKey = await parseKeyFromFile<RSAPublicKey>('test/public.pem');
final privKey = await parseKeyFromFile<RSAPrivateKey>('test/private.pem');
final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
final encrypter = Encrypter(RSA(publicKey: publicKey, privateKey: privKey));
final encrypted = encrypter.encrypt(plainText);
final decrypted = encrypter.decrypt(encrypted);
print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elitprint(encrypted.base64); // kO9EbgbrSwiq0EYz0aBdljHSC/rci2854Qa+nugbhKjidlezNplsEqOxR+pr1RtICZGAtv0YGevJBaRaHS17eHuj7GXo1CM3PR6pjGxrorcwR5Q7/bVEePESsimMbhHWF+AkDIX4v0CwKx9lgaTBgC8/yJKiLmQkyDCj64J3JSE=
}
Signature and verification
RSA
final publicKey = await parseKeyFromFile<RSAPublicKey>('test/public.pem');
final privateKey = await parseKeyFromFile<RSAPrivateKey>('test/private.pem');
final signer = Signer(RSASigner(RSASignDigest.SHA256, publicKey: publicKey, privateKey: privateKey));
print(signer.sign('hello world').base64);
print(signer.verify64('hello world', 'jfMhNM2v6hauQr6w3ji0xNOxGInHbeIH3DHlpf2W3vmSMyAuwGHG0KLcunggG4XtZrZPAib7oHaKEAdkHaSIGXAtEqaAvocq138oJ7BEznA4KVYuMcW9c8bRy5E4tUpikTpoO+okHdHr5YLc9y908CAQBVsfhbt0W9NClvDWegs='));