public static byte[] AESDecrypt(byte[] Key, byte[] IV, byte[] cipherText) { if (cipherText == null || cipherText.Length <= 0) { throw new ArgumentNullException("cipherText"); } if (Key == null || Key.Length <= 0) { throw new ArgumentNullException("Key"); } if (IV == null || IV.Length <= 0) { throw new ArgumentNullException("IV"); } BufferedBlockCipher decryptCipher = new BufferedBlockCipher(new CbcBlockCipher(new AesEngine())); decryptCipher.Reset(); ICipherParameters param = new ParametersWithIV(new KeyParameter(Key), IV); decryptCipher.Init(false, param); byte[] buf = new byte[decryptCipher.GetOutputSize(cipherText.Length)]; int len = decryptCipher.ProcessBytes(cipherText, 0, cipherText.Length, buf, 0); len += decryptCipher.DoFinal(buf, len); byte[] plainText = new byte[len]; Array.Copy(buf, 0, plainText, 0, len); return(plainText); }
/// <summary> /// Fills each block with pseudorandom data and appends it to the result. /// Data used for the transformation is the counter changed into a byte array. /// </summary> protected virtual byte[] GenerateBlocks(int numberOfBlocks) { if (!state.Seeded) { throw new GeneratorSeedException("Generator not seeded"); } var result = new byte[numberOfBlocks * CipherBlockSize]; int destArrayLength = 0; var encryptor = new BufferedBlockCipher(cipher); encryptor.Init(true, new KeyParameter(state.Key)); for (int i = 0; i < numberOfBlocks; i++) { var plainText = state.TransformCounterToByteArray(); encryptor.ProcessBytes(plainText, 0, plainText.Length, result, destArrayLength); destArrayLength += plainText.Length; state.Counter++; } encryptor.Reset(); return(result); }
// Coder init public AEScoder(KeyParameter Aeskey) { // Works in EBC/PKCS7 only! // setup AES cipher in ECB mode with PKCS7 padding AesEngine engine = new AesEngine(); _wkCipher = new PaddedBufferedBlockCipher(engine, new Pkcs7Padding()); //Default scheme is PKCS7 // _wkCipher = (BufferedBlockCipher) CipherUtilities.GetCipher("AES/ECB/PKCS7Padding"); this.key = Aeskey; _wkCipher.Reset(); }
public void Dispose() { if (_decryptCipher != null) { _decryptCipher.Reset(); } if (_encryptCipher != null) { _encryptCipher.Reset(); } if (_baseStream != null) { _baseStream.Dispose(); } }
public string Decrypt(byte[] data) { _cipher.Reset(); byte[] bytes = _cipher.DoFinal(data, 0, data.Length); return(Encoding.UTF8.GetString(bytes)); }
public static VariableTunnelBuildMessage BuildInboundTunnel( TunnelInfo setup) { byte usehops = (byte)(setup.Hops.Count > 5 ? 8 : 5); var result = new VariableTunnelBuildMessage(usehops); // Hop sort order var requests = new List <BuildRequestRecord>(); for (int i = 0; i < setup.Hops.Count; ++i) { // Hop order: GW -> us var endpoint = i == setup.Hops.Count - 1; var gateway = i == 0; var rec = new BuildRequestRecord(); rec.Data.Randomize(); rec.Flag = 0; var hop = setup.Hops[i]; rec.OurIdent = hop.Peer.IdentHash; rec.ReceiveTunnel = hop.TunnelId; if (!endpoint) { var nexthop = setup.Hops[i + 1]; rec.NextIdent = nexthop.Peer.IdentHash; rec.NextTunnel = nexthop.TunnelId; } else { // Used to identify the record as the last in an inbound tunnel to us rec.NextIdent = hop.Peer.IdentHash; rec.NextTunnel = hop.TunnelId; } rec.RequestTime = DateTime.UtcNow; rec.FromAnyone = gateway; hop.LayerKey = new I2PSessionKey(rec.LayerKey.Clone()); hop.IVKey = new I2PSessionKey(rec.IVKey.Clone()); requests.Add(rec); #if LOG_ALL_TUNNEL_TRANSFER Logging.Log(rec.ToString()); #endif } // Physical record sort order var order = new List <AesEGBuildRequestRecord>(result.Records); order.Shuffle(); // Scramble the rest for (int i = setup.Hops.Count; i < usehops; ++i) { order[i].Data.Randomize(); } // ElGamal encrypt all of the non random records // and place them in shuffled order. for (int i = 0; i < setup.Hops.Count; ++i) { var hop = setup.Hops[i]; var egrec = new EGBuildRequestRecord(order[i].Data, requests[i], hop.Peer.IdentHash, hop.Peer.PublicKey); } var cipher = new BufferedBlockCipher(new CbcBlockCipher(new AesEngine())); // Dont Aes the first block for (int i = setup.Hops.Count - 2; i >= 0; --i) { var prevhop = requests[i]; cipher.Init(false, new ParametersWithIV(new KeyParameter(prevhop.ReplyKey.ToByteArray()), prevhop.ReplyIV.ToByteArray())); for (int j = i + 1; j < setup.Hops.Count; ++j) { cipher.Reset(); order[j].Process(cipher); } } for (int i = 0; i < setup.Hops.Count; ++i) { setup.Hops[i].ReplyProcessing = new ReplyProcessingInfo() { BuildRequestIndex = result.Records.IndexOf(order[i]), ReplyIV = requests[i].ReplyIV.Clone(), ReplyKey = new I2PSessionKey(requests[i].ReplyKeyBuf.Clone()) }; } return(result); }