Saturday, 15 May 2010

c# - How to apply padding for Base64 -



c# - How to apply padding for Base64 -

i have next code works fine when utilize 4 letter word input, e.g. “test”. when input not multiple of 4, fails e.g. “mytest”.

eexception: invalid length base-64 char array.

questions

is guaranteed encrypted result compatible unicode string (without loss). if yes can utilize utf encoding instead of base64? what's difference between utf8/utf16 , base64 in terms of encoding how can add together padding right result (after decryption) if input not multiple of 4?

main prgoram

class programme { static void main(string[] args) { string valid128bitstring = "aaecawqfbgcicqolda0odw=="; string inputvalue = "mytest"; string keyvalue = valid128bitstring; byte[] bytevalforstring = convert.frombase64string(inputvalue); encryptresult result = aes128utility.encryptdata(bytevalforstring, keyvalue); encryptresult encyptedvalue = new encryptresult(); string resultingiv = "4uy34c9sqoc9rbv4gd8jra=="; if (string.equals(resultingiv,result.iv)) { int x = 0; } encyptedvalue.iv = resultingiv; encyptedvalue.encryptedmsg = result.encryptedmsg; string finalresult = convert.tobase64string(aes128utility.decryptdata(encyptedvalue, keyvalue)); console.writeline(finalresult); if (string.equals(inputvalue, finalresult)) { console.writeline("match"); } else { console.writeline("differ"); } console.readline(); } }

aes crypto utility

public static class aes128utility { private static byte[] key; public static encryptresult encryptdata(byte[] rawdata, string strkey) { encryptresult result = null; if (key == null) { if (!string.isnullorempty(strkey)) { key = convert.frombase64string((strkey)); result = encrypt(rawdata); } } else { result = encrypt(rawdata); } homecoming result; } public static byte[] decryptdata(encryptresult encryptresult, string strkey) { byte[] origdata = null; if (key == null) { if (!string.isnullorempty(strkey)) { key = convert.frombase64string(strkey); origdata = decrypt(convert.frombase64string(encryptresult.encryptedmsg), convert.frombase64string(encryptresult.iv)); } } else { origdata = decrypt(convert.frombase64string(encryptresult.encryptedmsg), convert.frombase64string(encryptresult.iv)); } homecoming origdata; } private static encryptresult encrypt(byte[] rawdata) { using (aescryptoserviceprovider aesprovider = new aescryptoserviceprovider()) { aesprovider.key = key; aesprovider.mode = ciphermode.cbc; aesprovider.padding = paddingmode.pkcs7; aesprovider.iv = convert.frombase64string("4uy34c9sqoc9rbv4gd8jra=="); using (memorystream memstream = new memorystream()) { cryptostream encstream = new cryptostream(memstream, aesprovider.createencryptor(), cryptostreammode.write); encstream.write(rawdata, 0, rawdata.length); encstream.flushfinalblock(); encryptresult encresult = new encryptresult(); encresult.encryptedmsg = convert.tobase64string(memstream.toarray()); encresult.iv = convert.tobase64string(aesprovider.iv); homecoming encresult; } } } private static byte[] decrypt(byte[] encryptedmsg, byte[] iv) { using (aescryptoserviceprovider aesprovider = new aescryptoserviceprovider()) { aesprovider.key = key; aesprovider.iv = iv; aesprovider.mode = ciphermode.cbc; aesprovider.padding = paddingmode.pkcs7; using (memorystream memstream = new memorystream()) { cryptostream decstream = new cryptostream(memstream, aesprovider.createdecryptor(), cryptostreammode.write); decstream.write(encryptedmsg, 0, encryptedmsg.length); decstream.flushfinalblock(); homecoming memstream.toarray(); } } } }

dto

public class encryptresult { public string encryptedmsg { get; set; } public string iv { get; set; } }

references:

how create byte[] length 16 using frombase64string getting wrong decryption value using aescryptoserviceprovider

base64 way of representing binary values text not conflict mutual command codes \x0a newline or \0 string terminator. not turning typed text in binary.

here how should passing text in , getting out. can replace utf8 whatever encoding want, need create sure encoding.whatever.getbytes same encoding encoding.whatever.getstring

class programme { static void main(string[] args) { string valid128bitstring = "aaecawqfbgcicqolda0odw=="; string inputvalue = "mytest"; string keyvalue = valid128bitstring; //turns our text in binary info byte[] bytevalforstring = encoding.utf8.getbytes(inputvalue); encryptresult result = aes128utility.encryptdata(bytevalforstring, keyvalue); encryptresult encyptedvalue = new encryptresult(); //(snip) encyptedvalue.iv = resultingiv; encyptedvalue.encryptedmsg = result.encryptedmsg; string finalresult = encoding.utf8.getstring(aes128utility.decryptdata(encyptedvalue, keyvalue)); console.writeline(finalresult); if (string.equals(inputvalue, finalresult)) { console.writeline("match"); } else { console.writeline("differ"); } console.readline(); } }

c# .net encryption cryptography

No comments:

Post a Comment