c# - AES to return Alphanumeric -
i have aes encryption code, want make return alphanumerical characters {0123456789abcdefghijklmnopqrstwuvyz}
but not figure out how that. have no idea encryption, not figure out fix. apreciate feedback. regards...
public class clscrypto { private string _key = string.empty; protected internal string key { { return _key; } set { if (!string.isnullorempty(value)) { _key = value; } } } private string _iv = string.empty; protected internal string iv { { return _iv; } set { if (!string.isnullorempty(value)) { _iv = value; } } } private string calcmd5(string strinput) { string stroutput = string.empty; if (!string.isnullorempty(strinput)) { try { stringbuilder strhex = new stringbuilder(); using (md5 md5 = md5.create()) { byte[] bytartext = encoding.default.getbytes(strinput); byte[] bytarhash = md5.computehash(bytartext); (int = 0; < bytarhash.length; i++) { strhex.append(bytarhash[i].tostring("x2")); } stroutput = strhex.tostring(); } } catch (exception ex) { messagebox.show(ex.message); } } return stroutput; } private byte[] getbytesfromhexstring(string strinput) { byte[] bytaroutput = new byte[] { }; if ((!string.isnullorempty(strinput)) && strinput.length % 2 == 0) { soaphexbinary hexbinary = null; try { hexbinary = soaphexbinary.parse(strinput); } catch (exception ex) { messagebox.show(ex.message); } bytaroutput = hexbinary.value; } return bytaroutput; } private byte[] generateiv() { byte[] bytaroutput = new byte[] { }; try { string striv = calcmd5(iv); bytaroutput = getbytesfromhexstring(striv); } catch (exception ex) { messagebox.show(ex.message); } return bytaroutput; } private byte[] generatekey() { byte[] bytaroutput = new byte[] { }; try { string strkey = calcmd5(key); bytaroutput = getbytesfromhexstring(strkey); } catch (exception ex) { messagebox.show(ex.message); } return bytaroutput; } protected internal string encrypt(string strinput, ciphermode ciphermode) { string stroutput = string.empty; if (!string.isnullorempty(strinput)) { try { byte[] byteplaintext = encoding.default.getbytes(strinput); using (rijndaelmanaged rijmanaged = new rijndaelmanaged()) { rijmanaged.mode = ciphermode; rijmanaged.blocksize = 128; rijmanaged.keysize = 128; rijmanaged.iv = generateiv(); rijmanaged.key = generatekey(); rijmanaged.padding = paddingmode.zeros; icryptotransform icpotransform = rijmanaged.createencryptor(rijmanaged.key, rijmanaged.iv); using (memorystream memstream = new memorystream()) { using (cryptostream cpostream = new cryptostream(memstream, icpotransform, cryptostreammode.write)) { cpostream.write(byteplaintext, 0, byteplaintext.length); cpostream.flushfinalblock(); } stroutput = encoding.default.getstring(memstream.toarray()); } } } catch (exception ex) { messagebox.show(ex.message); } } return stroutput; } protected internal string decrypt(string strinput, ciphermode ciphermode) { string stroutput = string.empty; if (!string.isnullorempty(strinput)) { try { byte[] byteciphertext = encoding.default.getbytes(strinput); byte[] bytebuffer = new byte[strinput.length]; using (rijndaelmanaged rijmanaged = new rijndaelmanaged()) { rijmanaged.mode = ciphermode; rijmanaged.blocksize = 128; rijmanaged.keysize = 128; rijmanaged.iv = generateiv(); rijmanaged.key = generatekey(); rijmanaged.padding = paddingmode.zeros; icryptotransform icpotransform = rijmanaged.createdecryptor(rijmanaged.key, rijmanaged.iv); using (memorystream memstream = new memorystream(byteciphertext)) { using (cryptostream cpostream = new cryptostream(memstream, icpotransform, cryptostreammode.read)) { cpostream.read(bytebuffer, 0, bytebuffer.length); } stroutput = encoding.default.getstring(bytebuffer); } } } catch (exception ex) { messagebox.show(ex.message); } } return stroutput; } }
encryption , decryption functions use byte arrays parameters. so, must convert these arrays base 36 string.
you can use following class (base36) make these conversions:
all have do, calling these 2 functions:
byte[] bytearray; //to convert byte array string string bytearrayinbase36 = base36.bytearraytobase36string(bytearray); //to convert string byte array byte[] bytearray2 = base36.base36stringtobytearray(bytearrayinbase36);
and, class:
using system; using system.collections.generic; class base36 { #region public methods public static string bytearraytobase36string(byte[] bytes) { string result = string.empty; result = encode36((ulong)bytes.length).padleft(base36_length_bloc_size_36, '0'); if (bytes.length > 0) { list<byte[]> byteslist = splitbytes(bytes, 8); if (byteslist[byteslist.count - 1].length < 8) { byte[] newlastarray = new byte[8]; byteslist[byteslist.count - 1].copyto(newlastarray, 0); byteslist[byteslist.count - 1] = newlastarray; } foreach (byte[] bytearray in byteslist) { ulong value = 0; //for (int = 0; < bytearray.length; i++) value = value * 256 + bytearray[i]; value = bitconverter.touint64(bytearray, 0); result = result + encode36(value).padleft(base36_bloc_size_36, '0'); } } return result; } public static byte[] base36stringtobytearray(string input) { byte[] result = new byte[0]; if (input.length >= base36_length_bloc_size_36) { int arraylength = (int)decode36(input.substring(0, base36_length_bloc_size_36)); string data = input.remove(0, base36_length_bloc_size_36); list<byte[]> byteslist = new list<byte[]>(); foreach (string value36 in new list<string>(splitstringbylength(data, base36_bloc_size_36))) { byte[] bytearray = bitconverter.getbytes(decode36(value36)); byteslist.add(bytearray); } result = joinbytes(byteslist); array.resize(ref result, arraylength); } return result; } #endregion #region const private const int base36_length_bloc_size_36 = 6; private const int base36_bloc_size_36 = 13; //encode36(ulong.maxvalue).length; #endregion #region private methods static string _charlist36 = string.empty; static private string charlist36 { { if (_charlist36.length < 36) { char[] array = new char[36]; (int = 0; < 10; i++) array[i] = (char)(i + 48); (int = 0; < 26; i++) array[i + 10] = (char)(i + 97); _charlist36 = new string(array); } return _charlist36; } } private static list<string> splitstringbylength(string str, int chunksize) { list<string> list = new list<string>(); int i; (i = 0; < str.length / chunksize; i++) { list.add(str.substring(i * chunksize, chunksize)); } = * chunksize; if (i < str.length - 1) list.add(str.substring(i, str.length - i)); return list; } private static string encode36(ulong input) { if (input < 0) throw new argumentoutofrangeexception("input", input, "input cannot negative"); char[] clistarr = charlist36.tochararray(); var result = new stack<char>(); while (input != 0) { result.push(clistarr[input % 36]); input /= 36; } return new string(result.toarray()).toupper(); } private static ulong decode36(string input) { var reversed = reversestring(input.tolower()); ulong result = 0; int pos = 0; foreach (char c in reversed) { result += (ulong)charlist36.indexof(c) * (ulong)math.pow(36, pos); pos++; } return result; } private static string reversestring(string text) { char[] carray = text.tochararray(); string reverse = string.empty; (int = 0; < carray.length / 2; i++) { char c = carray[i]; carray[i] = carray[carray.length - 1 - i]; carray[carray.length - 1 - i] = c; } return new string(carray); } private static byte[] stringtobytes(string str) { byte[] bytes = new byte[str.length * sizeof(char)]; system.buffer.blockcopy(str.tochararray(), 0, bytes, 0, bytes.length); return bytes; } private static list<byte[]> splitbytes(byte[] bytes, int length) { list<byte[]> result = new list<byte[]>(); int position = 0; while (bytes.length - position > length) { byte[] temp = new byte[length]; (int = 0; < temp.length; i++) temp[i] = bytes[i + position]; position += length; result.add(temp); } if (position < bytes.length) { byte[] temp = new byte[bytes.length - position]; (int = 0; + position < bytes.length; i++) temp[i] = bytes[i + position]; result.add(temp); } return result; } private static string bytestostring(byte[] bytes) { char[] chars = new char[bytes.length / sizeof(char)]; system.buffer.blockcopy(bytes, 0, chars, 0, bytes.length); return new string(chars); } private static byte[] joinbytes(list<byte[]> listbytes) { int totallength = 0; foreach (byte[] bytes in listbytes) totallength += bytes.length; byte[] result = new byte[totallength]; int position = 0; foreach (byte[] bytes in listbytes) (int = 0; < bytes.length; i++) { result[position] = bytes[i]; position++; } return result; } #endregion }
Comments
Post a Comment