c# - Invalid length for a Base-64 char array error -
as title says, getting:
invalid length base-64 char array.
so doing construct email delivery using below method:
smsg += "<br><b>to complete registration, verify email</b>" + "<a href=" + siteurl + "?usenamw=" + encrypt(username.trim()) + "><br>here!</a>"; the encrypt method looks this:
private static string encrypt(string cleartext) { string encryptionkey = "makv2spbni99212"; byte[] clearbytes = encoding.unicode.getbytes(cleartext); using (aes encryptor = aes.create()) { rfc2898derivebytes pdb = new rfc2898derivebytes(encryptionkey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.key = pdb.getbytes(32); encryptor.iv = pdb.getbytes(16); using (memorystream ms = new memorystream()) { using (cryptostream cs = new cryptostream(ms, encryptor.createencryptor(), cryptostreammode.write)) { cs.write(clearbytes, 0, clearbytes.length); cs.close(); } cleartext = convert.tobase64string(ms.toarray()); } } return cleartext; } here html looks in hotmail:
http://localhost:30850/activation.aspx?username=9mqkc5veebsl2qg6hbxl+r3kvnhoesig32op6eb6rd0=
on receiving end, activation.aspx.cs page has line:
string username = request.querystring["username"].tostring(); label1.text = decrypt(username); and decryption method:
private string decrypt(string ciphertext) { string encryptionkey = "makv2spbni99212"; byte[] cipherbytes = convert.frombase64string(ciphertext); using (aes encryptor = aes.create()) { rfc2898derivebytes pdb = new rfc2898derivebytes(encryptionkey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.key = pdb.getbytes(32); encryptor.iv = pdb.getbytes(16); using (memorystream ms = new memorystream()) { using (cryptostream cs = new cryptostream(ms, encryptor.createdecryptor(), cryptostreammode.write)) { cs.write(cipherbytes, 0, cipherbytes.length); cs.close(); } ciphertext = encoding.unicode.getstring(ms.toarray()); } } return ciphertext; } so when input username foe example jonathan have no problem displaying jonathan on receiving end label. however, if input not multiple of 4 error. understand base64 takes in multiple of 4, if has 7 characters add space it. there solution this? thank much.
the base64 username string padded '=' @ end; ignored when url query string being interpreted. ie:
username => "9mqkc5veebsl2qg6hbxl+r3kvnhoesig32op6eb6rd0=" becomes:
username => 9mqkc5veebsl2qg6hbxl+r3kvnhoesig32op6eb6rd0 in request handler.
you need url encode string before using in url.
Comments
Post a Comment