The need for the encryption / decryption method does not have a "/" in the encrypted string

I need to encrypt and decrypt string values ​​such as email address and numeric values, but the encrypted string should not contain "/" because I use this in the url and use "/" for the delimiter to get some values.

I am currently using the following method:

    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;
    }
+5
source share
3 answers

If you do this only for passing to the URL, I suggest you create any encrypted string (whether it has one /or not) and follow these steps:

var sanitized = HttpUtility.UrlEncode(encryptedString);

, / %2f. :

var encryptedString = HttpUtility.UrlDecode(sanitized)

.

: HttpUtility System.Web.

+11

, . /. , URL-. Base64 64.

/ Base64, . Base64 ASCII ( 62), / + , , = .

, .

/ _ + -. URL Base64 base64url. 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);
}
+4

Convert.ToBase64Stringuses letters, numbers, +and /so you can just turn off /for something else, rather than letters, numbers, or +:

Encoding:

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

Decoding

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

All Articles