使用常见最佳实践的跨平台字符串加密

flutter_string_encryption

使用常见最佳实践的跨平台字符串加密 (AES/CBC/PKCS5/Random IVs/HMAC-SHA256 完整性检查)。

它使用本文中所述的格式。

它目前使用本机平台实现,这些实现(我们都希望) 苹果和谷歌不断审查和更新,其中一些非常小 库包装器,以减轻一些繁琐的工作。

对于 Android 方面,我使用了以下库(来自相同的 以上文章的作者):https://github.com/tozny/java-aes-crypto

对于iOS端,我实现了文章中描述的格式 直接在原生插件内部,并使用以下库 帮助我使用Apple的CommonCrypto功能,这些功能并不容易 交互方式:https://github.com/sgl0v/SCrypto

支持

为了在iOS上工作,您需要将iOS支持版本提升到” 9.0. 这可以通过取消注释在您的 ios 项目 Podfile 中完成 文件的第一行:

# Uncomment this line to define a global platform for your project
platform :ios, '9.0'

用法

final cryptor = new PlatformStringCryptor();

生成密钥

随机

生成它并将其存储在安全的地方 - 例如钥匙串/钥匙库

final String key = await cryptor.generateRandomKey();

基于密码

生成并(安全)存储盐,然后使用用户提供的密钥生成密钥 加密/解密字符串之前的密码。

final password = "user_provided_password";
final String salt = await cryptor.generateSalt();
final String key = await cryptor.generateKeyFromPassword(password, salt);

Encrypt A String

final String encrypted = await cryptor.encrypt("A string to encrypt.", key);

Decrypt A String

try {
  final String decrypted = await cryptor.decrypt(encrypted, key);
  print(decrypted); // - A string to encrypt.
} on MacMismatchException {
  // unable to decrypt (wrong key or forged data)
}

License

MIT (both this plugin and the used helper libraries).

GitHub

点击跳转