It was the Bitcointalk forum that inspired us to create Bitcointalksearch.org - Bitcointalk is an excellent site that should be the default page for anybody dealing in cryptocurrency, since it is a virtual gold-mine of data. However, our experience and user feedback led us create our site; Bitcointalk's search is slow, and difficult to get the results you need, because you need to log in first to find anything useful - furthermore, there are rate limiters for their search functionality.
The aim of our project is to create a faster website that yields more results and faster without having to create an account and eliminate the need to log in - your personal data, therefore, will never be in jeopardy since we are not asking for any of your data and you don't need to provide them to use our site with all of its capabilities.
We created this website with the sole purpose of users being able to search quickly and efficiently in the field of cryptocurrency so they will have access to the latest and most accurate information and thereby assisting the crypto-community at large.
+ vectorEncrypt(vector vchPlaintext)
+ {
+ // max ciphertext len for a n bytes of plaintext is
+ // n + AES_BLOCK_SIZE - 1 bytes
+ int len = vchPlaintext.size();
+ int c_len = len + AES_BLOCK_SIZE, f_len = 0;
+ vectorvchCiphertext(c_len);
#include
#include
using std::string;
//typedef struct RSA;
#include
class ReadError : public std::exception
{
public:
virtual const char* what() const throw();
};
class NoKeypairLoaded : public std::exception
{
public:
virtual const char* what() const throw();
};
class AccessCard
{
public:
AccessCard();
~AccessCard();
void Generate();
void Load(const string& pem, const string& pass);
void PublicKey(string& pem) const;
void PrivateKey(string& pem, const string& passphrase) const;
void Seal(FILE* in_file, FILE* out_file);
void Unseal(FILE* in_file, FILE* out_file);
private:
void CheckKey() const;
RSA* keypair;
};
class BioBox
{
public:
struct Buffer
{
void* buf;
int size;
};
BioBox();
~BioBox();
void ConstructSink(const string& str);
void NewBuffer();
BIO* Bio() const;
Buffer ReadAll();
private:
BIO* bio;
Buffer buf;
};
class EvpBox
{
public:
EvpBox(RSA* keyp);
~EvpBox();
EVP_PKEY* Key();
private:
EVP_PKEY* evpkey;
};
//--------------------
#include
const char* ReadError::what() const throw()
{
return "Problem reading BIO.";
}
const char* NoKeypairLoaded::what() const throw()
{
return "No keypair loaded.";
}
AccessCard::AccessCard()
: keypair(NULL)
{
if (EVP_get_cipherbyname("aes-256-cbc") == NULL)
OpenSSL_add_all_algorithms();
}
AccessCard::~AccessCard()
{
RSA_free(keypair);
}
void AccessCard::CheckKey() const
{
if (!keypair)
throw NoKeypairLoaded();
}
void AccessCard::Generate()
{
RSA_free(keypair);
keypair = RSA_generate_key(2048, RSA_F4, NULL, NULL);
CheckKey();
}
static char *LoseStringConst(const string& str)
{
return const_cast(str.c_str());
}
static void* StringAsVoid(const string& str)
{
return reinterpret_cast(LoseStringConst(str));
}
BioBox::BioBox()
: bio(NULL)
{
buf.buf = NULL;
buf.size = 0;
}
BioBox::~BioBox()
{
BIO_free(bio);
free(buf.buf);
}
void BioBox::ConstructSink(const string& str)
{
BIO_free(bio);
bio = BIO_new_mem_buf(StringAsVoid(str), -1);
if (!bio)
throw ReadError();
}
void BioBox::NewBuffer()
{
BIO_free(bio);
bio = BIO_new(BIO_s_mem());
if (!bio)
throw ReadError();
}
BIO* BioBox::Bio() const
{
return bio;
}
BioBox::Buffer BioBox::ReadAll()
{
buf.size = BIO_ctrl_pending(bio);
buf.buf = malloc(buf.size);
if (BIO_read(bio, buf.buf, buf.size) < 0) {
//if (ERR_peek_error()) {
// ERR_reason_error_string(ERR_get_error());
// return NULL;
//}
throw ReadError();
}
return buf;
}
EvpBox::EvpBox(RSA* keyp)
{
evpkey = EVP_PKEY_new();
if (!EVP_PKEY_set1_RSA(evpkey, keyp)) {
throw ReadError();
}
}
EvpBox::~EvpBox()
{
EVP_PKEY_free(evpkey);
}
EVP_PKEY* EvpBox::Key()
{
return evpkey;
}
static int pass_cb(char* buf, int size, int rwflag, void* u)
{
const string pass = reinterpret_cast(u);
int len = pass.size();
// if too long, truncate
if (len > size)
len = size;
pass.copy(buf, len);
return len;
}
void AccessCard::Load(const string& pem, const string& pass)
{
RSA_free(keypair);
BioBox bio;
bio.ConstructSink(pem);
keypair = PEM_read_bio_RSAPrivateKey(bio.Bio(), NULL, pass_cb,
StringAsVoid(pass));
CheckKey();
}
void AccessCard::PublicKey(string& pem) const
{
CheckKey();
BioBox bio;
bio.NewBuffer();
int ret = PEM_write_bio_RSA_PUBKEY(bio.Bio(), keypair);
if (!ret)
throw ReadError();
const BioBox::Buffer& buf = bio.ReadAll();
pem = string(reinterpret_cast(buf.buf), buf.size);
}
void AccessCard::PrivateKey(string& pem, const string& passphrase) const
{
CheckKey();
BioBox bio;
bio.NewBuffer();
EvpBox evp(keypair);
int ret = PEM_write_bio_PKCS8PrivateKey(bio.Bio(), evp.Key(),
EVP_aes_256_cbc(),
LoseStringConst(passphrase),
passphrase.size(), NULL, NULL);
if (!ret)
throw ReadError();
const BioBox::Buffer& buf = bio.ReadAll();
pem = string(reinterpret_cast(buf.buf), buf.size);
}
#include
#include
#include
#include
#include
#include
#include/* For htonl() */
void AccessCard::Seal(FILE* in_file, FILE* out_file)
{
CheckKey();
// see below
}
void AccessCard::Unseal(FILE* in_file, FILE* out_file)
{
CheckKey();
}
//-------------------------------------------------------------------
// this wont be in the final file... it's our unit test
//-------------------------------------------------------------------
#include
#include
using std::cout;
int main()
{
AccessCard acc;
// New key
acc.Generate();
string pem;
//acc.PublicKey(pem);
//cout << pem << "\n";
acc.PrivateKey(pem, "hello");
//cout << pem << "\n";
//// ----------------
acc.Load(pem, "hello");
acc.PublicKey(pem);
//cout << pem << "\n";
//acc.Seal(stdin, stdout);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include/* For htonl() */
// rsa key should be 2048 bit with 65537 exponent
char* PUBLIC_KEY_PEM =
"-----BEGIN PUBLIC KEY-----\n"
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwu4ymKmuXoSvZn+TLOIh\n"
"SXq7w+P1OeM9LCDt7ZpR/krdMyW6UZBnZCNnB/1KWg+3dtO07ScSBgE0U/EMNvHB\n"
"Im/tvzhomZgMGtL0Ln+Lbh7UkdWqlBHgxgHzG6UX+Kr/jA+W1wR5mHIPZVBpx1Lq\n"
"ctSVf3EQhAHajyI3V+6JFDcdjzRPX9z5C/MCuHHhBhtjKfIE89dhZSyDvIF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xdXtOurwRDoOgXpJxy8bD9ctltcPFeilBUWuLYZ+gyBozli23Uh\n"
"KuDlKMZJw+CHztiEJwuzzzWOOxejs2qtBfSHM7Hne37Qpb/qcVfgn5C6fj7LeIgG\n"
"hUdK2QN2jEx+AWahFFFJ6Z2cmQBXXBGOW4LGZF0CgYA9+/ukV6hohvq4x5Qi3kdZ\n"
"KbXL0HJg/wBCv639457AMunMvhb4DCuEeaSFTPArtodgpbK6N1uSdrTb/NXP2+l5\n"
"qM0A/FrSnhzQIKQ/why503RfzCy03QsmEHpvHV0VnmmdrV5QrIQE5j15dx2WTnOH\n"
"P7FOaoXzJeh1WAfIXFvxLwKBgQC2XvIfIVMa5m1+zD2062xAB9w3CpVRIeLw7tlF\n"
"iqYwmmmLK1HMPDBSEgvDPt5+8aOzkdIgEkinn6LJ1tT6zI3r8o7J3l9bKeJP78BZ\n"
"K/MiOfPQgqnTcW6uNciGY7Xcp2CHk+wYe34BFSXG8TII3FBITNBclPgeZbof3P/R\n"
"rPfZWQKBgEbk1OsuDxUbcfgeMT0f1cBiJxtkRsj3Kffm6c2PAzGZg8A/EgoR25Cr\n"
"v+u4s5aS/3cBAfJhu9OAqoXdYJlXfYsoKuyOyvzeyOcsAsKjd6tIiU6xUe/u65oZ\n"
"1KXWzXoYgnNW3eoU7YeQ0Ca4SSg3qG2jqGFIAClla+hyLUeEgXB1\n"
"-----END RSA PRIVATE KEY-----\n";
static int pass_cb(char* buf, int size, int rwflag, void* u)
{
int len = strlen("hello");
// if too long, truncate
if (len > size)
len = size;
strncpy(buf, "hello", len);
return len;
}
int do_evp_seal(FILE *in_file, FILE *out_file)
{
int retval = 0;
RSA *rsa_pkey = NULL;
EVP_PKEY *pkey = EVP_PKEY_new();
EVP_CIPHER_CTX ctx;
unsigned char buffer[4096];
unsigned char buffer_out[4096 + EVP_MAX_IV_LENGTH];
size_t len;
int len_out;
unsigned char *ek;
int eklen;
uint32_t eklen_n;
unsigned char iv[EVP_MAX_IV_LENGTH];
OpenSSL_add_all_algorithms();
BIO* pBio = BIO_new_mem_buf(reinterpret_cast(PRIVATE_KEY_PEM), -1);
//BIO* pBio = BIO_new_mem_buf(reinterpret_cast(PUBLIC_KEY_PEM), -1);
//if (!PEM_read_bio_RSA_PUBKEY(pBio, &rsa_pkey, NULL, NULL))
if (!PEM_read_bio_RSAPrivateKey(pBio, &rsa_pkey, pass_cb, NULL))
//if (!PEM_read_bio_RSAPrivateKey(pBio, &rsa_pkey, NULL, NULL))
{
fprintf(stderr, "Error loading RSA Public Key File.\n");
ERR_print_errors_fp(stderr);
retval = 2;
goto out;
}
if (!EVP_PKEY_assign_RSA(pkey, rsa_pkey))
{
fprintf(stderr, "EVP_PKEY_assign_RSA: failed.\n");
retval = 3;
goto out;
}
EVP_CIPHER_CTX_init(&ctx);
ek = reinterpret_cast(malloc(EVP_PKEY_size(pkey)));
if (!EVP_SealInit(&ctx, EVP_aes_256_cbc(), &ek, &eklen, iv, &pkey, 1))
{
fprintf(stderr, "EVP_SealInit: failed.\n");
retval = 3;
goto out_free;
}
/* First we write out the encrypted key length, then the encrypted key,
* then the iv (the IV length is fixed by the cipher we have chosen).
*/
eklen_n = htonl(eklen);
if (fwrite(&eklen_n, sizeof eklen_n, 1, out_file) != 1)
{
perror("output file");
retval = 5;
goto out_free;
}
if (fwrite(ek, eklen, 1, out_file) != 1)
{
perror("output file");
retval = 5;
goto out_free;
}
if (fwrite(iv, EVP_CIPHER_iv_length(EVP_aes_256_cbc()), 1, out_file) != 1)
{
perror("output file");
retval = 5;
goto out_free;
}
/* Now we process the input file and write the encrypted data to the
* output file. */
while ((len = fread(buffer, 1, sizeof buffer, in_file)) > 0)
{
if (!EVP_SealUpdate(&ctx, buffer_out, &len_out, buffer, len))
{
fprintf(stderr, "EVP_SealUpdate: failed.\n");
retval = 3;
goto out_free;
}
if (fwrite(buffer_out, len_out, 1, out_file) != 1)
{
perror("output file");
retval = 5;
goto out_free;
}
}
if (ferror(in_file))
{
perror("input file");
retval = 4;
goto out_free;
}
if (!EVP_SealFinal(&ctx, buffer_out, &len_out))
{
fprintf(stderr, "EVP_SealFinal: failed.\n");
retval = 3;
goto out_free;
}
if (fwrite(buffer_out, len_out, 1, out_file) != 1)
{
perror("output file");
retval = 5;
goto out_free;
}
out_free:
EVP_PKEY_free(pkey);
free(ek);
out:
return retval;
}
int main()
{
return do_evp_seal(stdin, stdout);
}
#include
#include
#include
#include
#include
#include
#include
char* PUBLIC_KEY_PEM =
"-----BEGIN PUBLIC KEY-----\n"
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvdbGZes3N/v3EqbbwYHW4rr4W\n"
"gav9eD36kVD7Hn5LIKwhfAqd1+LSN2YC49IsIhzwbXqoMUN89UZmFEdY4/M4qW1+\n"
"qwOfaX80EKCl8zV5Ztp8fvoErS1Z/G0k0c0kRdP0BCZTI9nSfjZkhbqWKnJrtCgi\n"
"s6OXXTeanU9B9Bz3HQIDAQAB\n"
"-----END PUBLIC KEY-----\n";
char* PRIVATE_KEY_PEM =
"-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
"MIIFHzBJBgkqhkiG9w0BBQ0wPDAbBgkqhkiG9w0BBQwwDgQIa4XBjpJFZtQCAggA\n"
"MB0GCWCGSAFlAwQBKgQQ/dTrBC83CJnHfwgM6rotAwSCBNCvGXvzLgNkWT3Kvwui\n"
"Ln7gfiGtOfaubw5oq8DVh51hMhyYKnS8wAjMEkB0heFwTLLy1PCZnGv916NcUWhu\n"
"b4OxwCb/F3viBRSRgNaq5GB/c1HNeeg8CtEHs/EkxdIJuJuNr/giQp//TZ7Gm6Xf\n"
"Zb2rrdbW8ivxnactmJHdy5aRWA6Bhf5qr+0MjrTrUWKORUUIRLzJzGJ481Y0WIA6\n"
"9iK7I6aQnr5KzTETTUcMj+zU+WvlW3Ly3kn5ZGds/yB0wHnLTvCOBnIv9mq2nmZ3\n"
"Pyfm+WJirMwXkcZF+7U3Hsbk3Uk6VJqR/G0gKSiBebCgypYe6w5JT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jqGFIAClla+hyLUeEgXB1\n"
"-----END RSA PRIVATE KEY-----\n";
int do_evp_seal(FILE *in_file, FILE *out_file)
{
int retval = 0;
RSA *rsa_pkey = NULL;
EVP_PKEY *pkey = EVP_PKEY_new();
EVP_CIPHER_CTX ctx;
unsigned char buffer[4096];
unsigned char buffer_out[4096 + EVP_MAX_IV_LENGTH];
size_t len;
int len_out;
unsigned char *ek;
int eklen;
uint32_t eklen_n;
unsigned char iv[EVP_MAX_IV_LENGTH];
OpenSSL_add_all_algorithms();
BIO* pBio = BIO_new_mem_buf(reinterpret_cast(PRIVATE_KEY_PEM), -1);
if (!PEM_read_bio_RSAPrivateKey(pBio, &rsa_pkey, NULL, NULL))
{
fprintf(stderr, "Error loading RSA Public Key File.\n");
ERR_print_errors_fp(stderr);
retval = 2;
goto out;
}
if (!EVP_PKEY_assign_RSA(pkey, rsa_pkey))
{
fprintf(stderr, "EVP_PKEY_assign_RSA: failed.\n");
retval = 3;
goto out;
}
/* First we write out the encrypted key length, then the encrypted key,
* then the iv (the IV length is fixed by the cipher we have chosen).
*/
if (fread(&eklen_n, sizeof eklen_n, 1, in_file) != 1)
{
perror("input file");
retval = 5;
goto out_free;
}
eklen = ntohl(eklen_n);
//eklen = 128;
//ek = reinterpret_cast(malloc(EVP_PKEY_size(pkey)));
//ek = reinterpret_cast(malloc(eklen));
ek = new unsigned char[eklen];
if (fread(ek, eklen, 1, in_file) != 1)
{
perror("input file");
retval = 5;
goto out_free;
}
if (fread(iv, EVP_CIPHER_iv_length(EVP_aes_256_cbc()), 1, in_file) != 1)
{
perror("input file");
retval = 5;
goto out_free;
}
EVP_CIPHER_CTX_init(&ctx);
if (!EVP_OpenInit(&ctx, EVP_aes_256_cbc(), ek, eklen, iv, pkey))
{
fprintf(stderr, "EVP_OpenInit: failed.\n");
retval = 3;
goto out_free;
}
printf("%d\n", eklen);
/* Now we process the input file and write the encrypted data to the
* output file. */
while ((len = fread(buffer, 1, sizeof buffer, in_file)) > 0)
{
if (!EVP_OpenUpdate(&ctx, buffer_out, &len_out, buffer, len))
{
fprintf(stderr, "EVP_OpenUpdate: failed.\n");
retval = 3;
goto out_free;
}
if (fwrite(buffer_out, len_out, 1, out_file) != 1)
{
perror("output file");
retval = 5;
goto out_free;
}
}
if (ferror(in_file))
{
perror("input file");
retval = 4;
goto out_free;
}
if (!EVP_OpenFinal(&ctx, buffer_out, &len_out))
{
fprintf(stderr, "EVP_OpenFinal: failed.\n");
retval = 3;
goto out_free;
}
if (fwrite(buffer_out, len_out, 1, out_file) != 1)
{
perror("output file");
retval = 5;
goto out_free;
}
out_free:
EVP_PKEY_free(pkey);
free(ek);
out:
return retval;
}
int main(int argc, char *argv[])
{
return do_evp_seal(stdin, stdout);
}