private byte[] EncryptRDX(byte[] Key, byte[] Vector, byte[] Data, PaddingModes Padding = PaddingModes.Zeros) { int blockSize = Vector.Length; int dataLen = Data.Length; int remainder = dataLen % blockSize; int blocks = Data.Length / blockSize; int alignedSize = blocks * blockSize; int lastBlock = alignedSize - blockSize == 0 ? blockSize : alignedSize - blockSize; int outSize = remainder > 0 ? alignedSize + blockSize : alignedSize; byte[] outputData = new byte[outSize]; IPadding pad; if (Padding == PaddingModes.PKCS7) { pad = new PKCS7(); } else if (Padding == PaddingModes.X923) { pad = new X923(); } else { pad = new ZeroPad(); } using (ICipherMode mode = new CBC(new RDX())) { mode.Cipher.BlockSize = blockSize; mode.Init(true, Key, Vector); for (int i = 0; i < alignedSize; i += blockSize) { mode.Transform(Data, i, outputData, i); } if (remainder > 0) { byte[] temp = new byte[blockSize]; Buffer.BlockCopy(Data, alignedSize, temp, 0, remainder); pad.AddPadding(temp, (int)remainder); mode.Transform(temp, 0, outputData, blockSize); } } return(outputData); }
private void CBCTest(byte[] Key, byte[, ][] Input, byte[, ][] Output) { byte[] outBytes = new byte[16]; byte[] iv = _vectors[0]; int index = 6; if (Key.Length == 24) { index = 8; } else if (Key.Length == 32) { index = 10; } using (CBC mode = new CBC(new RHX())) { mode.Initialize(true, new KeyParams(Key, iv)); for (int i = 0; i < 4; i++) { mode.Transform(Input[index, i], outBytes); if (Evaluate.AreEqual(outBytes, Output[index, i]) == false) { throw new Exception("CBC Mode: Encrypted arrays are not equal!"); } } } index++; using (CBC mode = new CBC(new RHX())) { mode.Initialize(false, new KeyParams(Key, iv)); for (int i = 0; i < 4; i++) { mode.Transform(Input[index, i], outBytes); if (Evaluate.AreEqual(outBytes, _output[index, i]) == false) { throw new Exception("CBC Mode: Decrypted arrays are not equal!"); } } } }
private byte[] EncryptRDX(byte[] Key, byte[] Vector, byte[] Data) { int blocks = Data.Length / 16; byte[] outputData = new byte[Data.Length]; RDX transform = new RDX(); ICipherMode cipher = new CBC(transform); cipher.Init(true, new KeyParams(Key, Vector)); for (int i = 0; i < blocks; i++) { cipher.Transform(Data, i * 16, outputData, i * 16); } return(outputData); }
private byte[] DecryptRDX(byte[] Key, byte[] Vector, byte[] Data, PaddingModes Padding = PaddingModes.Zeros) { int blockSize = Vector.Length; int dataLen = Data.Length; int blocks = Data.Length / blockSize; int lastBlock = dataLen - blockSize == 0 ? blockSize : dataLen - blockSize; byte[] outputData = new byte[Data.Length]; IPadding pad; if (Padding == PaddingModes.PKCS7) { pad = new PKCS7(); } else if (Padding == PaddingModes.X923) { pad = new X923(); } else { pad = new ZeroPad(); } using (ICipherMode mode = new CBC(new RDX())) { mode.Cipher.BlockSize = blockSize; mode.Init(false, Key, Vector); for (int i = 0; i < dataLen; i += blockSize) { mode.Transform(Data, i, outputData, i); } int size = pad.GetPaddingLength(outputData); if (size > 0) { Array.Resize <byte>(ref outputData, dataLen - (size - 1)); } } return(outputData); }
private byte[] DecryptRDX(byte[] Key, byte[] Vector, byte[] Data, PaddingModes Padding = PaddingModes.Zeros) { int blockSize = Vector.Length; int dataLen = Data.Length; int blocks = Data.Length / blockSize; int lastBlock = dataLen - blockSize == 0 ? blockSize : dataLen - blockSize; byte[] outputData = new byte[Data.Length]; IPadding pad; if (Padding == PaddingModes.PKCS7) pad = new PKCS7(); else if (Padding == PaddingModes.X923) pad = new X923(); else pad = new ZeroPad(); using (ICipherMode mode = new CBC(new RDX())) { mode.Cipher.BlockSize = blockSize; mode.Init(false, Key, Vector); for (int i = 0; i < dataLen; i += blockSize) mode.Transform(Data, i, outputData, i); int size = pad.GetPaddingLength(outputData); if (size > 0) Array.Resize<byte>(ref outputData, dataLen - (size - 1)); } return outputData; }
private byte[] EncryptRDX(byte[] Key, byte[] Vector, byte[] Data, PaddingModes Padding = PaddingModes.Zeros) { int blockSize = Vector.Length; int dataLen = Data.Length; int remainder = dataLen % blockSize; int blocks = Data.Length / blockSize; int alignedSize = blocks * blockSize; int lastBlock = alignedSize - blockSize == 0 ? blockSize : alignedSize - blockSize; int outSize = remainder > 0 ? alignedSize + blockSize : alignedSize; byte[] outputData = new byte[outSize]; IPadding pad; if (Padding == PaddingModes.PKCS7) pad = new PKCS7(); else if (Padding == PaddingModes.X923) pad = new X923(); else pad = new ZeroPad(); using (ICipherMode mode = new CBC(new RDX())) { mode.Cipher.BlockSize = blockSize; mode.Init(true, Key, Vector); for (int i = 0; i < alignedSize; i += blockSize) mode.Transform(Data, i, outputData, i); if (remainder > 0) { byte[] temp = new byte[blockSize]; Buffer.BlockCopy(Data, alignedSize, temp, 0, remainder); pad.AddPadding(temp, (int)remainder); mode.Transform(temp, 0, outputData, blockSize); } } return outputData; }
private byte[] EncryptRDX(byte[] Key, byte[] Vector, byte[] Data) { int blocks = Data.Length / 16; byte[] outputData = new byte[Data.Length]; RDX transform = new RDX(); ICipherMode cipher = new CBC(transform); cipher.Init(true, new KeyParams(Key, Vector)); for (int i = 0; i < blocks; i++) cipher.Transform(Data, i * 16, outputData, i * 16); return outputData; }