private int twofishEncrypt() { ArrayUtils au = new ArrayUtils(); byte[] key = { 0xa4, 0x46, 0xdc, 0x73, 0x25, 0x08, 0xee, 0xb7, 0x6c, 0x9e, 0xb4, 0x4a, 0xb8, 0xe7, 0x11, 0x03 }; byte[] iv = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; byte[] plainText = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; byte[] knownResult = { 0xba, 0x05, 0xf5, 0x6e, 0x86, 0x5f, 0xb6, 0xc9, 0xb2, 0x1d, 0xe1, 0x00, 0xb6, 0xec, 0x46, 0xe9 }; byte[] result = new byte[16]; MxoTwofish tf = new MxoTwofish(); tf.setIV(iv); tf.setKey(key); tf.encrypt(plainText, result); if (!ArrayUtils.equal(knownResult, result)) { Output.Write("Failed\n"); Output.WriteLine(StringUtils.bytesToString(knownResult)); Output.WriteLine(StringUtils.bytesToString(result)); return(0); } Output.Write("OK\n"); return(1); }
public void initCrypto() { crc32 = new Crc32(); tf = new MxoTwofish(); tf.setIV(IV); tf.setKey(TF_Key); }
public ArrayList decrypt(byte[] encryptedData, int length) { ArrayList response = new ArrayList(); UInt16 pss = 0; UInt16 cseq = 0; UInt16 sseq = 0; PacketReader readerEncrypted = new PacketReader(encryptedData); readerEncrypted.incrementOffsetByValue(1); // skip 0x01 IV = readerEncrypted.readBytes(16); tf.setIV(IV); // Read the whole Data innerBuffer = new byte[encryptedData.Length - 17]; byte[] decryptBuffer = readerEncrypted.readBytes(encryptedData.Length - 17); tf.decrypt(decryptBuffer, innerBuffer); // just copy to clean byte[] decryptedPacket = innerBuffer; innerBuffer = new byte[2048]; PacketReader reader = new PacketReader(decryptedPacket); UInt32 crc32fromPacket = reader.readUInt32(1); byte[] packetCheckData = reader.readBytes(decryptedPacket.Length - 4); UInt32 crc32plainDataToCompare = NumericalUtils.ByteArrayToUint32(crc32.checksumB(packetCheckData, 1), 1); if (crc32plainDataToCompare != crc32fromPacket) { Output.WriteLine("Oh oh, CRC didnt matched in this packet"); } // as we read the whole packet for verifiy we reset the offset to read the other things reader.setOffsetOverrideValue(4); UInt16 packetSize = reader.readUInt16(1); // UInt32 timeStamp = reader.readUInt32(1); // Well we didnt need it - but just read it as uint32 UInt32 seqValues = reader.readUInt32(0); cseq = (UInt16)(seqValues & 0xFFF); sseq = (UInt16)(seqValues >> 12 & 0xFFF); pss = (UInt16)(seqValues >> 24 & 0xFF); // Finally add our responses and read the rest of the packet response.Add(reader.readBytes(packetSize - 4)); response.Add(pss); response.Add(cseq); response.Add(sseq); return(response); }
private int twofishEncrypt() { ArrayUtils au = new ArrayUtils(); byte[] key = { 0xa4,0x46,0xdc,0x73,0x25,0x08,0xee,0xb7,0x6c,0x9e,0xb4,0x4a,0xb8,0xe7,0x11,0x03 }; byte[] iv = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; byte[] plainText = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; byte[] knownResult = {0xba,0x05,0xf5,0x6e,0x86,0x5f,0xb6,0xc9,0xb2,0x1d,0xe1,0x00,0xb6,0xec,0x46,0xe9}; byte[] result = new byte[16]; MxoTwofish tf = new MxoTwofish(); tf.setIV(iv); tf.setKey(key); tf.encrypt(plainText,result); if (!ArrayUtils.equal(knownResult, result)){ Output.Write("Failed\n"); Output.WriteLine(StringUtils.bytesToString(knownResult)); Output.WriteLine(StringUtils.bytesToString(result)); return 0; } Output.Write("OK\n"); return 1; }
private int twofishDecrypt() { byte[] key = { 0x6C, 0xAB, 0x8E, 0xCC, 0xE7, 0x3C, 0x22, 0x47, 0xDB, 0xEB, 0xDE, 0x1A, 0xA8, 0xE7, 0x5F, 0xB8 }; byte[] iv = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }; byte[] cipherText = { 0x77, 0x7f, 0x54, 0xc6, 0xc9, 0x9e, 0x35, 0x56, 0x26, 0x4b, 0xc0, 0x17, 0x96, 0x33, 0x79, 0xe8 }; byte[] knownResult = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; byte[] result = new byte[16]; MxoTwofish tf = new MxoTwofish(); tf.setIV(iv); tf.setKey(key); tf.decrypt(cipherText, result); if (!ArrayUtils.equal(knownResult, result)) { Output.Write("Failed\n"); return(0); } Output.Write("OK\n"); return(1); }
public byte[] encrypt(byte[] packet) { // Get timestamp byte[] timestamp = TimeUtils.getUnixTime(); // get size int psize = packet.Length; byte[] size = NumericalUtils.uint16ToByteArray((UInt16)psize, 1); //showPacket(size, " Size "); // final Packet DynamicArray temp = new DynamicArray(); temp.append(size); temp.append(timestamp); temp.append(packet); // compute CRC32 byte[] crc32pax = crc32.checksumB(temp.getBytes(), 1); // Padding int totalLength = temp.getSize() + 4; int padding = 16 - (totalLength % 16); byte[] paddingBytes = new byte[padding]; for (int i = 0; i < padding; i++) { paddingBytes[i] = (byte)padding; } temp.append(paddingBytes); tf.setIV(IV); tf.setKey(TF_Key); // We init with 2 more than needed, so no memory reservation is done on dyn array DynamicArray finalPlainData = new DynamicArray(); finalPlainData.append(crc32pax); finalPlainData.append(temp.getBytes()); temp = null; // Cleaning the house byte[] encryptedData = new byte[finalPlainData.getSize()]; tf.encrypt(finalPlainData.getBytes(), encryptedData); finalPlainData = null; // Cleaning the house (2) // add IV before the results DynamicArray response = new DynamicArray(); response.append(IV); response.append(encryptedData); // Display HEX Values after Encryption return(response.getBytes()); }
private int twofishEncrypt() { byte[] key = { 0x6C, 0xAB, 0x8E, 0xCC, 0xE7, 0x3C, 0x22, 0x47, 0xDB, 0xEB, 0xDE, 0x1A, 0xA8, 0xE7, 0x5F, 0xB8 }; byte[] iv = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }; byte[] plainText = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x08 ,0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; byte[] knownResult = {0x77,0x7f,0x54,0xc6,0xc9,0x9e,0x35,0x56,0x26,0x4b,0xc0,0x17,0x96,0x33,0x79,0xe8}; byte[] result = new byte[16]; MxoTwofish tf = new MxoTwofish(); tf.setIV(iv); tf.setKey(key); tf.encrypt(plainText,result); if (!ArrayUtils.equal(knownResult, result)) { Output.Write("Failed\n"); Output.WriteLine(StringUtils.bytesToString(knownResult)); Output.WriteLine(StringUtils.bytesToString(result)); return 0; } Output.Write("OK\n"); return 1; }