public byte[] encrypt(byte[] plainData, int length, ushort pss, ushort cseq, ushort sseq) { UInt32 seqValue = (uint)pss << 24 | (uint)cseq << 12 | (uint)sseq; byte[] sequences = BitConverter.GetBytes(seqValue); Array.Reverse(sequences); // Create the packet that needs to be CRC32 checksummed PacketContent packet = new PacketContent(); packet.addUint16((UInt16)(plainData.Length + 4), 1); packet.addByteArray(TimeUtils.getCurrentTime()); packet.addByteArray(sequences); packet.addByteArray(plainData); byte[] paddingBytes = getPaddedBytes(packet.returnFinalPacket()); // Final Packet which needs to be encrypted PacketContent packetWithCrc = new PacketContent(); packetWithCrc.addByteArray(crc32.checksumB(packet.returnFinalPacket(), 1)); packetWithCrc.addByteArray(packet.returnFinalPacket()); packetWithCrc.addByteArray(paddingBytes); string hexPackCrcedPlain = StringUtils.bytesToString(packetWithCrc.returnFinalPacket()); string paddingBytesHex = StringUtils.bytesToString(paddingBytes); innerBuffer = new byte[packetWithCrc.returnFinalPacket().Length]; tf.encrypt(packetWithCrc.returnFinalPacket(), innerBuffer); PacketContent encryptedPacket = new PacketContent(); encryptedPacket.addByte(0x01); encryptedPacket.addByteArray(IV); encryptedPacket.addByteArray(innerBuffer); innerBuffer = new byte [2048]; string hexPackEncrypt = StringUtils.bytesToString(encryptedPacket.returnFinalPacket()); return(encryptedPacket.returnFinalPacket()); }
private int crcCheck() { Crc32 crc32 = new Crc32(); byte[] knownResult = { 0x0d, 0x1e, 0xe7, 0xea }; string testString = "this is a test"; byte[] testArray = StringUtils.stringToBytes(testString); // Test is done with a non reversed array byte [] result = crc32.checksumB(testArray, 0); if (!ArrayUtils.equal(knownResult, result)) { Console.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 crcCheck() { Crc32 crc32 = new Crc32(); byte[] knownResult={0x0d,0x1e,0xe7,0xea}; string testString = "this is a test"; byte[] testArray = StringUtils.stringToBytes(testString); // Test is done with a non reversed array byte [] result = crc32.checksumB(testArray,0); if (!ArrayUtils.equal(knownResult, result)) { Console.Write("Failed\n"); return 0; } Output.Write("OK\n"); return 1; }