一个简单的恺撒加密程序

 

#include<iostream>
#include<fstream>
void main(void)

 char strch,ch;
   int i,x;
 ifstream readfile;
 ofstream writefile; 
 readfile.open("1.txt",ios::in | ios::nocreate);  
if(!readfile)  

cerr<<"cannot open thie file for input"<<endl;        
exit(0);    }  
 writefile.open("2.txt",ios::in | ios::nocreate);
 if(!writefile)
 {  
cerr<<"cannot open the file for output"<<endl;  
exit(0);   } 
while(!readfile.eof()) 
{     
ch=readfile.get();      
x=static_cast<int>(ch);   
x=(x+3)%128;
strch=static_cast<char>(x);  
 writefile.put(strch);        
}
}

 

凯撒密码加解密程序(C语言) 2009年09月30日 星期三 13:21 1、程序结构化,用函数分别实现 2、对文件的加密,解密输出到文件 #include #include void menu()/*菜单,1.加密 2.解密 3.退出*/ { clrscr(); printf("\n==============================================================================="); printf("\n1.Encrypt the file"); printf("\n2.Decrypt the file"); printf("\n3.Quit\n"); printf("===============================================================================\n"); printf("Please select a item:"); return; } char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/ { while(ch>='A'&&ch='a'&&ch<='z') { return ('a'+(ch-'a'+n)%26); } return ch; } main() { int i,n; char ch0,ch1; FILE *in,*out; char infile[10],outfile[10]; textbackground(RED); textcolor(LIGHTGREEN); clrscr(); menu(); ch0=getch(); while(ch0!='3') { if(ch0=='1') { clrscr(); printf("\nPlease input the infile:"); scanf("%s",infile);/*输入需要加密的文件名*/ if((in=fopen(infile,"r"))==NULL) { printf("Can not open the infile!\n"); printf("Press any key to exit!\n"); getch(); exit(0); } printf("Please input the key:"); scanf("%d",&n);/*输入加密密码*/ printf("Please input the outfile:"); scanf("%s",outfile);/*输入加密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL) { printf("Can not open the outfile!\n"); printf("Press any key to exit!\n"); fclose(in); getch(); exit(0); } while(!feof(in))/*加密*/ { fputc(encrypt(fgetc(in),n),out); } printf("\nEncrypt is over!\n"); fclose(in); fclose(out); sleep(1); } if(ch0=='2') { clrscr(); printf("\nPlease input the infile:"); scanf("%s",infile);/*输入需要解密的文件名*/ if((in=fopen(infile,"r"))==NULL) { printf("Can not open the infile!\n"); printf("Press any key to exit!\n"); getch(); exit(0); } printf("Please input the key:"); scanf("%d",&n);/*输入解密密码(可以为加密时候的密码)*/ n=26-n; printf("Please input the outfile:"); scanf("%s",outfile);/*输入解密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL) { printf("Can not open the outfile!\n"); printf("Press any key to exit!\n"); fclose(in); getch(); exit(0); } while(!feof(in)) { fputc(encrypt(fgetc(in),n),out); } printf("\nDecrypt is over!\n"); fclose(in); fclose(out); sleep(1); } clrscr(); printf("\nGood Bye!\n"); sleep(3); getch(); } }
恺撒加密算法是一种简单的替换加密方法,通过将字母按照一定的位移进行替换来实现加密和解密。 下面是使用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的恺撒加密算法进行加密,得到密文。然后使用同样的位移大小进行解密,得到原始的明文。最终打印出加密和解密结果。 这段程序实现了恺撒加密算法的过程,能够对输入的明文进行加密和对密文进行解密。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值