The encryption itself simply outputs bytes, not characters. So this question is quite unrelated to encryption/decryption. Your actual problem is converting arbitrary bytes into a string that can be used in a url. I recommend using URL safe Base64 over normal Base64 for this.
Those /
characters are produced by the Base64 encoding you apply to the ciphertext. Base64 uses ASCII letters and digits (62 total), plus /
and +
and finally =
as padding.
Padding is pretty useless, so I'd strip it.
Then replace /
with _
and +
with -
. This is called URL safe Base64 or base64url. It's described in 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);
}