1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #include "bitcoinrpc.h"
11 using namespace json_spirit;
14 Value encryptdata(const Array& params, bool fHelp)
16 if (fHelp || params.size() != 2)
18 "encryptdata <public key> <hex data>\n"
19 "Encrypt octet stream with provided public key..\n");
21 CPubKey pubKey(ParseHex(params[0].get_str()));
23 vector<unsigned char> vchEncrypted;
24 pubKey.EncryptData(ParseHex(params[1].get_str()), vchEncrypted);
26 return HexStr(vchEncrypted);
29 Value decryptdata(const Array& params, bool fHelp)
31 if (fHelp || params.size() != 2)
33 "decryptdata <novacoin address or private key> <encrypted stream>\n"
34 "Decrypt octet stream.\n");
36 EnsureWalletIsUnlocked();
38 CBitcoinAddress addr(params[0].get_str());
42 if (!pwalletMain->GetKey(keyID, key))
43 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "We have no private key for this address");
46 CBitcoinSecret vchSecret;
47 if (!vchSecret.SetString(params[0].get_str()))
48 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Provided private key is inconsistent.");
50 auto secret = vchSecret.GetSecret(fCompressed);
51 key.SetSecret(secret, fCompressed);
54 vector<unsigned char> vchDecrypted;
55 key.DecryptData(ParseHex(params[1].get_str()), vchDecrypted);
57 return HexStr(vchDecrypted);
60 Value encryptmessage(const Array& params, bool fHelp)
62 if (fHelp || params.size() != 2)
64 "encryptmessage <public key> <message string>\n"
65 "Encrypt message with provided public key.\n");
67 CPubKey pubKey(ParseHex(params[0].get_str()));
69 vector<unsigned char> vchEncrypted;
70 string strData = params[1].get_str();
71 pubKey.EncryptData(vector<unsigned char>(strData.begin(), strData.end()), vchEncrypted);
73 return EncodeBase58Check(vchEncrypted);
76 Value decryptmessage(const Array& params, bool fHelp)
78 if (fHelp || params.size() != 2)
80 "decryptmessage <novacoin address or private key> <encrypted message>\n"
81 "Decrypt message string.\n");
83 EnsureWalletIsUnlocked();
86 CBitcoinAddress addr(params[0].get_str());
90 if (!pwalletMain->GetKey(keyID, key))
91 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "We have no private key for this address");
94 CBitcoinSecret vchSecret;
95 if (!vchSecret.SetString(params[0].get_str()))
96 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Provided private key is inconsistent.");
98 auto secret = vchSecret.GetSecret(fCompressed);
99 key.SetSecret(secret, fCompressed);
102 vector<unsigned char> vchEncrypted;
103 if (!DecodeBase58Check(params[1].get_str(), vchEncrypted))
104 throw runtime_error("Incorrect string");
105 vector<unsigned char> vchDecrypted;
106 key.DecryptData(vchEncrypted, vchDecrypted);
108 return std::string((const char*)&vchDecrypted[0], vchDecrypted.size());