【问题标题】:Need an Encryption/decryption method does not have a '/' in the encrypted string [duplicate]需要加密/解密方法在加密字符串中没有“/”[重复]
【发布时间】:2013-02-13 02:18:24
【问题描述】:

我需要加密和解密字符串值,例如电子邮件地址和数值,但加密的字符串中不应包含“/”,因为我在 URL 中使用它并使用“/”作为分隔符来获取一些值。

我目前正在使用以下方法:

    string passPhrase = "Pas5pr@se";        // can be any string
    string saltValue = "s@1tValue";        // can be any string
    string hashAlgorithm = "SHA1";             // can be "MD5"
    int passwordIterations = 2;                  // can be any number
    string initVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
    int keySize = 256;                // can be 192 or 128

    public string Encrypt(string plainText)
    {            
        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);         
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
        PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase,saltValueBytes,hashAlgorithm,passwordIterations);
        byte[] keyBytes = password.GetBytes(keySize / 8);
        RijndaelManaged symmetricKey = new RijndaelManaged();
        symmetricKey.Mode = CipherMode.CBC;            
        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes,initVectorBytes);
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream,encryptor,CryptoStreamMode.Write);           
        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);            
        cryptoStream.FlushFinalBlock();
        byte[] cipherTextBytes = memoryStream.ToArray();
        memoryStream.Close();
        cryptoStream.Close();
        string cipherText = Convert.ToBase64String(cipherTextBytes);
        return cipherText;
    }

【问题讨论】:

  • 用其他字符替换 / 怎么样?
  • 如果你在 URL 中使用它,UrlEncodeUrlDecode 不工作吗?
  • 试试这个链接 1.Link1 2.Link2 谢谢Abiruban
  • @AbiRuban 在这种情况下的问题不是“如何加密/解密” - 它更多地涉及表示加密数据 在 url 中 - 这些帖子都没有解决的问题。

标签: c# asp.net


【解决方案1】:

如果你只是为了传递 URL,我建议你生成任何加密字符串(不管它是否有/),然后:

var sanitized = HttpUtility.UrlEncode(encryptedString);

如您所见,/ 变为 %2f。然后你可以简单地做:

var encryptedString = HttpUtility.UrlDecode(sanitized)

你会再次得到相同的字符串。

编辑: HttpUtilitySystem.Web 程序集中。

【讨论】:

    【解决方案2】:

    加密本身只是输出字节,而不是字符。所以这个问题与加密/解密完全无关。您的实际问题是将任意字节转换为可在 url 中使用的字符串。为此,我建议使用 URL 安全的 Base64 而不是普通的 Base64。

    那些/ 字符是由您应用于密文的Base64 编码产生的。 Base64 使用 ASCII 字母和数字(总共 62 个),加上 /+,最后是 = 作为填充。

    填充物没什么用,所以我会去掉它。

    然后将/ 替换为_ 并将+ 替换为-。这称为 URL 安全 Base64base64url。在RFC4648 中有描述。

    public static string Base64UrlEncode(byte[] bytes)
    {
        return Convert.ToBase64String(bytes).Replace("=", "").Replace('+', '-').Replace('/', '_');
    }
    
    public static byte[] Base64UrlDecode(string s)
    {
        s = s.Replace('-', '+').Replace('_', '/');
        string padding = new String('=', 3 - (s.Length + 3) % 4);
        s += padding;
        return Convert.FromBase64String(s);
    }
    

    【讨论】:

      【解决方案3】:

      Convert.ToBase64String 使用字母、数字、+/,因此您可以简单地将 / 换成不是字母、数字或 + 的其他内容:

      编码:

      // ...
      string cipherText = Convert.ToBase64String(cipherTextBytes);
      string ctWithoutSlashes = cipherText.Replace("/", "-");
      

      解码

      string cipherText = ctWithoutSlashes.Replace("-", "/");
      // ...
      

      【讨论】:

        猜你喜欢
        • 2013-04-20
        • 1970-01-01
        • 1970-01-01
        • 2017-02-28
        • 2012-07-01
        • 2018-08-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多