如何在asp.net中加密字符串c#[复制](how to encrypt string in asp.net c# [duplicate])

这个问题在这里已有答案:

在C#中加密和解密字符串[复制] 7个答案 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; }

这是我的代码。请告诉我这是正确的方式。

This question already has an answer here:

Encrypting & Decrypting a String in C# [duplicate] 7 answers 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; }

This is my code.Please suggest me it is right way or not.

最满意答案

你有办法解密吗? 这是关于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; }

Do you have a way to decrypt that? Here is another answer on 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; }如何在asp.net中加密字符串c#[复制](how to encrypt string in asp.net c# [duplicate])

这个问题在这里已有答案:

在C#中加密和解密字符串[复制] 7个答案 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; }

这是我的代码。请告诉我这是正确的方式。

This question already has an answer here:

Encrypting & Decrypting a String in C# [duplicate] 7 answers 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; }

This is my code.Please suggest me it is right way or not.

最满意答案

你有办法解密吗? 这是关于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; }

Do you have a way to decrypt that? Here is another answer on 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; }