0

他正在研究如何在我的应用程序中实现加密算法。他见过RC2。

一方面,我设法从 c# 加密和解密。

另一方面,我还设法从 SQL Server 加密和解密。

但是从 SQL Server 加密的内容,我无法从 C# 解密它,而且当你从 C# 加密时,我也无法从 SQL Server 解密。

当然加密密钥和初始向量是相同的。

我猜想使 SQL 服务器保持数据加密的原因不仅仅是二进制加密,而是增加了一些东西。


编辑:C#代码:

public static byte[] Cifrar(string dato, string clave, string vectorInicial)
    {
        byte[] plainText = Encoding.ASCII.GetBytes(dato);
        byte[] keys = Encoding.ASCII.GetBytes(clave);
        byte[] vecI = Encoding.ASCII.GetBytes(vectorInicial);
        MemoryStream memdata = new MemoryStream();


        RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
        ICryptoTransform transform;
        rc2.Mode = CipherMode.CBC;
        transform = rc2.CreateEncryptor(keys, vecI);

        CryptoStream encStream = new CryptoStream(memdata,transform,CryptoStreamMode.Write);


        encStream.Write(plainText, 0, plainText.Length);
        encStream.Close();

        return memdata.ToArray();
    }

    public static string Descrifrar(byte[] dato,string clave, string vectorInicial)
    {
        byte[] keys = Encoding.ASCII.GetBytes(clave);
        byte[] vecI = Encoding.ASCII.GetBytes(vectorInicial);
         MemoryStream memdata = new MemoryStream();

        RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
        ICryptoTransform transform;
        rc2.Mode = CipherMode.CBC;
        transform = rc2.CreateDecryptor(keys, vecI);

        CryptoStream encStream = new CryptoStream(memdata,transform,CryptoStreamMode.Write);


        encStream.Write(dato, 0, dato.Length);
        encStream.Close();

        return Encoding.ASCII.GetString(memdata.ToArray());

    }

对于加密我使用:

CifradoRC2.Cifrar("Perico", "sdsdfdsf323", "huihuihuih123");

T-SQL 代码:

CREATE SYMMETRIC KEY NuevaClaveDeCifrado WITH IDENTITY_VALUE = 'huihuihuih123',ALGORITHM = RC2,KEY_SOURCE = 'sdsdfdsf323' ENCRYPTION BY Password = 'dsjdkflJ435907NnmM#sX003';

CREATE PROCEDURE OpenSqlKeys
AS
BEGIN    
     SET NOCOUNT ON;
     BEGIN TRY
             OPEN SYMMETRIC KEY NuevaClaveDeCifrado DECRYPTION BY PASSWORD ='dsjdkflJ435907NnmM#sX003';
     END TRY
     BEGIN CATCH
             -- Handle non-existant key here
     END CATCH
END
GO

CREATE FUNCTION Encrypt(@ValueToEncrypt varchar(max))
RETURNS varbinary(256)
AS
BEGIN    
-- Declare the return variable here    
DECLARE @Result varbinary(256)  
SET @Result = EncryptByKey(Key_GUID('NuevaClaveDeCifrado'), @ValueToEncrypt)     
-- Return the result of the function    
RETURN @Result
END
GO

CREATE FUNCTION Decrypt(@ValueToDecrypt varbinary(256))
RETURNS varchar(max)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @Result varchar(max)
    SET @Result = DecryptByKey(@ValueToDecrypt)
    -- Return the result of the function
    RETURN @Result
END

加密“Perico”T-SQL的二进制结果:0x009C6828879054489426381EFD2E3FD501000000266B3AF2ED7029C1F567AF8119C5D458FCDE1E984B2262A0

C#:

0x9FC66B77905837F5

谢谢!

4

1 回答 1

1

我认为 SQL 和 RC2CryptoServiceProvider 输出不同的原因是因为您为在 SQL 中创建的密钥指定了密码。这就是为什么您的 2 个结果之间存在很大差异的原因。我建议你使用 CLR dll 和 C# 代码来做你想做的事。

这是一个例子。我用你的代码来创建函数

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Text;
using System.Security.Cryptography;

public partial class UserDefinedFunctions
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlBinary Cifrar(string dato, string clave, string vectorInicial)
    {
        byte[] plainText = Encoding.ASCII.GetBytes(dato);
        byte[] keys = Encoding.ASCII.GetBytes(clave);
        byte[] vecI = Encoding.ASCII.GetBytes(vectorInicial);
        MemoryStream memdata = new MemoryStream();


        RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
        ICryptoTransform transform;
        rc2.Mode = CipherMode.CBC;
        transform = rc2.CreateEncryptor(keys, vecI);

        CryptoStream encStream = new CryptoStream(memdata, transform, CryptoStreamMode.Write);


        encStream.Write(plainText, 0, plainText.Length);
        encStream.Close();
        return new SqlBinary(memdata.ToArray());
    }

    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString Descrifrar(byte[] dato, string clave, string vectorInicial)
    {
        byte[] keys = Encoding.ASCII.GetBytes(clave);
        byte[] vecI = Encoding.ASCII.GetBytes(vectorInicial);
        MemoryStream memdata = new MemoryStream();

        RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
        ICryptoTransform transform;
        rc2.Mode = CipherMode.CBC;
        transform = rc2.CreateDecryptor(keys, vecI);

        CryptoStream encStream = new CryptoStream(memdata, transform, CryptoStreamMode.Write);


        encStream.Write(dato, 0, dato.Length);
        encStream.Close();

        return new SqlString(Encoding.ASCII.GetString(memdata.ToArray()));
    }
};

您需要创建一个 SQL CLR 数据库项目。它会将您连接到数据库。请按照步骤操作并确保您阅读了警告。现在不要去停止生产服务器。添加一个新项目,然后选择用户定义的函数。将代码粘贴到那里。编译它以确保一切正常,然后右键单击项目,然后选择部署。然后你可以测试它,看看它是如何工作的,以及它是否在做你想做的事情。

我已经在我的机器上进行了测试,它似乎可以完成这项工作。

于 2012-03-14T10:52:03.723 回答