计算机网络上机实验4——加密
利用ASCII码值进行加密,如下:
简单示例
这是一个比较简单的计算,老师提供的示例,解密也容易写出来
加密
#include <stdio.h>
#include <string.h>
int main()
{
int i;
int count = 0;
char Text[128] = { "time"};
char cryptograph[128] = { '\0' };
printf("请输入要加密的明文:%s\n", Text);
count = strlen(Text);//求字符串的长度,但不包括字符串的null结束符
for (i = 0; i < count; i++)
{
cryptograph[i] = Text[i] + i + 8;//利用ASCII码值进行一定的运算加密
}
printf("加密后的密文是:%s\n", cryptograph);
return 0;
}
解密
#include <stdio.h>
#include <string.h>
int main()
{
int i;
int count = 0;
char Text[128] = { "|rwp"};
char cryptograph[128] = { '\0' };
printf("请输入加密后的密文:%s\n", Text);
count = strlen(Text);//求字符串的长度,但不包括字符串的null结束符
for (i = 0; i < count; i++)
{
cryptograph[i] = Text[i] - i - 8;
}
printf("解密后的明文是:%s\n", cryptograph);
return 0;
}
修改——可输入
加密
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main()
{
int i;
int count = 0;
char *Text;
Text=(char *)malloc(sizeof(char));
printf("请输入要加密的明文:\n");
scanf("%[^\n]",Text);
//char Text[128] = { "time"};
char cryptograph[128] = { '\0' };
printf("你要加密的明文是:%s\n", Text);
count = strlen(Text);//求字符串的长度,但不包括字符串的null结束符
for (i = 0; i < count; i++)
{
cryptograph[i] = Text[i] + i + 2 ;//利用ASCII码值进行一定的运算加密
}
printf("加密后的密文是:%s\n", cryptograph);
return 0;
}
解密——可输入
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main()
{
int i;
int count = 0;
char *Text;
Text=(char *)malloc(sizeof(char));
printf("请输入加密后的密文:", Text);
scanf("%[^\n]",Text);
//char Text[128] = { "time"};
char cryptograph[128] = { '\0' };
printf("你要解密的密文是:%s\n", Text);
count = strlen(Text);//求字符串的长度,但不包括字符串的null结束符
for (i = 0; i < count; i++)
{
cryptograph[i] = Text[i] - i - 2 ;//利用ASCII码值进行一定的运算加密
}
printf("解密后的明文是:%s\n", cryptograph);
return 0;
}
貌似解不出来。。。有机会完善一下,暂存代码
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main()
{
int i;
int count = 0;
char *Text;
char k[128];
char key[128] = { "xiaojingyixiaojingyixiaojingyixiaojingyixiaojingyi"};
Text=(char *)malloc(sizeof(char));
printf("请输入要加密的明文:\n");
scanf("%[^\n]",Text);
//char Text[128] = { "time"};
char cryptograph[128] = { '\0' };
printf("你要加密的明文是:%s\n", Text);
count = strlen(Text);//求字符串的长度,但不包括字符串的null结束符
for (i = 0; i < count; i++)
{
if(Text[i] + key[i]>128) {
cryptograph[i] = Text[i] + key[i] - 128;
}
else{
cryptograph[i] = Text[i] + key[i] ;//利用ASCII码值进行一定的运算加密
}
}
printf("加密后的密文是:%s\n", cryptograph);
return 0;
}