public int auth_client(byte[] givenusername) { int kdnr = 0; for(kdnr=0;kdnr<kundendb.Length;kdnr++) { MD5 uname = new MD5CryptoServiceProvider(); ASCIIEncoding uname_md5_encoding=new ASCIIEncoding(); byte[] uname_md5 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; uname_md5 = uname.ComputeHash(uname_md5_encoding.GetBytes(kundendb[kdnr].usr)); uname.Clear(); //ASCIIEncoding crc32_encoding = new ASCIIEncoding(); CRC32 crc = new CRC32(0xedb88320); // equivalent to new CRC32(CRC32.DefaultPolynomial); byte[] uname_crc32 = { 0, 0, 0, 0 }; uname_crc32 = crc.ComputeHash(uname_md5); crc.Clear(); if((givenusername[0]==uname_crc32[0]) && (givenusername[1]==uname_crc32[1]) &&(givenusername[2]==uname_crc32[2]) &&(givenusername[3]==uname_crc32[3])) { return kdnr; } } return -1; //lets return the "kundennummer" of the right viewer }
public void RangeTest() { byte[] sample = new byte[10000]; const int delta = 19; new System.Random().NextBytes(sample); byte[] sample2 = new byte[10]; for(int i=0; i<10; i++) { sample2[i] = sample[i+delta]; } CRC32 expected = new CRC32(); expected.ComputeHash(sample2, 0, 10); CRC32 actual = new CRC32(); actual.ComputeHash(sample, delta, 10); Assert.AreEqual(BitConverter.ToString(expected.Hash), BitConverter.ToString(actual.Hash)); }
public void MethodsTest() { byte[] sample = new byte[10000]; new System.Random().NextBytes(sample); CRC32 a1 = new CRC32(); CRC32 a2 = new CRC32(); CRC32 a3 = new CRC32(); CRC32 a4 = new CRC32(); //method1 byte[] a1Hash = a1.ComputeHash(sample); //method2 a2.TransformFinalBlock(sample, 0, 10000); byte[] a2Hash = a2.Hash; //method3 for(int i=0; i<10000; i+=1000) { a3.TransformBlock(sample, i, 1000, sample, i); } a3.TransformFinalBlock(sample, 0, 0); byte[] a3Hash = a3.Hash; //method4 for(int i=0; i<10000; i+=1000) { if (i == 9000) a4.TransformFinalBlock(sample, i, 1000); else a4.TransformBlock(sample, i, 1000, sample, i); } byte[] a4Hash = a4.Hash; Assert.AreEqual(BitConverter.ToString(a1Hash), BitConverter.ToString(a2Hash), "Method a2 failed"); Assert.AreEqual(BitConverter.ToString(a1Hash), BitConverter.ToString(a3Hash), "Method a3 failed"); Assert.AreEqual(BitConverter.ToString(a1Hash), BitConverter.ToString(a4Hash), "Method a4 failed"); }
public int validate_request(byte[] message) { int i=0; switch(message[0]) { case 0: { byte length = message[1]; byte[] datacrc = new byte[4]; byte[] data = new byte[length]; Array.Copy(message,4,datacrc,0,4); Array.Copy(message,20,data,0,length); CRC32 crc = new CRC32(0xedb88320); byte[] data_crc32 = crc.ComputeHash(data); for(i=0;i<4;i++) { if(datacrc[i]!=data_crc32[i]) { return -1; //crc not correct - data broken ! } } return 1; // data is ok, we can proceed ... } case 2: { return 0; //not implemented yet } } return -1; }
public byte[] generate_cwanswer(byte[] checksum, byte[] message, byte[] cw, string key) { byte[] cwanswer = new byte[48]; byte[] cryptedanswer = new byte[52]; MemoryStream ms = new MemoryStream(); CRC32 crc = new CRC32(0xedb88320); // equivalent to new CRC32(CRC32.DefaultPolynomial); byte[] cw_crc32 = crc.ComputeHash(cw); //Array.Copy(checksum,0,cwanswer,0,4); Array.Copy(message,0,cwanswer,0,20); Array.Copy(cw_crc32,0,cwanswer,4,4); Array.Copy(cw,0,cwanswer,20,16); cwanswer[0] = 0x01; cwanswer[1] = 0x10; cwanswer[2] = 0x32; cwanswer[3] = 0x84; for(int i=0;i<12;i++) cwanswer[i+36] = 0xFF; MD5 passwd = new MD5CryptoServiceProvider(); ASCIIEncoding passwd_md5_encoding=new ASCIIEncoding(); byte[] passwd_md5 = passwd.ComputeHash(passwd_md5_encoding.GetBytes(key)); RijndaelManaged aes = new RijndaelManaged(); aes.Mode = CipherMode.ECB; aes.BlockSize = 128; aes.KeySize = 128; //128-Bit AES En/Decryption aes.Key = passwd_md5; CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(cwanswer, 0, 48); aes.Clear(); byte[] cryptedData = ms.ToArray(); Array.Copy(cryptedData,0,cryptedanswer,4,48); Array.Copy(checksum,0,cryptedanswer,0,4); return cryptedanswer; }