首页 > 解决方案 > 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. 我该怎么做呢?

标签: c#sqlsql-serverencryptionado.net

解决方案


推荐阅读