我有一个使用 Hmac 和 SHA256 的密钥对双精度数进行加密并返回一个字符串以将其存储在数据库中的类。
String encryptDoubleToString(double valueDouble) {
print("valueDouble: $valueDouble"); // -1.8565915524959564
String valueString = valueDouble.toString();
List<int> encodedValueBytes = utf8.encode(valueString);
print("encodedValueBytes: $encodedValueBytes"); // [45, 49, 46, 56, 53, 54, 53, 57, 49, 53, 53, 50, 52, 57, 53, 57, 53, 54, 52]
List<int> key = getKey();
List<int> encryptedBytes = Hmac(sha256, key).convert(encodedValueBytes).bytes;
print("encryptedBytes: $encryptedBytes"); // [140, 80, 17, 114, 38, 219, 217, 165, 254, 184, 212, 164, 120, 98, 166, 35, 122, 246, 22, 32, 187, 239, 7, 61, 252, 207, 195, 23, 220, 168, 105, 93]
String valueBytesString = encryptedBytes.toString();
return valueBytesString;
}
然后我有第二个类解密并返回 Double 类型
double decryptStringToDouble(String encryptedValueBytesString) {
print("encryptedValueBytesString: $encryptedValueBytesString"); // [140, 80, 17, 114, 38, 219, 217, 165, 254, 184, 212, 164, 120, 98, 166, 35, 122, 246, 22, 32, 187, 239, 7, 61, 252, 207, 195, 23, 220, 168, 105, 93]
List<int> decodedValueBytes = json.decode(encryptedValueBytesString).cast<int>();
print("decodedValueBytes: $decodedValueBytes"); // [140, 80, 17, 114, 38, 219, 217, 165, 254, 184, 212, 164, 120, 98, 166, 35, 122, 246, 22, 32, 187, 239, 7, 61, 252, 207, 195, 23, 220, 168, 105, 93]
List<int> key = getKey();
List<int> decryptedBytes = Hmac(sha256, key).convert(decodedValueBytes).bytes;
print("decryptedBytes: $decryptedBytes"); // Wrong [78, 193, 67, 74, 97, 235, 187, 66, 39, 23, 211, 216, 7, 27, 20, 80, 139, 29, 19, 213, 51, 192, 113, 33, 185, 121, 177, 228, 31, 145, 104, 176]
String decryptedString = utf8.decode(decryptedBytes); // FAILED
double decryptedValue = double.parse(decryptedString);
return decryptedValue;
}
我想我在解密时犯了一个错误,但我不知道是什么,因为目前解密的值不等于加密的值。
你能帮帮我吗?
谢谢
我想要
decryptedBytes
= encodedValueBytes
.