コード例 #1
0
        /// <summary>
        /// Encrypt the bytes with Shared secret
        /// </summary>
        /// <param name="inputBytes">Array of the bytes to encrypt</param>
        /// <param name="bobAddress">Receiver Neblio Address</param>
        /// <param name="secret">Neblio Private Key of the Sender in the form of BitcoinSecret</param>
        /// <param name="sharedkey">shared key</param>
        /// <returns></returns>
        public static async Task <(bool, byte[])> EncryptBytesWithSharedSecret(byte[] inputBytes, string bobAddress, BitcoinSecret secret, string sharedkey = "")
        {
            if (inputBytes == null || inputBytes.Length == 0)
            {
                throw new Exception("Input cannot be empty or null.");
            }
            if (string.IsNullOrEmpty(bobAddress))
            {
                throw new Exception("Partner Address cannot be empty or null.");
            }
            if (secret == null)
            {
                throw new Exception("Input secret cannot null.");
            }

            (bool, string)key = (false, "");
            if (string.IsNullOrEmpty(sharedkey))
            {
                key = await GetSharedSecret(bobAddress, secret);

                if (!key.Item1)
                {
                    return(false, null);
                }
            }
            else
            {
                key.Item2 = sharedkey;
            }

            try
            {
                var ebytes = SymetricProvider.EncryptBytes(key.Item2, inputBytes);
                return(true, ebytes);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot encrypt bytes. " + ex.Message);
                throw new Exception("Cannot encrypt bytes. " + ex.Message);
            }
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static byte[] EncryptBytesWithPublicKey(byte[] message, PubKey key)
        {
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            var ephemeral     = new Key();
            var sharedKey     = NBitcoin.Crypto.Hashes.SHA512(key.GetSharedPubkey(ephemeral).ToBytes());
            var iv            = sharedKey.SafeSubarray(0, 16);
            var encryptionKey = sharedKey.SafeSubarray(16, 16);
            var hashingKey    = sharedKey.SafeSubarray(32);

            //var aes = new AesBuilder().SetKey(encryptionKey).SetIv(iv).IsUsedForEncryption(true).Build();
            //var cipherText = aes.Process(message, 0, message.Length);
            var cipherText           = SymetricProvider.EncryptBytes(encryptionKey, message, iv);
            var ephemeralPubkeyBytes = ephemeral.PubKey.ToBytes();
            var encrypted            = Encoders.ASCII.DecodeData("BIE1").Concat(ephemeralPubkeyBytes, cipherText);
            var hashMAC = HMACSHA256(hashingKey, encrypted);

            return(encrypted.Concat(hashMAC));
        }