c# - SQL Server 上的 AES 解密
问题描述
我在我的 MSSMS 12 上创建了以下加密:
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Password123';
CREATE CERTIFICATE Certificate1
WITH SUBJECT = 'Protect Data';
CREATE SYMMETRIC KEY SymmetricKey1
WITH ALGORITHM = AES_128
ENCRYPTION BY CERTIFICATE Certificate1;
UPDATE Customer_data
SET Credit_card_number_encrypt = EncryptByKey (Key_GUID('SymmetricKey1'),Credit_card_number)
如何解密来自我的数据库的值,它是public byte[] Credit_card_number_encrypt
一个字符串?
这是我尝试的代码:
private string DecryptString(byte[] inputString)
{
MemoryStream memStream = null;
try
{
byte[] key = { };
byte[] IV = { 12, 21, 43, 17, 57, 35, 67, 27 };
string encryptKey = "Password123"; // MUST be 8 characters
key = Encoding.UTF8.GetBytes(encryptKey);
byte[] byteInput = new byte[inputString.Length];
byteInput = Convert.FromBase64String(inputString);
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
memStream = new MemoryStream();
ICryptoTransform transform = provider.CreateDecryptor(key, IV);
CryptoStream cryptoStream = new CryptoStream(memStream, transform, CryptoStreamMode.Write);
cryptoStream.Write(byteInput, 0, byteInput.Length);
cryptoStream.FlushFinalBlock();
Encoding encoding1 = Encoding.UTF8;
return encoding1.GetString(memStream.ToArray());
}
catch (Exception ex)
{
Console.Write(ex.Message);
return "";
}
}
问题是Convert.FromBase64String(inputString)
无法将数据类型转换byte[]
为string
. 我该怎么做呢?
解决方案
推荐阅读
- sql - 如何在 SQL Server 中的特定值之后选择下一个值?
- wordpress - wordpress 联系表 7 问题 - 如果我们有这么多联系表,如何跟踪来自哪个联系表的潜在客户
- node.js - 在 Node.js (Express) 身份验证控制器和 Node 邮件程序中将标头发送到客户端后无法设置标头
- multithreading - 如何在 Xamarin 表单的 ContentPage 上使用 RunOnUiThread
- google-bigquery - Google BigQuery 总是用逗号分隔列
- typescript - 在 mixin 函数中使用现有类(js 构造函数)
- python - 在 python boto3 中使用 AWS cognito Identity 和 openID salesforce
- c# - Visual Studio Mac 上的超出范围异常,但在 vs Windows 上没有 - 两者都是 2017
- c# - .NET 自定义语言注册
- javascript - 三.js HTML 背景为 clearColor