2010-12-14 106 views
6

我的要求是我需要C#中的簡單加密/解密方法進行加密,並且 解密圖像(可能是gif/jpeg)。簡單原因我必須將其存儲在數據庫中在BLOB字段中,其他一些編程語言(如java)中的其他開發人員可能需要提取並顯示此圖像。我不需要太多的安全性,因爲它只是一個「隱藏」(生命)安全問題。加密圖像文件的簡單加密/解密方法

Gulp..can有人幫助...

+0

你碰巧使用MS SQL 2005或更高?你可以加密一個列,如果你想要走這條路...... http://msdn.microsoft.com/en-us/library/ms179331(v=SQL.90).aspx – 2010-12-14 11:44:44

+0

沒有...... ............... – abmv 2010-12-14 11:47:21

回答

7

既然你「不需要太多的安全性」,你或許可以設法通過諸如AES (Rijndael)之類的東西。它使用對稱密鑰,並且在.NET框架中提供了很多幫助,使其易於實現。 MSDN on the Rijndael class有很多信息可能對您有所幫助。

這裏是加密的一個非常精簡的例子/解密,可用於使用字節數組(二進制內容)的工作方法......

using System; 
using System.IO; 
using System.Text; 
using System.Security.Cryptography; 

public class RijndaelHelper 
{ 
    // Example usage: EncryptBytes(someFileBytes, "SensitivePhrase", "SodiumChloride"); 
    public static byte[] EncryptBytes(byte[] inputBytes, string passPhrase, string saltValue) 
    { 
     RijndaelManaged RijndaelCipher = new RijndaelManaged(); 

     RijndaelCipher.Mode = CipherMode.CBC; 
     byte[] salt = Encoding.ASCII.GetBytes(saltValue); 
     PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2); 

     ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(password.GetBytes(32), password.GetBytes(16)); 

     MemoryStream memoryStream = new MemoryStream(); 
     CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write); 
     cryptoStream.Write(inputBytes, 0, inputBytes.Length); 
     cryptoStream.FlushFinalBlock(); 
     byte[] CipherBytes = memoryStream.ToArray(); 

     memoryStream.Close(); 
     cryptoStream.Close(); 

     return CipherBytes; 
    } 

    // Example usage: DecryptBytes(encryptedBytes, "SensitivePhrase", "SodiumChloride"); 
    public static byte[] DecryptBytes(byte[] encryptedBytes, string passPhrase, string saltValue) 
    { 
     RijndaelManaged RijndaelCipher = new RijndaelManaged(); 

     RijndaelCipher.Mode = CipherMode.CBC; 
     byte[] salt = Encoding.ASCII.GetBytes(saltValue); 
     PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2); 

     ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(password.GetBytes(32), password.GetBytes(16)); 

     MemoryStream memoryStream = new MemoryStream(encryptedBytes); 
     CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read); 
     byte[] plainBytes = new byte[encryptedBytes.Length]; 

     int DecryptedCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length); 

     memoryStream.Close(); 
     cryptoStream.Close(); 

     return plainBytes; 
    } 
} 
+0

此加密不會更改圖像文件格式嗎? – Mani 2014-03-28 05:09:28

+0

@Mani不,它不。加密擾亂文件的內容(不論其類型)。解密(使用適當的用於加密的免費方法以及相關的密鑰)簡單地反轉加密的數據以恢復原始文件內容,文件類型不會改變。 – 2015-11-16 18:29:28

1

看那System.Security.Cryptography命名空間。

這是關於encrypting data的文章。

你的問題是非常普遍的 - 鏈接的文章給你使用內置函數進行加密的例子。

+5

我可以一整天看! – abmv 2010-12-14 11:41:27

+0

@abmv - http://msdn.microsoft.com/en-us/library/as0w18af.aspx – Oded 2010-12-14 11:42:28