public async Task <byte[]> HandleEncryption(CipherAction cipherAction, SymmetricCipherMode cipherMode, CipherBlockSize cipherBlockSize, Stream data, Stream keyStream, Stream initilizationVectorStream) { var dataToProcess = await ReadAllBytesFromStream(data); var key = await ReadAllBytesFromStream(keyStream); _symmetricCipherManager.Key = key; _symmetricCipherManager.CipherMode = cipherMode; _symmetricCipherManager.CipherBlockSize = cipherBlockSize; if (cipherMode is not SymmetricCipherMode.ElectronicCodeBook) { var initializationVector = await ReadAllBytesFromStream(initilizationVectorStream); _symmetricCipherManager.InitializationVector = initializationVector; } var processedData = new byte[] {}; switch (cipherAction) { case CipherAction.Encrypt: processedData = _symmetricCipherManager.Encrypt(dataToProcess); break; case CipherAction.Decrypt: processedData = _symmetricCipherManager.Decrypt(dataToProcess); break; } return(processedData); }
public static string Cipher(CipherAction action, CipherType type, string data) { try { byte[] iv; byte[] key; switch (type) { case CipherType.UserPassword: iv = Encoding.UTF8.GetBytes("B?E(H+MbQeThWmZq"); key = Encoding.UTF8.GetBytes("(H+MbQeThWmZq4t7w!z%C*F-J@NcRfUj"); break; case CipherType.Token: iv = Encoding.UTF8.GetBytes("aNdRgUkXp2s5v8y/"); key = Encoding.UTF8.GetBytes(")H@McQfTjWnZr4u7x!A%D*G-JaNdRgUk"); break; default: return(null); } switch (action) { case CipherAction.Encrypt: return(Encrypt(iv, key, data)); case CipherAction.Decrypt: return(Decrypt(iv, key, data)); default: return(null); } } catch { return(null); } }
private byte[] EncryptionConvertion(byte[] data, CipherAction cipherAction) { var message = new BitArray(data); message.applyPermutations(ip); var twoParts = message.DevideByParts(2); var L = twoParts[0]; var R = twoParts[1]; for (int i = 0; i < 16; i++) { var keyIndex = cipherAction == CipherAction.Encrypt ? i : 16 - i; var tempL = L.Clone() as BitArray; L = R.Clone() as BitArray; R = tempL ^ F(R, _roundKeys[keyIndex]); } var res = R.JoinArr(L); res.applyPermutations(ip1); return(res.getByteArrayFromBitArray); }
private List <byte[]> EncryptionConvertion(ISymmetricCipher symmetricCipher, List <byte[]> data, CipherAction cipherAction) { var encryptedMessageblocks = new byte[data.Count][]; Func <byte[], byte[]> cipherConvertion = cipherAction switch { CipherAction.Encrypt => symmetricCipher.Encrypt, CipherAction.Decrypt => symmetricCipher.Decrypt }; var parallelForResult = Parallel.For(0, data.Count, blockNumber => { encryptedMessageblocks[blockNumber] = cipherConvertion(data[blockNumber]); }); return(encryptedMessageblocks.ToList()); } }
/// <summary> /// Encrypts/Decrypts using OFB /// </summary> /// <param name="blockCipher"></param> /// <param name="cipherAction"></param> /// <param name="inputStream"></param> /// <param name="outputStream"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <param name="padding"></param> /// <param name="stop"></param> /// <param name="progressChanged"></param> /// <param name="lastInputBlock"></param> /// <param name="blocksize"></param> public static void ExecuteOFB(BlockCipher blockCipher, CipherAction cipherAction, ref ICrypToolStream inputStream, ref ICrypToolStream outputStream, byte[] key, byte[] iv, PaddingType padding, ref bool stop, ProgressChanged progressChanged, ref byte[] lastInputBlock, int blocksize = 8) { using (CStreamReader reader = inputStream.CreateReader()) { using (CStreamWriter writer = new CStreamWriter()) { byte[] lastBlock = iv; int readcount = 0; while (reader.Position < reader.Length && !stop) { //we always try to read a complete block byte[] inputBlock = new byte[blocksize]; readcount = 0; while ((readcount += reader.Read(inputBlock, readcount, blocksize - readcount)) < blocksize && reader.Position < reader.Length && !stop) { ; } if (stop) { return; } //Show progress in UI progressChanged(reader.Position, reader.Length); byte[] outputblock = null; //we read a complete block if (readcount == blocksize) { //Compute XOR with lastblock for OFB mode if (cipherAction == CipherAction.Encrypt) { outputblock = blockCipher(lastBlock, key); lastBlock = outputblock; outputblock = XOR(outputblock, inputBlock); } else { outputblock = blockCipher(lastBlock, key); lastBlock = outputblock; outputblock = XOR(outputblock, inputBlock); } } //we read an incomplete block, thus, we are at the end of the stream else if (readcount > 0) { //Compute XOR with lastblock for CFB mode if (cipherAction == CipherAction.Encrypt) { byte[] block = new byte[blocksize]; Array.Copy(inputBlock, 0, block, 0, readcount); outputblock = blockCipher(lastBlock, key); outputblock = XOR(outputblock, block); } else { byte[] block = new byte[blocksize]; Array.Copy(inputBlock, 0, block, 0, readcount); outputblock = blockCipher(inputBlock, key); outputblock = XOR(outputblock, lastBlock); } } //check if it is the last block and we decrypt, thus, we have to remove the padding if (reader.Position == reader.Length && cipherAction == CipherAction.Decrypt && padding != PaddingType.None) { int valid = StripPadding(outputblock, blocksize, padding, blocksize); if (valid != blocksize) { byte[] newoutputblock = new byte[valid]; Array.Copy(outputblock, 0, newoutputblock, 0, valid); outputblock = newoutputblock; } else if (valid == 0) { outputblock = null; } } //if we crypted something, we output it if (outputblock != null) { writer.Write(outputblock, 0, outputblock.Length); //if we wrote to the stream, we memorize the last input block for the visualization lastInputBlock = inputBlock; } } writer.Flush(); outputStream = writer; } } }