-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkb2pkh.cpp
63 lines (53 loc) · 1.86 KB
/
pkb2pkh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/obj_mac.h>
#include <fstream>
#include <iostream>
#include <iomanip>
int main() {
const int keySize = 32; // Bitcoin private keys are 32 bytes (256 bits)
unsigned char buffer[keySize];
// Open the private key file in binary mode
std::ifstream inFile("random_byte.bin", std::ios::binary);
if (!inFile) {
std::cerr << "Failed to open random_byte.bin for reading." << std::endl;
return 1;
}
// Read the binary data from the file
inFile.read(reinterpret_cast<char*>(buffer), keySize);
inFile.close();
if (inFile.gcount() != keySize) {
std::cerr << "Error: random_byte.bin does not contain a valid 32-byte random byte." << std::endl;
return 1;
}
// Convert the buffer to a BIGNUM (big integer) for use as a private key
BIGNUM* privKeyBN = BN_bin2bn(buffer, keySize, nullptr);
if (!privKeyBN) {
std::cerr << "Failed to convert buffer to BIGNUM." << std::endl;
return 1;
}
// Create an EC_KEY structure to hold the private key for the secp256k1 curve
EC_KEY* ecKey = EC_KEY_new_by_curve_name(NID_secp256k1);
if (!ecKey) {
std::cerr << "Failed to create EC_KEY object." << std::endl;
BN_free(privKeyBN);
return 1;
}
// Set the private key
if (EC_KEY_set_private_key(ecKey, privKeyBN) != 1) {
std::cerr << "Failed to set private key." << std::endl;
EC_KEY_free(ecKey);
BN_free(privKeyBN);
return 1;
}
// Print the private key in hexadecimal format
std::cout << "Bitcoin Private Key (Hex): ";
for (int i = 0; i < keySize; ++i) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)buffer[i];
}
std::cout << std::endl;
// Clean up
EC_KEY_free(ecKey);
BN_free(privKeyBN);
return 0;
}