/// <summary> Decrypts./ </summary> /// /// auto secret = priv.get_shared_secret(pub); /// auto nonce_plus_secret = fc::sha512::hash(fc::to_string(nonce) + secret.str()); /// auto plain_text = fc::aes_decrypt( nonce_plus_secret, message ); /// auto result = memo_message::deserialize(string(plain_text.begin(), plain_text.end())); /// FC_ASSERT( result.checksum == uint32_t(digest_type::hash(result.text)._hash[0]) ); /// return result.text; /// /// <remarks> Paul, 26/10/2015. </remarks> /// /// <param name="memo"> The memo. </param> /// <param name="receiverPrivateKey"> The receiver private key. </param> /// <param name="senderPublicKey"> The sender public key. </param> /// /// <returns> A string. </returns> static public string Decrypt(GrapheneMemo memo, KeyPair receiverKeyPair) { PublicKey pub = new BitsharesPubKey(memo.from).GetBitcoinPubKey(); byte[] sharedSecret = GetSharedSecret(receiverKeyPair, pub); string ss = StringExtensions.ByteArrayToHexString(sharedSecret, true); string ps = memo.nonce.ToString() + StringExtensions.ByteArrayToHexString(Crypto.ComputeSha512(sharedSecret), true); byte[] seed = ASCIIEncoding.ASCII.GetBytes(ps); string hex = StringExtensions.ByteArrayToHexString(Crypto.ComputeSha512(seed), true); string key = hex.Substring(0, 64); string iv = hex.Substring(64, 32); byte[] data = Util.HexStringToBytes(memo.message); int len = AES.AesDecrypt(data, Util.HexStringToBytes(key), Util.HexStringToBytes(iv)); byte[] result = new byte[len - 4]; Buffer.BlockCopy(data, 4, result, 0, result.Length); string message = UTF8Encoding.UTF8.GetString(result); return(message); }
/// <summary> Decrypts./ </summary> /// /// auto secret = priv.get_shared_secret(pub); /// auto nonce_plus_secret = fc::sha512::hash(fc::to_string(nonce) + secret.str()); /// auto plain_text = fc::aes_decrypt( nonce_plus_secret, message ); /// auto result = memo_message::deserialize(string(plain_text.begin(), plain_text.end())); /// FC_ASSERT( result.checksum == uint32_t(digest_type::hash(result.text)._hash[0]) ); /// return result.text; /// /// <remarks> Paul, 26/10/2015. </remarks> /// /// <param name="memo"> The memo. </param> /// <param name="receiverPrivateKey"> The receiver private key. </param> /// <param name="senderPublicKey"> The sender public key. </param> /// /// <returns> A string. </returns> public static string Decrypt(GrapheneMemo memo, KeyPair receiverKeyPair) { PublicKey pub = new BitsharesPubKey(memo.from).GetBitcoinPubKey(); byte[] sharedSecret = GetSharedSecret(receiverKeyPair, pub); string ss = StringExtensions.ByteArrayToHexString(sharedSecret, true); string ps = memo.nonce.ToString() + StringExtensions.ByteArrayToHexString(Crypto.ComputeSha512( sharedSecret), true); byte[] seed = ASCIIEncoding.ASCII.GetBytes(ps); string hex = StringExtensions.ByteArrayToHexString(Crypto.ComputeSha512(seed), true); string key = hex.Substring(0, 64); string iv = hex.Substring(64, 32); byte[] data = Util.HexStringToBytes(memo.message); int len = AES.AesDecrypt(data, Util.HexStringToBytes(key), Util.HexStringToBytes(iv)); byte[] result = new byte[len - 4]; Buffer.BlockCopy(data, 4, result, 0, result.Length); string message = UTF8Encoding.UTF8.GetString(result); return message; }