生成一个RSA加密的C语言代码
时间: 2024-05-05 08:16:12 浏览: 12
以下是一个使用openssl库生成RSA加密的C语言代码示例:
```c
#include <openssl/rsa.h>
#include <openssl/pem.h>
int main() {
RSA *keypair = RSA_generate_key(2048, RSA_F4, NULL, NULL);
BIO *bio_private = BIO_new_file("private.pem", "w");
PEM_write_bio_RSAPrivateKey(bio_private, keypair, NULL, NULL, 0, NULL, NULL);
BIO *bio_public = BIO_new_file("public.pem", "w");
PEM_write_bio_RSAPublicKey(bio_public, keypair);
RSA_free(keypair);
return 0;
}
```
该代码生成一个2048位的RSA密钥对,并将私钥和公钥分别写入名为`private.pem`和`public.pem`的文件中。
需要注意的是,在使用openssl库时,需要在编译器中链接openssl库,例如使用gcc编译该代码时,需要加上`-lssl -lcrypto`参数。
相关问题
rsa加密解密C语言代码
以下是一个简单的C语言实现RSA加密解密的代码,其中包括生成密钥、加密和解密三个部分:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define MAX 1000
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int extend(int a, int b) {
int x1 = 1, y1 = 0, x2 = 0, y2 = 1, x, y, r, q;
while (b > 0) {
q = a / b;
r = a - q * b;
x = x1 - q * x2;
y = y1 - q * y2;
a = b;
b = r;
x1 = x2;
y1 = y2;
x2 = x;
y2 = y;
}
return x1;
}
int is_prime(int n) {
int i;
if (n <= 1) {
return 0;
}
for (i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int generate_prime() {
int p;
do {
p = rand() % MAX;
} while (!is_prime(p));
return p;
}
void generate_key(int *p, int *q, int *n, int *t, int *e, int *d) {
srand(time(NULL));
*p = generate_prime();
*q = generate_prime();
*n = (*p) * (*q);
*t = ((*p) - 1) * ((*q) - 1);
do {
*e = rand() % (*t - 2) + 2;
} while (gcd(*e, *t) != 1);
*d = extend(*e, *t);
}
int fast_pow(int a, int b, int n) {
int ans = 1;
while (b > 0) {
if (b & 1) {
ans = (ans * a) % n;
}
a = (a * a) % n;
b >>= 1;
}
return ans;
}
void encrypt(int e, int n, char *plaintext, int *ciphertext) {
int i;
for (i = 0; i < strlen(plaintext); i++) {
ciphertext[i] = fast_pow(plaintext[i], e, n);
}
}
void decrypt(int d, int n, int *ciphertext, char *plaintext) {
int i;
for (i = 0; i < strlen(plaintext); i++) {
plaintext[i] = fast_pow(ciphertext[i], d, n);
}
}
int main() {
int p, q, n, t, e, d, i;
char plaintext[MAX];
int ciphertext[MAX];
generate_key(&p, &q, &n, &t, &e, &d);
printf("公钥为:(%d, %d)\n", e, n);
printf("私钥为:%d\n", d);
printf("请输入要加密的明文:");
scanf("%s", plaintext);
encrypt(e, n, plaintext, ciphertext);
printf("密文为:");
for (i = 0; i < strlen(plaintext); i++) {
printf("%d ", ciphertext[i]);
}
printf("\n请输入正确的密钥,密钥正确将解密上面的密文:");
scanf("%d", &d);
if (d == e) {
decrypt(d, n, ciphertext, plaintext);
printf("明文为:%s\n", plaintext);
} else {
printf("密钥错误,无法解密!\n");
}
return 0;
}
```
该代码实现了RSA加密解密的基本功能,包括生成密钥、加密和解密。用户可以输入要加密的明文,程序将输出密文,并要求用户输入正确的密钥进行解密。如果密钥正确,程序将输出解密后的明文,否则将输出密钥错误的提示信息。
rsa加密算法代码C语言
以下是一个简单的RSA加密算法C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#define RSA_KEY_SIZE 2048
int main() {
// 生成RSA密钥对
RSA *rsa = RSA_generate_key(RSA_KEY_SIZE, RSA_F4, NULL, NULL);
if (!rsa) {
printf("failed to generate RSA key pair\n");
return -1;
}
// 将公钥和私钥分别保存到文件中
FILE *fp = fopen("public_key.pem", "w");
if (!fp) {
printf("failed to open file public_key.pem\n");
return -1;
}
PEM_write_RSAPublicKey(fp, rsa);
fclose(fp);
fp = fopen("private_key.pem", "w");
if (!fp) {
printf("failed to open file private_key.pem\n");
return -1;
}
PEM_write_RSAPrivateKey(fp, rsa, NULL, NULL, 0, NULL, NULL);
fclose(fp);
// 加密和解密测试
const char *message = "Hello World!";
int message_len = strlen(message);
// 加密
int encrypted_size = RSA_size(rsa);
unsigned char *encrypted = malloc(encrypted_size);
int result = RSA_public_encrypt(message_len, (unsigned char *)message, encrypted, rsa, RSA_PKCS1_PADDING);
if (result == -1) {
printf("failed to encrypt message\n");
return -1;
}
// 解密
int decrypted_size = RSA_size(rsa);
unsigned char *decrypted = malloc(decrypted_size);
result = RSA_private_decrypt(result, encrypted, decrypted, rsa, RSA_PKCS1_PADDING);
if (result == -1) {
printf("failed to decrypt message\n");
return -1;
}
printf("Original message: %s\n", message);
printf("Decrypted message: %s\n", decrypted);
// 释放RSA密钥对和加解密缓冲区
RSA_free(rsa);
free(encrypted);
free(decrypted);
return 0;
}
```
该代码使用OpenSSL库,首先生成一个RSA密钥对,然后将公钥和私钥保存到文件中。接着,使用公钥对一条消息进行加密,再使用私钥对密文进行解密,并输出原始消息和解密后的消息。最后,释放RSA密钥对和加解密缓冲区。