简单的加密解密程序范例

这是一个简单的读取文件,异或后,再输出到另一文件中,起到一个简单的加密,

#include<stdlib.h>

#include<stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp,*pp;
char buf[123]="";
int i=0;
int j,k,l;
long long num=0;
char m[123]="";
char s[123]="";
printf("please input convert filename:");
scanf("%s",s);
getchar();


fp=fopen(s,"r");
pp=fopen("1.txt","w");
int n=1;
while(1){
n=fread(buf,1,1,fp);
if(n<=0)
break;
fprintf(pp,"%c",(buf[0]^0xff));//0xff  就是秘钥,也可以是任意的字母或者数字的组合

}
printf("convert %s is OK!\n",s);
printf("convert creat \"1.txt\" !");
exit(0);
return 0;

}

这是一个简单的读取文件,再一次异或同一个秘钥后,再输出到另一文件中,起到一个简单的解密,

#include<stdlib.h>
#include<stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp,*pp;
char buf[123]="";
int i=0;
int j=0;
char m[123]="";
char s[123]="";
printf("please input convert file name:");
scanf("%s",s);
getchar();
fp=fopen(s,"r");
pp=fopen("1.txt","w");
int n=1;
while(1){
n=fread(buf,1,1,fp);
if(n<=0)
break;
buf[0]=buf[0]^0xff;//连续两次异或同一个秘钥,就可以起到解密的作用
fwrite(&buf[0],1,1,pp);
}
return 0;
}


RSA加密解密算法是一种非对称加密算法,它是由Ron Rivest、Adi Shamir和Leonard Adleman在1977年提出的。RSA算法的安全性基于大数分解的困难性,即将一个大数分解成两个质数的乘积。RSA算法的加密过程是将明文先转换成数字,然后使用公钥进行加密,得到密文;解密过程是使用私钥对密文进行解密,得到明文。RSA算法在数字签名、密钥协商等领域有着广泛的应用。 以下是一个简单的C语言实现RSA加密解密算法的范例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/rsa.h> #include <openssl/pem.h> #define PUBLIC_KEY_FILE "public_key.pem" #define PRIVATE_KEY_FILE "private_key.pem" int main() { RSA *rsa = NULL; FILE *fp = NULL; char *plain_text = "Hello, RSA!"; char *cipher_text = NULL; char *decrypted_text = NULL; int plain_text_len = strlen(plain_text); int cipher_text_len = 0; int decrypted_text_len = 0; // 生成RSA密钥对 rsa = RSA_generate_key(1024, RSA_F4, NULL, NULL); if (rsa == NULL) { printf("Failed to generate RSA key pair!\n"); return -1; } // 将公钥入文件 fp = fopen(PUBLIC_KEY_FILE, "w"); if (fp == NULL) { printf("Failed to open public key file!\n"); return -1; } PEM_write_RSAPublicKey(fp, rsa); fclose(fp); // 将私钥入文件 fp = fopen(PRIVATE_KEY_FILE, "w"); if (fp == NULL) { printf("Failed to open private key file!\n"); return -1; } PEM_write_RSAPrivateKey(fp, rsa, NULL, NULL, 0, NULL, NULL); fclose(fp); // 加密明文 cipher_text_len = RSA_size(rsa); cipher_text = (char *)malloc(cipher_text_len); memset(cipher_text, 0, cipher_text_len); RSA_public_encrypt(plain_text_len, (unsigned char *)plain_text, (unsigned char *)cipher_text, rsa, RSA_PKCS1_PADDING); // 解密密文 decrypted_text_len = RSA_size(rsa); decrypted_text = (char *)malloc(decrypted_text_len); memset(decrypted_text, 0, decrypted_text_len); RSA_private_decrypt(cipher_text_len, (unsigned char *)cipher_text, (unsigned char *)decrypted_text, rsa, RSA_PKCS1_PADDING); // 输出结果 printf("Plain text: %s\n", plain_text); printf("Cipher text: "); for (int i = 0; i < cipher_text_len; i++) { printf("%02x", (unsigned char)cipher_text[i]); } printf("\n"); printf("Decrypted text: %s\n", decrypted_text); // 释放资源 RSA_free(rsa); free(cipher_text); free(decrypted_text); return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值