public byte[] Encrypt(PublicKey recipientPublicKey, byte[] plaintext, byte[] macData) { byte[] iv = _cryptoRandom.GenerateRandomBytes(KeySize / 8); PrivateKey ephemeralPrivateKey = _keyGenerator.Generate(); IIesEngine iesEngine = MakeIesEngine(true, recipientPublicKey, ephemeralPrivateKey, iv); try { byte[] cipher = iesEngine.ProcessBlock(plaintext, 0, plaintext.Length, macData); MemoryStream memoryStream = new MemoryStream(); memoryStream.Write(ephemeralPrivateKey.PublicKey.PrefixedBytes, 0, ephemeralPrivateKey.PublicKey.PrefixedBytes.Length); memoryStream.Write(iv, 0, iv.Length); memoryStream.Write(cipher, 0, cipher.Length); return(memoryStream.ToArray()); } catch (InvalidCipherTextException) { throw; } catch (IOException) { throw; } }
private byte[] Decrypt(PublicKey ephemeralPublicKey, PrivateKey privateKey, byte[] iv, byte[] ciphertextBody, byte[] macData) { IIesEngine iesEngine = MakeIesEngine(false, ephemeralPublicKey, privateKey, iv); return(iesEngine.ProcessBlock(ciphertextBody, 0, ciphertextBody.Length, macData)); }