2011-01-20 150 views
3

我想使用一種算法將C#中的字符串加密和解密,這種算法會導致我使用相同的加密字符串。例如,如果我加密字符串122ujhdheiwe,其結果是uoi8asdf8asdf,如果我再次加密相同的字符串122ujhdheiwe它使我uoi8asdf8asdf字符串。我可以使用哪些加密算法以及如何使用?如何在C#中對字符串進行加密和解密#

+0

以及(如何)你要解密,你想用什麼關鍵(和鹽),它應該有多安全? – 2011-01-20 09:11:58

+0

在我的情況下安全不是一個問題,我只是不想讓用戶查看即時保存在隱藏域中的內容 – 2011-01-20 09:25:52

回答

1

rot13(凱撒)也許?它將所有字符移動偏移量13.因此,應用兩次,您將再次獲得純文本。

1

我有一個簡單的解決方案在這裏:

http://remy.supertext.ch/2011/01/simple-c-encryption-and-decryption/

Bascially它的工作原理是這樣的:

DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
byte[] Key = { 12, 13, 14, 15, 16, 17, 18, 19 }; 
byte[] IV = { 12, 13, 14, 15, 16, 17, 18, 19 }; 

ICryptoTransform encryptor = des.CreateEncryptor(Key, IV); 

byte[] IDToBytes = ASCIIEncoding.ASCII.GetBytes(source); 
byte[] encryptedID = encryptor.TransformFinalBlock(IDToBytes, 0, IDToBytes.Length); 
return Convert.ToBase64String(encryptedID); 

然後反過來。

1

可以使用ProtectedData類,如果你想有一個簡單的解決方案:

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

private void example() 
{ 
    string data = "122ujhdheiwe"; 

    // Encrypt 
    UnicodeEncoding unicodeEncoding = new UnicodeEncoding(); 
    byte[] secret = ProtectedData.Protect(unicodeEncoding.GetBytes(data), null, DataProtectionScope.CurrentUser); 
    Console.WriteLine(BitConverter.ToString(secret)); 

    // If you need it as a printable string, you can convert the binary to Base64 
    string base64 = Convert.ToBase64String(secret); 
    Console.WriteLine(base64); 

    // Back to binary... 
    byte[] backagain = Convert.FromBase64String(base64); 

    // Decrypt 
    byte[] clearbytes = ProtectedData.Unprotect(backagain, null, DataProtectionScope.CurrentUser); 
    string roundtripped = unicodeEncoding.GetString(clearbytes); 
    Console.WriteLine(roundtripped); 
} 

比照ProtectedDataClass

如果你想加密的數據看起來非常像你的原始數據,在本例中您的問題(122ujhdheiwe ==> uoi8asdf8asdf),那麼你正在尋找的格式保留加密 - cf. here,爲此我沒有一個例子。

編輯:我剛剛注意到,在你的問題中,你希望能夠再次加密相同的字符串並獲得相同的加密結果,在這種情況下,ProtectedData將無法工作,因爲加密中使用的密鑰將會隨着時間的推移而變化。

0

enter image description here

我的代碼演示程序確實需要源? 我使用此命名空間using System.Security.Cryptography;

兩本

 public string encryptus(string x, string encrypt)//function 
    { 
     try 
     { 

      string y = x; 
      byte[] etext = UTF8Encoding.UTF8.GetBytes(y); 
      string Code = encrypt; 
      MD5CryptoServiceProvider mdhash = new MD5CryptoServiceProvider(); 
      byte[] keyarray = mdhash.ComputeHash(UTF8Encoding.UTF8.GetBytes(Code)); 
      TripleDESCryptoServiceProvider tds = new TripleDESCryptoServiceProvider(); 
      tds.Key = keyarray; 
      tds.Mode = CipherMode.ECB; 
      tds.Padding = PaddingMode.PKCS7; 

      ICryptoTransform itransform = tds.CreateEncryptor(); 
      byte[] result = itransform.TransformFinalBlock(etext, 0, etext.Length); 
      string encryptresult = Convert.ToBase64String(result); 
      return encryptresult.ToString(); 
     } 
     catch (Exception ex) 
     { 
      return ex.Message; 
     } 
    } 
     public string dencryptus(string x, string keyai) 
    { 
     try 
     { 
      string y = x.Replace("\0", null); 
      byte[] etext = Convert.FromBase64String(y); 
      string key = keyai; 
      MD5CryptoServiceProvider mdhash = new MD5CryptoServiceProvider(); 
      byte[] keyarray = mdhash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); 
      TripleDESCryptoServiceProvider tds = new TripleDESCryptoServiceProvider(); 
      tds.Key = keyarray; 
      tds.Mode = CipherMode.ECB; 
      tds.Padding = PaddingMode.PKCS7; 

      ICryptoTransform itransform = tds.CreateDecryptor(); 
      byte[] result = itransform.TransformFinalBlock(etext, 0, etext.Length); 
      string dencryptresult = UTF8Encoding.UTF8.GetString(result); 
      return dencryptresult.ToString(); 
     } 
     catch (Exception ex) 
     { 
      return ex.Message; 
     } 
    } 

,並利用它們是這樣的:

Encrypted.Text = encryptus(Message.Text, EncryptCode.Text.ToString()); 
Decrypted.Text = dencryptus(Message.Text, EncryptCode.Text.ToString()); 
相關問題