2016-08-30 45 views
1
public string EncryptPwd(String strString) 
     { 
      int intAscii; 
      string strEncryPwd = ""; 
      for (int intIndex = 0; intIndex < strString.ToString().Length; intIndex++) 
      { 
       intAscii = (int)char.Parse(strString.ToString().Substring(intIndex, 1)); 
       intAscii = intAscii + 5; 
       strEncryPwd += (char)intAscii; 
      } 
      return strEncryPwd; 
     } 

這是我的code.Please建議我這是正確的方式或不。如何在asp.net c中加密字符串#

+0

建議您使用'MD5','AES'或其他標準加密,而不是自定義加密。 –

+0

使用http://securitydriven.net/inferno/#Encryption它有一個nuget包。 Inferno爲妥善保護數據做了所有適當的事情,大多數例子都顯示了弱和有缺陷的保護配置。 –

回答

1

你有辦法解密嗎?這裏是SO

private static readonly UTF8Encoding Encoder = new UTF8Encoding(); 

public static string Encrypt(string unencrypted) 
{ 
    if (string.IsNullOrEmpty(unencrypted)) 
     return string.Empty; 

    try 
    { 
     var encryptedBytes = MachineKey.Protect(Encoder.GetBytes(unencrypted)); 

     if (encryptedBytes != null && encryptedBytes.Length > 0) 
      return HttpServerUtility.UrlTokenEncode(encryptedBytes);  
    } 
    catch (Exception) 
    { 
     return string.Empty; 
    } 

    return string.Empty; 
} 

public static string Decrypt(string encrypted) 
{ 
    if (string.IsNullOrEmpty(encrypted)) 
     return string.Empty; 

    try 
    { 
     var bytes = HttpServerUtility.UrlTokenDecode(encrypted); 
     if (bytes != null && bytes.Length > 0) 
     { 
      var decryptedBytes = MachineKey.Unprotect(bytes); 
      if(decryptedBytes != null && decryptedBytes.Length > 0) 
       return Encoder.GetString(decryptedBytes); 
     } 

    } 
    catch (Exception) 
    { 
     return string.Empty; 
    } 

    return string.Empty; 
} 
+0

感謝我所做的一切。 –

1

另一個答案您可以使用此代碼:

加密字符串密碼:

public static string EncryptString(string Message, string Passphrase) 
     { 
      byte[] Results; 
      System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding(); 

      MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider(); 
      byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));   
      TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();   
      TDESAlgorithm.Key = TDESKey; 
      TDESAlgorithm.Mode = CipherMode.ECB; 
      TDESAlgorithm.Padding = PaddingMode.PKCS7;   
      byte[] DataToEncrypt = UTF8.GetBytes(Message);  
      try 
      { 
       ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor(); 
       Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length); 
      } 
      finally 
      { 
       TDESAlgorithm.Clear(); 
       HashProvider.Clear(); 
      }   
      return Convert.ToBase64String(Results); 
     } 

和解密字符串密碼:

public static string DecryptString(string Message, string Passphrase) 
     { 
      byte[] Results; 
      System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding(); 

      MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider(); 
      byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase)); 

      TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider(); 

      TDESAlgorithm.Key = TDESKey; 
      TDESAlgorithm.Mode = CipherMode.ECB; 
      TDESAlgorithm.Padding = PaddingMode.PKCS7; 

      byte[] DataToDecrypt = Convert.FromBase64String(Message); 
      // Step 5. Bat dau giai ma chuoi 
      try 
      { 
       ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor(); 
       Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length); 
      } 
      catch (Exception) { Results = DataToDecrypt; } 
      finally 
      {     
       TDESAlgorithm.Clear(); 
       HashProvider.Clear(); 
      } 

      return UTF8.GetString(Results); 
     } 
+0

請勿使用3DES! –

2

我同意與所有以前的答案(特別是share pvv),但在情況下y OU真的想要一個Caesar Cipher(愷撒密碼是非常弱加密,不應在生產中使用),如果這是一個學習鍛鍊,那麼這裏是一些代碼

string strString = "abcdefghijklmnopqrstuvwxyz"; 
string enc = String.Join("", strString.ToArray().Select(x =>(char)('a' + (x-'a'+5)%26))); 
Console.WriteLine(enc); 

或使用for循環像你一樣

string strEncryPwd = ""; 
for (int index = 0; index < strString.ToString().Length; index++) 
{ 
     int plainChar = strString[index]; 
     int encrypted = 'a' + (plainChar - 'a' + 5) % 26; 
     strEncryPwd += (char)encrypted; 
    } 
     Console.WriteLine(strEncryPwd);