写了一个能够加密文字的小程序

#include<stdio.h>
int main()
{
FILE *p,*fp;
char a='\b',c=0;
int i=0,n,d=1;
printf("请将需要处理的字符弄到程序根目录a.txt里\n\n"); 
printf("1.加密文字   2.解密文字\n");
scanf("%d",&i);
if(i==2)
d=-1;
if((p=fopen("a.txt","r"))==NULL)
{
printf("未发现a.txt,请核对后重新启动程序");
fclose(p);
return 0;
}
fclose(p);
p=fopen("a.txt","a");
fprintf(p,"%c",c);
fclose(p);
p=fopen("a.txt","r");
printf("请输入文件密码:");
scanf("%d",&n);
n=n%98;
fp=fopen("b.txt","w");
while(1)
{
fscanf(p,"%c",&a);
if(a==c)
break;
a=a+d*n;
fprintf(fp,"%c",a);
}
fclose(p);
fclose(fp);
printf("已经将处理过的文字保存在b.txt中");
return 0;
}
闲来无事写的一个小程序。仅供娱乐。使用说明将想要加密或者解密的字符保存在a.txt中,然后自己设定或者输入加密码.会生成一个b.txt这个就是“密文”或者明码。


仅供娱乐,不喜勿喷。。。小五木
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
恺撒加密算法是一种简单的替换加密方法,通过将字母按照一定的位移进行替换来实现加密和解密。 下面是使用Python编的恺撒加密算法的程序实现: ```python def caesar_encrypt(plain_text, shift): cipher_text = "" for char in plain_text: if char.isalpha(): ascii_offset = 65 if char.isupper() else 97 shifted_char = chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset) cipher_text += shifted_char else: cipher_text += char return cipher_text def caesar_decrypt(cipher_text, shift): plain_text = "" for char in cipher_text: if char.isalpha(): ascii_offset = 65 if char.isupper() else 97 shifted_char = chr((ord(char) - ascii_offset - shift) % 26 + ascii_offset) plain_text += shifted_char else: plain_text += char return plain_text text = "Hello, Caesar!" shift = 3 encrypted_text = caesar_encrypt(text, shift) print("加密结果:", encrypted_text) decrypted_text = caesar_decrypt(encrypted_text, shift) print("解密结果:", decrypted_text) ``` 在上述程序中,`caesar_encrypt`函数用于加密明文,`caesar_decrypt`函数用于解密密文。其中,`plain_text`表示明文字符串,`shift`表示位移大小。`caesar_encrypt`函数通过遍历明文中的每一个字符,将字母按照指定的位移进行替换,最终得到密文。`caesar_decrypt`函数则对密文进行反向操作,实现解密过程。 在程序末尾,我使用了一个简单的例子进行测试。首先,将明文字符串"Hello, Caesar!"使用位移大小为3的恺撒加密算法进行加密,得到密文。然后使用同样的位移大小进行解密,得到原始的明文。最终打印出加密和解密结果。 这段程序实现了恺撒加密算法的过程,能够输入的明文进行加密和对密文进行解密。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值