c# - Simple Byte Encryption Not Working -
when message decrypted, characters 1 less original. example: h g
i have tried debug code printing out values , goes until trying divide 100000 , multiplying date
here code used: didn't include main method here
public static string encrypt(string input) { string final; string date = datetime.now.date.toshortdatestring().tostring(); var datetime = int.parse(date.replace("/", "")); list<int> semi = new list<int>(); var bytes = encoding.utf8.getbytes(input.tochararray()); (int = 0; < bytes.length; i++) { int y = bytes[i] * datetime / 100000; semi.add(y); console.writeline(y); } console.writeline(string.join("", bytes)); final = string.join(":", semi.toarray()) + ":" + date; return final; } public static string decrypt(string input) { string final; string[] raw = input.split(':'); int date = int.parse(raw[raw.length - 1].replace("/","")); var dump = new list<string>(raw); dump.removeat(raw.length - 1); string[] stringbytes = dump.toarray(); list<byte> bytes = new list<byte>(); (int = 0; < stringbytes.length; i++) { int x = int.parse(stringbytes[i]); console.writeline(x); x = x * 100000 / date; byte finalbytes = convert.tobyte(x); bytes.add(finalbytes); } console.writeline(string.join("", bytes.toarray())); console.writeline(date); var bytearray = bytes.toarray(); final = encoding.utf8.getstring(bytearray); return final; }
you using low precision datatype int
store result of division. have changed type double
, works
public static string encrypt(string input) { string final; string date = datetime.now.date.tostring("mmddyyyy"); var datetime = int.parse(date); list<double> semi = new list<double>(); var bytes = encoding.utf8.getbytes(input); (int = 0; < bytes.length; i++) { double y = bytes[i] * datetime / 100000; semi.add(y); console.writeline(y); } console.writeline(string.join("", bytes)); final = string.join(":", semi.toarray()) + ":" + date; return final; } public static string decrypt(string input) { string final; string[] raw = input.split(':'); int date = int.parse(raw[raw.length - 1].replace("/", "")); var dump = new list<string>(raw); dump.removeat(raw.length - 1); string[] stringbytes = dump.toarray(); list<byte> bytes = new list<byte>(); (int = 0; < stringbytes.length; i++) { var x = double.parse(stringbytes[i]); console.writeline(x); x = x * 100000 / date; byte finalbytes = convert.tobyte(x); bytes.add(finalbytes); } console.writeline(string.join("", bytes.toarray())); console.writeline(date); var bytearray = bytes.toarray(); final = encoding.utf8.getstring(bytearray); return final; }
here working console app http://ideone.com/rjc13a
Comments
Post a Comment