用C语言实现任意字符串的加密,其中,字母用凯撒加密方法加密,非字母不变

来自:    更新日期:早些时候
c#求助:编写一个Windows窗体应用程序来装输入的字符串进行加密和解密,对于字母字符串加密~

不难直接用字符串替换方法!自己建一个工程写一下就。

是用C语言吗?

我尽量用注释阐述了思路,希望可以帮到你!!

#include<stdio.h>
#include<string.h>
#define N 80 //可加密字符串最大长度

char plaintext[N]={0}; //明文,输入时输入字符,参与运算时强制转换成整数
int ciphertext[N]={0}; //密文,保存成整数,输出时强制转换成字符
int k; //后(右)移位数,相当于密钥

void getPlainText() //获得明文字符串
{
printf("请输入明文:");
scanf("%s",plaintext);
printf("\n");
}

void getLength() //获取后(右)移位数(密钥)
{
printf("请输入后移的位数:");
scanf("%d",&k);
k%=26; //因为字母只有26个,所以超过26相当于重复
}

void Caesar_cipher() //凯撒加密,本程序采用的是字母循环后(右)移
{
unsigned int i;

for(i=0;i<strlen(plaintext);i++)
{
//两个bool类型的变量是为了判断字符是否是字母(包括大写和小写)
bool flag1=plaintext[i]>='a'&&plaintext[i]<='z';
bool flag2=plaintext[i]>='A'&&plaintext[i]<='Z';

if(flag1||flag2){ //如果是字母,加密
ciphertext[i]=(int)plaintext[i]+k; //字母在字母表中后(右)移K位
if(ciphertext[i]>(int)'z'){ //保证是循环后(右)移
ciphertext[i]-=26;
}
}
else //非字母字符,不做处理,原样保存
ciphertext[i]=(int)plaintext[i];

}

}

void printCipherText() //输出加密后的密文
{
unsigned int i;
printf("\n加密后的密文是:");
for(i=0;i<strlen(plaintext);i++) //把参与计算后是整数强制转换成对应的字符
printf("%c",(char)ciphertext[i]);
printf("\n");

}

void main()
{
getPlainText(); //明文
getLength(); //后(右)移位数
Caesar_cipher(); //凯撒加密
printCipherText(); //密文

}

(2)kaiser加密算法
具体程序:
#include<stdio.h>
#include<conio.h>
char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/
{
while(ch>='A'&&ch<='Z')
{
return ('A'+(ch-'A'+n)%26);
}
while(ch>='a'&&ch<='z')
{
return ('a'+(ch-'a'+n)%26);
}
return ch;
}
void menu()/*菜单,1.加密,2.解密,3.暴力破解,密码只能是数字*/
{
clrscr();
printf("\n=========================================================");
printf("\n1.Encrypt the file");
printf("\n2.Decrypt the file");
printf("\n3.Force decrypt file");
printf("\n4.Quit\n");
printf("=========================================================\n");
printf("Please select a item:");
return;
}

main()
{
int i,n;
char ch0,ch1;
FILE *in,*out;
char infile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();

sleep(3);/*等待3秒*/
menu();
ch0=getch();
while(ch0!='4')
{
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);
}
if(ch0=='3')
{
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 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);
}
for(i=1;i<=25;i++)/*暴力破解过程,在察看信息正确后,可以按'Q'或者'q'退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("==========================================================\n");
printf("The outfile is:\n");
printf("==========================================================\n");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf("\n========================================================\n");
printf("The current key is: %d \n",i);/*显示当前破解所用密码*/
printf("Press 'Q' to quit and other key to continue......\n");
printf("==========================================================\n");
ch1=getch();
if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'时退出*/
{
clrscr();
printf("\nGood Bye!\n");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
}
printf("\nForce decrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();

printf("\nGood Bye!\n");
sleep(3);
}
另外,团IDC网上有许多产品团购,便宜有口碑


用C语言实现任意字符串的加密,其中,字母用凯撒加密方法加密,非字母不变视频

