2016-07-03 86 views
0

有沒有辦法用AES加密數據?我希望至少使用128位加密密鑰。C#加密數據AES

我試着用微軟提供的AES默認類,但是我做不了很多......我無法設置Key長度。

是否有另一個類可以使用?

+1

http://yossi-yakubov.blogspot.co.id/2010/07 /aes-encryption-using-c-short-way.html –

回答

1

是的。您可以使用如下的代碼在http://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt

您可以使用RIJNDAEL

首先,你需要包括using System.Security.Cryptography;

public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes) 
    { 
     byte[] encryptedBytes = null; 

     // Set your salt here, change it to meet your flavor: 
     // The salt bytes must be at least 8 bytes. 
     byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; 

     using (MemoryStream ms = new MemoryStream()) 
     { 
      using (RijndaelManaged AES = new RijndaelManaged()) 
      { 
       AES.KeySize = 256; 
       AES.BlockSize = 128; 

       var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000); 
       AES.Key = key.GetBytes(AES.KeySize/8); 
       AES.IV = key.GetBytes(AES.BlockSize/8); 

       AES.Mode = CipherMode.CBC; 

       using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write)) 
       { 
        cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length); 
        cs.Close(); 
       } 
       encryptedBytes = ms.ToArray(); 
      } 
     } 

     return encryptedBytes; 
    } 

    public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes) 
    { 
     byte[] decryptedBytes = null; 

     // Set your salt here, change it to meet your flavor: 
     // The salt bytes must be at least 8 bytes. 
     byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; 

     using (MemoryStream ms = new MemoryStream()) 
     { 
      using (RijndaelManaged AES = new RijndaelManaged()) 
      { 
       AES.KeySize = 256; 
       AES.BlockSize = 128; 

       var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000); 
       AES.Key = key.GetBytes(AES.KeySize/8); 
       AES.IV = key.GetBytes(AES.BlockSize/8); 

       AES.Mode = CipherMode.CBC; 

       using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write)) 
       { 
        cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length); 
        cs.Close(); 
       } 
       decryptedBytes = ms.ToArray(); 
      } 
     } 

     return decryptedBytes; 
    } 


public void EncryptFile(string filename, string outfilename, string psw) 
     { 
      string file = filename; 
      string password = psw; 

      byte[] bytesToBeEncrypted = File.ReadAllBytes(file); //read bytes to encrypt them 
      byte[] passwordBytes = Encoding.UTF8.GetBytes(password); //read with UTF8 encoding the password. 
      passwordBytes = SHA256.Create().ComputeHash(passwordBytes); //hash the psw 

      byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes); 

      string fileEncrypted = outfilename; 

      File.WriteAllBytes(fileEncrypted, bytesEncrypted); 
     } 
     public void DecryptFile(string filename, string outfilename, string psw) 
     { 
      string fileEncrypted = filename; 
      string password = psw; 

      byte[] bytesToBeDecrypted = File.ReadAllBytes(fileEncrypted); 
      byte[] passwordBytes = Encoding.UTF8.GetBytes(password); 
      passwordBytes = SHA256.Create().ComputeHash(passwordBytes); 

      byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes); 

      string file = outfilename; 
      File.WriteAllBytes(file, bytesDecrypted); 
     } 
+1

重要的是要注意'AES.BlockSize = 128'是關鍵,這就是'Rijndael'AES的原因。 – zaph

+0

@zaph是的!謝謝...你對代碼有什麼看法?可以嗎? –

+0

該方案看起來不錯,關鍵是正確的來自密碼,CBC模式是好的,有一個關於填充的假設,如果它在代碼中是明確的,我會更快樂。沒有檢查是否使用了正確的解密密鑰,但許多人不需要這樣做,如果您需要添加加密認證。我不知道C#所以不能對此發表評論。 – zaph