c# - DateTime encryption and decryption, System.Text.Encoding.Unicode.GetBytes and System.Text.Encoding.Unicode.GetString not working as expected? -
i have minimal class encrypt , decrypt datetime objects without time component. test below work 1988 jan 01, fail 1988 jan 02, ie. first iteration passes sec fails already.
it seems problem outside of encryption , decryption of bytes. when debugging sec iteration (1988 jan 02), encrypted in datetime2encryptedstring has next value:
{byte[16]} [0]: 147 [1]: 1 [2]: 22 [3]: 250 [4]: 74 [5]: 227 [6]: 225 [7]: 91 [8]: 157 [9]: 202 [10]: 138 [11]: 246 [12]: 91 [13]: 131 [14]: 42 [15]: 217
while encrypted in encryptedstring2datetime datetime2encryptedstring's output string parameter has next value:
{byte[16]} [0]: 147 [1]: 1 [2]: 22 [3]: 250 [4]: 74 [5]: 227 [6]: 225 [7]: 91 [8]: 157 [9]: 202 [10]: 138 [11]: 246 [12]: 91 [13]: 131 [14]: 253 [15]: 255
the problem come misunderstanding of bytes string (and vice versa) operations ??
the test
public void test1() { (int year = 1988; year < 2010; year++) { (int month = 1; month < 12; month++) { (int day = 1; day < 28; day++) { var dt = new datetime(year, month, day); testdate(dt); } } } } private void testdate(datetime dt) { var encryptedstring = dateencryption.datetime2encryptedstring(dt); var output = dateencryption.encryptedstring2datetime(encryptedstring); assert.areequal(dt, output); }
and here little utility class
public static class dateencryption { private static readonly byte[] key = new byte[] { 32, 29, 124, 21, 92, 18, 28,34, 74, 85, 14, 91, 51, 28, 73, 49, 54, 99, 1, 192, 211, 253, 251, 252, 237, 142, 161, 178, 199, 208, 97, 98 }; private static readonly byte[] iv = new byte[] { 19, 28, 33, 77, 131, 178, 192, 200, 215, 148, 247, 192, 184, 127, 3, 7}; private static byte[] decrypt(byte[] cipherdata) { byte[] decrypteddata; using (memorystream ms = new memorystream()) { using (rijndael alg = rijndael.create()) { alg.padding = paddingmode.none; alg.key = key; alg.iv = iv; using (cryptostream cs = new cryptostream(ms, alg.createdecryptor(), cryptostreammode.write)) { cs.write(cipherdata, 0, cipherdata.length); } decrypteddata = ms.toarray(); } } homecoming decrypteddata; } private static byte[] encrypt(byte[] cleardata) { byte[] encrypteddata; using (memorystream ms = new memorystream()) { using (rijndael alg = rijndael.create()) { alg.padding = paddingmode.none; alg.key = key; alg.iv = iv; using (cryptostream cs = new cryptostream(ms, alg.createencryptor(), cryptostreammode.write)) { cs.write(cleardata, 0, cleardata.length); } encrypteddata = ms.toarray(); } } homecoming encrypteddata; } #region datetimeencryption public static string datetime2encryptedstring(datetime dt) { var dt2str = string.format("{0:d4}{1:d2}{2:d2}", dt.year, dt.month, dt.day); var str2bytes = system.text.encoding.unicode.getbytes(dt2str); var encrypted = encrypt(str2bytes); homecoming system.text.encoding.unicode.getstring(encrypted); } public static datetime encryptedstring2datetime(string s) { var encrypted = system.text.encoding.unicode.getbytes(s); var decrypted = decrypt(encrypted); var bytes2str = system.text.encoding.unicode.getstring(decrypted); homecoming new datetime(int.parse(bytes2str.substring(0, 4)), int.parse(bytes2str.substring(4, 2)), int.parse(bytes2str.substring(6, 2))); } #endregion }
not every sequence of bytes valid in unicode. system.text.encoding.unicode.getstring
ignores such sequences. base64 strings designed convert sequence of bytes in string. in .net work base64 strings via convert class through methods such tobase64string(byte[])
, frombase64string(string)
.
c# string encoding .net-2.0
No comments:
Post a Comment