Search code examples
asp.netasp.net-mvc-3base64url-encoding

Is there a way to get the DESCryptoServiceProvider to not use special characters like plus signs?


When creating an encrypted string:

 using(var cryptoProvider = new DESCryptoServiceProvider())
    {
      cryptoProvider.Key = Convert.FromBase64String(Key);
      cryptoProvider.IV = Convert.FromBase64String(Key);

      var encryptedString = cryptoProvider
        .CreateEncryptor()
        .TransformFinalBlock(serializedBytes, 0, serializedBytes.Length);

      return Convert.ToBase64String(encryptedString);

Is there anyway to stop it from using certain characters? The character that is actually causing problems is the "+" sign. The problem is apparent when asp.net decodes and (for example) a + sign is turned into a space.

There might be other characters that would be fall into the same issue, but the + sign seems to be the only one I have encountered as an issue. Is there a way to either not have the encryption use a certain character, or a way to protect against this in my encryption logic?


Solution

  • So in my particular case where the encrypted value is used in url's, to avoid the mentioned url encoding/decoding management nightmare, using

    HttpUtilityServer.UrlTokenEncode / UrlTokenDecode
    

    takes care of all the issues presented with the padded Ascii values used by the To64BaseString().