Ejemplo n.º 1
0
        public byte[] Encrypt(PublicKey recipientPublicKey, byte[] plaintext, byte[] macData)
        {
            byte[]     iv = _cryptoRandom.GenerateRandomBytes(KeySize / 8);
            PrivateKey ephemeralPrivateKey = new PrivateKey(_cryptoRandom.GenerateRandomBytes(32));

            ECPublicKeyParameters  publicKeyParameters           = BouncyCrypto.WrapPublicKey(recipientPublicKey);
            ECPrivateKeyParameters ephemeralPrivateKeyParameters = BouncyCrypto.WrapPrivateKey(ephemeralPrivateKey);
            EthereumIesEngine      iesEngine = MakeIesEngine(true, publicKeyParameters, ephemeralPrivateKeyParameters, 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;
            }
        }
Ejemplo n.º 2
0
        private byte[] Decrypt(PublicKey ephemeralPublicKey, PrivateKey privateKey, byte[] iv, byte[] ciphertextBody, byte[] macData)
        {
            AesEngine aesFastEngine = new AesEngine();

            EthereumIesEngine iesEngine = new EthereumIesEngine(
                new ECDHBasicAgreement(),
                new ConcatKdfBytesGenerator(new Sha256Digest()),
                new HMac(new Sha256Digest()),
                new Sha256Digest(),
                new BufferedBlockCipher(new SicBlockCipher(aesFastEngine)));

            IesParameters    iesParameters    = new IesWithCipherParameters(new byte[] { }, new byte[] { }, KeySize, KeySize);
            ParametersWithIV parametersWithIV = new ParametersWithIV(iesParameters, iv);

            ECPrivateKeyParameters privateKeyParameters = BouncyCrypto.WrapPrivateKey(privateKey);
            ECPublicKeyParameters  publicKeyParameters  = BouncyCrypto.WrapPublicKey(ephemeralPublicKey);

            iesEngine.Init(false, privateKeyParameters, publicKeyParameters, parametersWithIV);

            return(iesEngine.ProcessBlock(ciphertextBody, 0, ciphertextBody.Length, macData));
        }