2011-10-27 63 views
0

我想用C#創建一個ASP.Net應用程序,我將在SQL Server 2005上存儲數據,這些數據將被加密我想找到一種算法來加密數據與C#和解密它在SQL服務端,我想用SQL加密一些數據,並用C#解密它什麼是最好的算法?使用C#和SQL Server加密和解密數據

private byte[] key = { 
    0x61, 
    0x72, 
    0x84, 
    0x7a, 
    0x24, 
    0x43, 
    0x65, 
    0x64, 
    0x73, 
    0x55, 
    0x64, 
    0x75, 
    0x66 

}; 



const string PASSWORD = "TestPassword"; 
public object Encrypt(string sPlainText) 
{ 



    byte[] aPlainBytes = null; 

    PasswordDeriveBytes aPassword = default(PasswordDeriveBytes); 



    aPlainBytes = System.Text.Encoding.Unicode.GetBytes(sPlainText); 

    aPassword = new PasswordDeriveBytes(PASSWORD, key); 

    byte[] sEncryptedData = Encrypt(aPlainBytes, aPassword.GetBytes(32), aPassword.GetBytes(16)); 

    //' MessageBox.Show(Convert.ToString(sEncryptedData.ToString)) 

    return Convert.ToBase64String(sEncryptedData); 



} 



private byte[] Encrypt(byte[] sPlainData, byte[] aKey, byte[] aIV) 
{ 



    MemoryStream oMemoryStream = new MemoryStream(); 



    Rijndael oRijndael = Rijndael.Create(); 

    oRijndael.Key = aKey; 



    oRijndael.IV = aIV; 



    CryptoStream oCryptoStream = new CryptoStream(oMemoryStream, oRijndael.CreateEncryptor(), CryptoStreamMode.Write); 

    oCryptoStream.Write(sPlainData, 0, sPlainData.Length); 

    oCryptoStream.Close(); 

    byte[] aEncryptedData = oMemoryStream.ToArray(); 




    return aEncryptedData; 



} 
+0

您計劃在哪裏隱藏密鑰? –

回答

5

C#:System.Security.Cryptography

SQL服務器:Sql Server Encryption

C#實施例here

private static void EncryptData(String inName, String outName, byte[] tdesKey, byte[] tdesIV) 
{  
    //Create the file streams to handle the input and output files. 
    FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read); 
    FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); 
    fout.SetLength(0); 

    //Create variables to help with read and write. 
    byte[] bin = new byte[100]; //This is intermediate storage for the encryption. 
    long rdlen = 0;    //This is the total number of bytes written. 
    long totlen = fin.Length; //This is the total length of the input file. 
    int len;      //This is the number of bytes to be written at a time. 

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();   
    CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(tdesKey, tdesIV), CryptoStreamMode.Write); 

    Console.WriteLine("Encrypting..."); 

    //Read from the input file, then encrypt and write to the output file. 
    while(rdlen < totlen) 
    { 
     len = fin.Read(bin, 0, 100); 
     encStream.Write(bin, 0, len); 
     rdlen = rdlen + len; 
     Console.WriteLine("{0} bytes processed", rdlen); 
    } 

    encStream.Close();      
} 

SQL Server的實例here

USE AdventureWorks2008R2; 
GO 

--If there is no master key, create one now. 
IF NOT EXISTS 
    (SELECT * FROM sys.symmetric_keys WHERE symmetric_key_id = 101) 
    CREATE MASTER KEY ENCRYPTION BY 
    PASSWORD = '23987hxJKL969#ghf0%94467GRkjg5k3fd117r$$#1946kcj$n44nhdlj' 
GO 

CREATE CERTIFICATE HumanResources037 
    WITH SUBJECT = 'Employee Social Security Numbers'; 
GO 

CREATE SYMMETRIC KEY SSN_Key_01 
    WITH ALGORITHM = AES_256 
    ENCRYPTION BY CERTIFICATE HumanResources037; 
GO 

USE [AdventureWorks2008R2]; 
GO 

-- Create a column in which to store the encrypted data. 
ALTER TABLE HumanResources.Employee 
    ADD EncryptedNationalIDNumber varbinary(128); 
GO 

-- Open the symmetric key with which to encrypt the data. 
OPEN SYMMETRIC KEY SSN_Key_01 
    DECRYPTION BY CERTIFICATE HumanResources037; 

-- Encrypt the value in column NationalIDNumber with symmetric 
-- key SSN_Key_01. Save the result in column EncryptedNationalIDNumber. 
UPDATE HumanResources.Employee 
SET EncryptedNationalIDNumber = EncryptByKey(Key_GUID('SSN_Key_01'), NationalIDNumber); 
GO 

-- Verify the encryption. 
-- First, open the symmetric key with which to decrypt the data. 
OPEN SYMMETRIC KEY SSN_Key_01 
    DECRYPTION BY CERTIFICATE HumanResources037; 
GO 

-- Now list the original ID, the encrypted ID, and the 
-- decrypted ciphertext. If the decryption worked, the original 
-- and the decrypted ID will match. 
SELECT NationalIDNumber, EncryptedNationalIDNumber 
    AS 'Encrypted ID Number', 
    CONVERT(nvarchar, DecryptByKey(EncryptedNationalIDNumber)) 
    AS 'Decrypted ID Number' 
    FROM HumanResources.Employee; 
GO 
+0

你能給我一個簡單的例子嗎? –

+0

我添加了一些鏈接的例子,但通過搜索很容易找到例子。 – CAbbott

+0

我加密一個字符串的代碼,我想SQL上的eqwivilent可以幫助我嗎?以及我可以如何發送給你? –

0

C#和SQL可以使用三重DES加密方法,不過我可能會選擇一個位置,兩者都做。除非您因爲某種原因需要進行預成型。請參閱this關於如何在SQL級別使用加密的示例