php 如何从 flutter 中解密 aes 加密
how to descrypt an aes encryption from flutter in php
在我的 flutter 应用程序中,我使用了一个名为 encrypt 的加密包。并且能够使用此代码加密我的聊天消息
import 'package:encrypt/encrypt.dart';
class TextEncrypt {
final _iv = IV.fromLength(16);
final key = Key.fromLength(32);
String decrypt(String text) {
final enc = Encrypter(AES(key));
final encrypted = Encrypted.fromBase64(text);
return enc.decrypt(encrypted, iv: this._iv);
}
String encrypt(String text) {
final enc = Encrypter(AES(key));
return enc.encrypt(text, iv: _iv).base64;
}
}
但问题是加密的部分将存储在 mysql 数据库中,并且也会在本地数据库 (sqflite) 中运行,当我想使用 [=24] 为应用程序构建网站时=] mysql html css javascript。我将如何使用 php 将其解密回其正常文本,或者您认为可能有更好的方法在应用程序中加密它并且还能够在 php 中解密它从这个方法
如果您不明白我在说什么,需要更多解释,请告诉我
这是可以帮助您的示例:
颤振代码:
import 'package:encrypt/encrypt.dart';
String decrypt(String encrypted) {
final key = Key.fromUtf8("1245714587458888"); //hardcode combination of 16 character
final iv = IV.fromUtf8("e16ce888a20dadb8"); //hardcode combination of 16 character
final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
Encrypted enBase64 = Encrypted.from64(encrypted);
final decrypted = encrypter.decrypt(enBase64, iv: iv);
return decrypted;
}
String encrypt(String value) {
final key = Key.fromUtf8("1245714587458745"); //hardcode
final iv = IV.fromUtf8("e16ce888a20dadb8"); //hardcode
final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
final encrypted = encrypter.encrypt(value, iv: iv);
return encrypted.base64;
}
和 PHP 代码:(启用 openssl.dll 扩展)
function encrypt($value){
$key = '1245714587458888'; //combination of 16 character
$iv = 'e16ce888a20dadb8'; //combination of 16 character
$method = 'aes-128-cbc';
$encryptedString = openssl_encrypt($value, $method,
$key, OPENSSL_RAW_DATA, $iv);
return base64_encode($encryptedString);
}
function decrypt($value){
$key = '1245714587458888'; //combination of 16 character
$iv = 'e16ce888a20dadb8'; //combination of 16 character
$method = 'aes-128-cbc';
$base64 = base64_decode($value);
$decryptedString = openssl_decrypt($base64, $method,
$key, OPENSSL_RAW_DATA, $iv);
return $decryptedString;
}
在我的 flutter 应用程序中,我使用了一个名为 encrypt 的加密包。并且能够使用此代码加密我的聊天消息
import 'package:encrypt/encrypt.dart';
class TextEncrypt {
final _iv = IV.fromLength(16);
final key = Key.fromLength(32);
String decrypt(String text) {
final enc = Encrypter(AES(key));
final encrypted = Encrypted.fromBase64(text);
return enc.decrypt(encrypted, iv: this._iv);
}
String encrypt(String text) {
final enc = Encrypter(AES(key));
return enc.encrypt(text, iv: _iv).base64;
}
}
但问题是加密的部分将存储在 mysql 数据库中,并且也会在本地数据库 (sqflite) 中运行,当我想使用 [=24] 为应用程序构建网站时=] mysql html css javascript。我将如何使用 php 将其解密回其正常文本,或者您认为可能有更好的方法在应用程序中加密它并且还能够在 php 中解密它从这个方法
如果您不明白我在说什么,需要更多解释,请告诉我
这是可以帮助您的示例:
颤振代码:
import 'package:encrypt/encrypt.dart';
String decrypt(String encrypted) {
final key = Key.fromUtf8("1245714587458888"); //hardcode combination of 16 character
final iv = IV.fromUtf8("e16ce888a20dadb8"); //hardcode combination of 16 character
final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
Encrypted enBase64 = Encrypted.from64(encrypted);
final decrypted = encrypter.decrypt(enBase64, iv: iv);
return decrypted;
}
String encrypt(String value) {
final key = Key.fromUtf8("1245714587458745"); //hardcode
final iv = IV.fromUtf8("e16ce888a20dadb8"); //hardcode
final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
final encrypted = encrypter.encrypt(value, iv: iv);
return encrypted.base64;
}
和 PHP 代码:(启用 openssl.dll 扩展)
function encrypt($value){
$key = '1245714587458888'; //combination of 16 character
$iv = 'e16ce888a20dadb8'; //combination of 16 character
$method = 'aes-128-cbc';
$encryptedString = openssl_encrypt($value, $method,
$key, OPENSSL_RAW_DATA, $iv);
return base64_encode($encryptedString);
}
function decrypt($value){
$key = '1245714587458888'; //combination of 16 character
$iv = 'e16ce888a20dadb8'; //combination of 16 character
$method = 'aes-128-cbc';
$base64 = base64_decode($value);
$decryptedString = openssl_decrypt($base64, $method,
$key, OPENSSL_RAW_DATA, $iv);
return $decryptedString;
}