/// <summary> /// Decrypts the contents of the provided PacketStream instance. /// </summary> /// <param name="EncryptedPacket">An encrypted PacketStream instance.</param> /// <param name="DecryptionArgs">A DecryptionArgsContainer instance.</param> /// <returns>A MemoryStream instance with the decrypted data.</returns> public override MemoryStream DecryptPacket(PacketStream EncryptedPacket, DecryptionArgsContainer DecryptionArgs) { byte[] EncryptedData = new byte[EncryptedPacket.Length - (int)PacketHeaders.ENCRYPTED]; EncryptedPacket.Read(EncryptedData, 0, EncryptedData.Length); byte[] DecryptedData = StaticStaticDiffieHellman.Decrypt(m_PrivateKey, ECDiffieHellmanCngPublicKey.FromByteArray(m_PublicKey, CngKeyBlobFormat.EccPublicBlob), m_NOnce, EncryptedData); return(new MemoryStream(DecryptedData)); }
/// <summary> /// Constructs an encrypted packet. /// </summary> /// <param name="PacketID">The ID of the packet.</param> /// <param name="PacketData">The data to encrypt.</param> /// <returns>A byte array containing the ID, length of the encrypted data and the encrypted data.</returns> public override byte[] FinalizePacket(byte PacketID, byte[] PacketData) { MemoryStream PacketStream = new MemoryStream(); BinaryWriter PacketWriter = new BinaryWriter(PacketStream); PacketWriter.Write(PacketID); byte[] EncryptedData = StaticStaticDiffieHellman.Encrypt(m_PrivateKey, ECDiffieHellmanCngPublicKey.FromByteArray(m_PublicKey, CngKeyBlobFormat.EccPublicBlob), m_NOnce, PacketData); //The length of the encrypted data can be longer or smaller than the original length, //so write the length of the encrypted data. PacketWriter.Write((uint)(PacketHeaders.ENCRYPTED + EncryptedData.Length)); //Also write the length of the unencrypted data. PacketWriter.Write(PacketData.Length); PacketWriter.Flush(); PacketWriter.Write(EncryptedData); PacketWriter.Flush(); return(PacketStream.ToArray()); }