Exemple #1
0
        internal static byte[] Encrypt(byte[] data, byte[] pubkey)
        {
            // pyelliptic.ECC(curve='secp256k1').encrypt(msg,hexToPubkey(hexPubkey))
            // curve, pubkey_x, pubkey_y, i = ECC._decode_pubkey(pubkey)
            UInt16 curve      = ECC.Secp256K1;
            string ciphername = "aes-256-cbc";
            var    ephem      = new ECC(null, null, null, null, null, curve);

            byte[] pubkey_x = new byte[32];
            byte[] pubkey_y = new byte[32];
            Buffer.BlockCopy(pubkey, 1, pubkey_x, 0, 32);
            Buffer.BlockCopy(pubkey, 33, pubkey_y, 0, 32);

            //key = sha512(ephem.raw_get_ecdh_key(pubkey_x, pubkey_y)).digest()
            byte[] key;
            using (var sha512 = new SHA512Managed())
                key = sha512.ComputeHash(ephem.raw_get_ecdh_key(pubkey_x, pubkey_y));

            byte[] key_e = new byte[32];
            byte[] key_m = new byte[32];
            Buffer.BlockCopy(key, 0, key_e, 0, 32);
            Buffer.BlockCopy(key, 32, key_m, 0, 32);

            byte[] pubkey4Write = ephem.get_pubkey();
            byte[] iv           = new byte[get_blocksize("aes-256-cbc")];
            using (var rnd = new RNGCryptoServiceProvider())
                rnd.GetBytes(iv);

            var ctx        = new Cipher(key_e, iv, true, ciphername);
            var ciphertext = ctx.Ciphering(data);
            var mac        = new HMACSHA256(key_m).ComputeHash(ciphertext);

            byte[] result = new byte[iv.Length + pubkey4Write.Length + ciphertext.Length + mac.Length];

            Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
            Buffer.BlockCopy(pubkey4Write, 0, result, iv.Length, pubkey4Write.Length);
            Buffer.BlockCopy(ciphertext, 0, result, iv.Length + pubkey4Write.Length, ciphertext.Length);
            Buffer.BlockCopy(mac, 0, result, iv.Length + pubkey4Write.Length + ciphertext.Length, mac.Length);

            //Debug.WriteLine("iv=" + iv.ToHex());
            //Debug.WriteLine("pubkey4Write=" + pubkey4Write.ToHex());

            return(result);
        }
        internal static byte[] Encrypt(byte[] data, byte[] pubkey)
        {
            // pyelliptic.ECC(curve='secp256k1').encrypt(msg,hexToPubkey(hexPubkey))
            // curve, pubkey_x, pubkey_y, i = ECC._decode_pubkey(pubkey)
            UInt16 curve = ECC.Secp256K1;
            string ciphername = "aes-256-cbc";
            var ephem = new ECC(null, null, null, null, null, curve);

            byte[] pubkey_x = new byte[32];
            byte[] pubkey_y = new byte[32];
            Buffer.BlockCopy(pubkey, 1, pubkey_x, 0, 32);
            Buffer.BlockCopy(pubkey, 33, pubkey_y, 0, 32);

            //key = sha512(ephem.raw_get_ecdh_key(pubkey_x, pubkey_y)).digest()
            byte[] key;
            using (var sha512 = new SHA512Managed())
                key = sha512.ComputeHash(ephem.raw_get_ecdh_key(pubkey_x, pubkey_y));

            byte[] key_e = new byte[32];
            byte[] key_m = new byte[32];
            Buffer.BlockCopy(key, 0, key_e, 0, 32);
            Buffer.BlockCopy(key, 32, key_m, 0, 32);

            byte[] pubkey4Write = ephem.get_pubkey();
            byte[] iv = new byte[get_blocksize("aes-256-cbc")];
            using (var rnd = new RNGCryptoServiceProvider())
                rnd.GetBytes(iv);

            var ctx = new Cipher(key_e, iv, true, ciphername);
            var ciphertext = ctx.Ciphering(data);
            var mac = new HMACSHA256(key_m).ComputeHash(ciphertext);

            byte[] result = new byte[iv.Length + pubkey4Write.Length + ciphertext.Length + mac.Length];

            Buffer.BlockCopy(iv,           0, result, 0,                                                   iv.Length);
            Buffer.BlockCopy(pubkey4Write, 0, result, iv.Length,                                           pubkey4Write.Length);
            Buffer.BlockCopy(ciphertext,   0, result, iv.Length + pubkey4Write.Length,                     ciphertext.Length);
            Buffer.BlockCopy(mac,          0, result, iv.Length + pubkey4Write.Length + ciphertext.Length, mac.Length);

            //Debug.WriteLine("iv=" + iv.ToHex());
            //Debug.WriteLine("pubkey4Write=" + pubkey4Write.ToHex());

            return result;
        }