Flutter加密解密插件cryppo的使用
Flutter加密解密插件cryppo的使用
Cryppo Dart 是一个加密库,使你能够加密和解密数据。Cryppo Dart 结合了多种不同的密码算法,并提供了一个简化版的 API 和一组序列化格式。
此库是 Meeco 平台在 Ruby 和 JavaScript 中使用的 Cryppo 库的 Dart 版本。
使用派生密钥进行加密和解密
import 'package:cryppo/cryppo.dart';
Future<void> main() async {
// 使用派生密钥加密数据
final encrypted = await encryptWithDerivedKey(
data: utf8.encode('Hello World'), // 要加密的数据
encryptionStrategy: EncryptionStrategy.aes256Gcm, // 加密策略
keyDerivationStrategy: DerivationStrategy.pbkdf2Hmac, // 密钥派生策略
passphrase: 'correct horse battery staple' // 密码
);
print(encrypted.serialize());
// 输出:'Aes256Gcm.pjqdT<snip>.Pbkdf2Hmac.SzAAAA<snip>'
// 打印派生密钥的序列化信息
print(encrypted.derivationArtefacts.serialize());
// 输出:'Pbkdf2Hmac.SzAAAA<snip>'
// 使用相同的密码解密数据
final decrypted = await decryptWithKeyDerivedFromString(
serialized: encrypted.serialize(), // 已加密数据的序列化信息
passphrase: 'correct horse battery staple' // 密码
);
print(decrypted);
// 输出:'Hello World'
}
使用生成的加密密钥进行加密和解密
import 'package:cryppo/cryppo.dart';
Future<void> main() async {
// 生成加密密钥
final key = DataEncryptionKey.generate();
print(key.serialize());
// 输出:'fB3gwp8b...'
// 使用生成的密钥加密数据
final encrypted = await encryptWithKey(
data: utf8.encode('Hello World'), // 要加密的数据
key: key, // 加密密钥
encryptionStrategy: EncryptionStrategy.aes256Gcm // 加密策略
);
print(encrypted.serialize());
// 输出:'Aes256Gcm.pjqdT....'
// 使用相同的密钥解密数据
final decrypted = await decryptWithKey({
serialized: encrypted.serialize(), // 已加密数据的序列化信息
key: key // 解密密钥
});
print(utf8.decode(decrypted));
// 输出:'Hello World'
}
签名和验证
import 'package:cryppo/cryppo.dart';
Future<void> main() async {
// 生成 RSA 密钥对
final keyPair = Rsa4096().generateKeyPair();
// 使用私钥签名数据
final signature = sign({
keyPair.privateKey,
data: utf8.encode('data to sign') // 需要签名的数据
});
print(signature);
// 输出:'Sign.Rsa4096.hOQsys....'
// 使用公钥验证签名
final verified = verify({
publicKey: keyPair.publicKey,
serializedSignature: signature // 签名的序列化信息
});
print(verified);
// 输出:true
}
加密策略
Aes256Gcm
Aes256Gcm 选择是因为它提供了认证加密。如果在解密过程中使用了错误的值(如加密密钥),将引发错误。这意味着你可以确保解密的数据与原始加密的数据相同。
密钥派生策略
Pbkdf2Hmac
Pbkdf2Hmac 可以从潜在不安全的源(如用户生成的密码)生成加密安全密钥。
派生密钥是加密安全的,使得直接暴力破解加密数据变得不可行。完成操作所需的计算量可以调整,以确保暴力破解密码加密数据的难度。
示例代码
以下是完整的示例代码:
import './gcm_page.dart';
import './rsa_page.dart';
import 'package:flutter/material.dart';
void main() async {
runApp(CryppoDemoApp());
}
class CryppoDemoApp extends StatefulWidget {
[@override](/user/override)
_CryppoDemoAppState createState() => _CryppoDemoAppState();
}
class _CryppoDemoAppState extends State<CryppoDemoApp> {
[@override](/user/override)
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(new FocusNode());
},
child: MaterialApp(
title: 'Cryppo Demo',
home: BottomTabBar(),
),
);
}
}
class BottomTabBar extends StatefulWidget {
BottomTabBar();
[@override](/user/override)
_BottomTabBarState createState() => _BottomTabBarState();
}
class _BottomTabBarState extends State<BottomTabBar> {
int _selectedIndex = 0;
static const pageTitles = ['RSA', 'AES', 'File'];
static List<Widget> _widgetOptions = [
RsaPage(),
GcmPage(),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(pageTitles[_selectedIndex]),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <
BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.enhanced_encryption),
label: 'RSA',
),
BottomNavigationBarItem(
icon: Icon(Icons.lock),
label: 'AES',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
更多关于Flutter加密解密插件cryppo的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复