-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenKey.c
100 lines (77 loc) · 2.28 KB
/
genKey.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Secure key/salt generator
Written in nano by Zyantos ;)
zzzyantos made it! (sorry Benjamin)
special shoutout to Zella, even tho you went ghost on me cx
Compile the script with this command please:
~# gcc genKey.c -o zkey -lcrypto
*/
#include <stdio.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include "commandHandler.h"
#include <string.h>
int genKeyPair() {
int ret = 0;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if (!ctx || EVP_PKEY_keygen_init(ctx) <= 0
|| EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 4096) <= 0
|| EVP_PKEY_keygen(ctx, &pkey) <= 0) {
EVP_PKEY_CTX_free(ctx);
return -1;
}
// Write private key
FILE *pk_file = fopen("private.pem", "wb");
if (pk_file == NULL || PEM_write_PrivateKey(pk_file, pkey, NULL, NULL, 0 , NULL, NULL) != 1) {
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(ctx);
return -1;
}
fclose(pk_file); // Close the private key file :P
printf("private.pem written\n"); // Communication is key in any relationship <3
// Write public key
FILE *pub_key_file = fopen("public.pem", "wb");
if (pub_key_file == NULL || PEM_write_PUBKEY(pub_key_file, pkey) != 1) {
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(ctx);
return -1;
}
fclose(pub_key_file); // Close the public key file :)
printf("public.pem written\n"); // Let the user know it worked :3
// Remember gotta clean up the mess afterwards ;)
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(ctx);
return 0;
}
int secret256() {
unsigned char key[32]; // 256 bits = 32 bytes
// Generate 256-bit cryptographically secure random key
if (RAND_bytes(key, sizeof(key)) != 1) {
fprintf(stderr, "Error generating random key\n");
return -1;
}
// Print the key
for (int i = 0; i < sizeof(key); i++) {
printf("%02x", key[i]);
}
}
int main(int argc, char *argv[]) {
int ret = 0; // Might as well ~\(o_o/~
char *key_type = NULL;
handle_command(argc, argv, &key_type);
if (strcmp(key_type, "rsa4096") == 0) {
ret = 0;
genKeyPair();
}
else if (strcmp(key_type, "secret256") == 0) {
ret = 0;
secret256();
printf("\n");
} else {
fprintf(stderr, "Invalid key type.\nSupport keys:\nrsa4096\nsecret256\n");
ret = -1;
}
return ret;
}