This question already has answers here:
Encrypting & Decrypting a String in C# [duplicate]
                                
                                    (7个答案)
                                
                        
                                3年前关闭。
            
                    
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;
        }


这是我的代码。请建议我这是正确的方法。

最佳答案

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

关于c# - 如何在asp.net C#中加密字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39226751/

10-09 03:47