static byte[] HexToByteArray(string hex)
{
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return bytes;
}
byte[] data = HexToByteArray("004FB8A54E37D642BAB9E26A10242F0D02000000DB7B7425C4EFA00A50FCE2BF38445B5E25ED086E47304A56DD2B3DC85E281291");
using (AesCryptoServiceProvider csp = new AesCryptoServiceProvider())
{
csp.KeySize = 256;
csp.BlockSize = 128;
csp.Key = new byte[32] {
// 1 9 1 2 7 3 5 5
0x31, 0x39, 0x31, 0x32, 0x37, 0x33, 0x35, 0x35,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
csp.Padding = PaddingMode.PKCS7;
csp.Mode = CipherMode.CBC;
byte[] iv = new byte[16];
Array.Copy(data, iv, 16);
csp.IV = iv;
byte[] decrypted = csp.CreateDecryptor().TransformFinalBlock(data, 16, data.Length - 16);
string plaintext = Encoding.UTF8.GetString(decrypted);
MessageBox.Show(plaintext);
Console.WriteLine(plaintext);
}
这里是SQL加密:
create master key encryption by
password = '19127355';
--tao certificate
create certificate myCert
with subject = 'myCert';
create symmetric key PriKey
with algorithm = AES_256
encryption by certificate myCert;
create procedure SP_INS_NHANVIEN
(
@MANV varchar(20),
@HOTEN nvarchar(100),
@EMAIL varchar(20) = null,
@LUONG int,
@TENDN nvarchar(100),
@MATKHAU varchar(20))
as
begin
declare @encP2 varbinary(max);
declare @encL varbinary(max);
open symmetric key PriKey
decryption by certificate myCert
set @encP2 = convert(varbinary, hashbytes('SHA1', @MATKHAU));
set @encL = encryptbykey(key_guid('PriKey'), convert(varbinary, @LUONG))
insert into NHANVIEN
values (@MANV, @HOTEN, @EMAIL, @encL, @TENDN, @encP2);
end
我想解密上面的值(AES256)。密钥是 19127355。但是在 SQL Server 中加密时,它只有 52 个字节,所以我无法在 C# 中解密。 (我仍然可以通过查询命令在 SQL Server 中解密它)。我能做些什么来解决这个问题。 (解密结果为3000000)。帮帮我