Beispiel #1
0
        public Packet MessageDecrypt(Packet outer)
        {
            byte[] remoteKeyData      = outer.Body.Take(21).ToArray();
            byte[] ivData             = outer.Body.Skip(21).Take(4).ToArray();
            byte[] innerEncryptedData = outer.Body.Skip(25).Take(outer.Body.Length - 29).ToArray();

            // Decode the body
            ECKeyPair remoteEphemeralKeys = ECKeyPair.LoadKeys(SecNamedCurves.GetByName("secp160r1"), remoteKeyData, null);

            var idAgreement = ECDHAgree(remoteEphemeralKeys.PublicKey, Key.PrivateKey);
            var agreedHash  = Helpers.SHA256Hash(Helpers.ToByteArray(idAgreement, 20));
            var aesKey      = Helpers.FoldOnce(agreedHash);

            // Pad out the IV
            byte[] aesIV = new byte[16];
            Array.Clear(aesIV, 0, 16);
            Buffer.BlockCopy(ivData, 0, aesIV, 0, 4);

            // Decrypt it
            var cipher     = new BufferedBlockCipher(new SicBlockCipher(new AesFastEngine()));
            var parameters = new ParametersWithIV(new KeyParameter(aesKey), aesIV);

            cipher.Init(false, parameters);
            byte[] decryptedBody = new byte[innerEncryptedData.Length];
            var    offset        = cipher.ProcessBytes(innerEncryptedData, decryptedBody, 0);

            cipher.DoFinal(decryptedBody, offset);

            Packet outPacket = Packet.DecodePacket(decryptedBody);

            return(outPacket);
        }
Beispiel #2
0
 public void LoadKeys(byte[] publicKeyData, byte[] privateKeyData)
 {
     Key = ECKeyPair.LoadKeys(SecNamedCurves.GetByName("secp160r1"), publicKeyData, privateKeyData);
 }