相关评论:
  • 15175706252用C语言实现任意字符串的加密,其中,字母用凯撒加密方法加密,非字母不...
    应倪狄void printCipherText() \/\/输出加密后的密文 { unsigned int i;printf("\\n加密后的密文是:");for(i=0;i<strlen(plaintext);i++) \/\/把参与计算后是整数强制转换成对应的字符 printf("%c",(char)ciphertext[i]);printf("\\n");} void main(){ getPlainText(); \/\/明文 getLen...

  • 15175706252C语言实现将字符串进行加密处理,每个字符的加密规则是,将其转换为对应...
    应倪狄输入的是英文字符的话,直接加3就可以,但是如果是中文字符的话,如果直接高位和地位加3也可以,但是解密的时候就有一个不确定的存在,因为中文字符的ascii编码是有0xfe这样的存在,加上3的话就到时候还原就变得不确定。所以如果是中文字符加密的话,应该把两个ascii码转合并为两个字节无符号类型,然后再...

  • 15175706252如何对字符串进行MD5加密,用C语言实现,给出源代码和加密函数
    应倪狄512bits*\/unsigned char buffer[64];} MD5_CTX;static void MD5Transform(UINT4[4], unsigned char[64]);static void Encode(unsigned char *, UINT4 *, unsigned int);

  • 15175706252字符串加密
    应倪狄在C语言中,字符串加密是一种常见的数据保护技术,本文将展示两个函数replace和disorder,用于实现字符串的加密操作。首先,replace函数接收一个字符串src和一个整数n,通过循环迭代,每个字符会按照n的值进行位移,如果字符超过'Z',则循环回字母表的开头。这样,即使知道n的值,也难以直接推断出加密后的...

  • 15175706252谁可以给我一个c语言写的DES代码,要求(输入任意一个字符串,可以得到相 ...
    应倪狄printf("请输入8个要加密的字符:\\n"); for(i=0;i<8;i++) scanf("%c",&str[i]); getchar(); for(i=0;i<8;i++) strkey[i]=str[i]; printf("\\n输入明文的十六进制为:\\n"); for(i=0;i<8;i++) printf("%10x",strkey[i]); printf("\\n请输入密钥(8个字符):\\n"); for(i=...

  • 15175706252如何用c语言编:输入一字符串,将其中所有的大写英文字母+3,小写英文字...
    应倪狄char c,b;printf("请输入字符:\\n");while((c=getchar())!='\\n'){ if(c>='a'&&c<='z')b=c-3;else if(c>='A'&&c<='Z')b=c+3;printf("%c",b);} printf("\\n");} 程序已经运行过了,大写字母的后三位和小写字母的前三位因为运算后已经超过了字母的范围,输出的是别的...

  • 15175706252C语言:​输入一串字符(字符数小于 70)和正整数k,将其中的字母加密,并...
    应倪狄include <stdio.h>#include <ctype.h>int main(){ char str[70]; int k, i, N; scanf ("%d", &N); while (N--){ getchar(); scanf ("%s%d", str, &k); k %= 26; for (i = 0; str[i] != '\\0'; ++i){ if (isupper(str[i])){ ...

  • 15175706252C语言 输入字符串输出对应字母的ASCII编码 加密
    应倪狄char pass[101];int main(){ int i;char s[101],key[101];puts("设定密码:");scanf("%s",pass);puts("输入源字符串:");scanf("%s",s);puts("输入密码:");scanf("%s",key);while(strcmp(pass,key)){ \/*for(i=0;i<strlen(s);i++)printf("%d ",s[i]+4);puts("");...

  • 15175706252用c语言设计一个简单地加密算,解密算法,并说明其中的原理
    应倪狄可能很长 ,这是在我以前一个程序里摘出来的。原理:用户输入创建密码,机器读取,并把每一位密码进行加密,这里就是把每一位的 ASCII码加一(也可以有其他的加密方式),然后保存在文件里。解密时从文件中读取保存的乱码,然后把它每一位的ascII码减一 在与你输入的密码比较,正确既可以进入。define...

  • 15175706252c 语言常用的加密算法——MD5
    应倪狄SHA-1算法全称为Secure Hash Algorithm 1,用于数字签名、验证、消息摘要等,C语言中通过OpenSSL库实现SHA-1加密。Base64编码虽非加密算法,但用于隐藏信息,C语言中通过OpenSSL库进行Base64编码与解码。实现这些算法时通常利用OpenSSL库,因其提供了高效实现,避免了重复开发。MD5算法在C语言中的实现示例如下...

  • 相关主题精彩

    版权声明:本网站为非赢利性站点,内容来自于网络投稿和网络,若有相关事宜,请联系管理员

    Copyright © 喜物网