当前位置:首页 » 编程语言 » 单表代替密码的C语言实现
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

单表代替密码的C语言实现

发布时间: 2022-12-18 21:19:55

c语言 替换 简易密码代换

#include <stdio.h>

void main()
{
int i=0, j=0;
char a[100] = {0};
scanf("%s",a);
while ('\0' != a[i])
{
a[i] += 4;
if (a[i] > 'z')
{
a[i] -= 26;
}
else if (a[i] > 'Z' && a[i] < 'e')
{
a[i] -= 26;
}
++i;
}
printf("%s",a);
}
验证无问题

② 映射表加密程序(替换密码)

有单表替换加密解密c语言程序希望对你能有所帮助
#include<stdio.h>
char a[100];
char d[]={""};
void encryption();
void decryption();
void encryption()
{int i,j;
char b[100];
printf("please input a plaintext:\n");
gets(b);
printf("the ciphertext is:\n");
for(i=0;i<100;i++)
{if(b[i]==' ')
printf("%c",b[i]);
else if(b[i]!='\0')
{for(j=0;j<53;j++)
{if(d[j]==b[i])
printf("%c",a[j]);
}
}
else
break;}
printf("\n");
}
void decryption()
{char c[100];
int i,j,k;
printf("please input the ciphertext:\n");
gets(c);
printf("the plaintext is:\n");
for(i=0;i<100;i++)
{if(c[i]==' ')
printf("%c",c[i]);
else if(c[i]!='\0')
for(j=0;j<27;j++)
{if(a[j]==c[i])
{k=j+97;
printf("%c",k);
}
}
else break;
}
printf("\n");
}
void main()
{int i;
printf("please input 26 letters to form a key:\n");
scanf("%s",a);
list:printf("please choose a function:1.encryption 2.decryption 3.exit\n");
scanf("%d",&i);
getchar();
switch(i){
case 1:encryption();
goto list;break;
case 2:decryption();
goto list;break;
case 3:exit(1);
}
}

③ c语言实现密码替代

看b[i]=(char)(a[i]+k); 这句就知道了,就是将每个字母变成其后第k个字母,比如k=2,那么a变成c,b变成d,。。。。x变成z。那么此时,y,z就没法变了,所以就用if(b[i]>122){b[i]=(char)(b[i]-26); 把y变成a,z变成b.

④ 用C语言或者其他语言编写替代密码和置换密码

给你,自己再稍微改造一下吧:
#include "stdio.h"
#include "conio.h"

main()
{
int k,i=0;
char a[100],b[100];
printf("qing shu ru ni de mi wen \n");
gets(a);
printf("qing shu ru mi shi \n");
scanf("%d",&k);
printf("\n");
do{
b[i]=(char)(a[i]+k);
if(b[i]>122){
b[i]=(char)(b[i]-26);
}
i++;
}while(a[i]!='\0');
puts(b);
getch();
}

⑤ 用c语言编使用密钥的单表代替密码的加和解密

期待看到有用的回答!

⑥ 使用密码表加密 用C语言

密码加密,有很多种方法的,最简单的是单表仿射,也有多表置换之类的。一般都有一个变换公式的。。
你这里看不出来用哪一种方法的。。所以不好写代码。建议,拿本密码学之类的书,看下。

⑦ C语言程序。急!

在单表置换密码中,密钥是由字母与空格组成的 如shana

在没有密钥作用前,置换表如下


a b c d e f g h i j k l m n o p q r s t u v w x y z

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z



在密钥的作用下,置换表将发生变化,具体如下

将密钥填入置换表,如果遇到重复的字符则忽略,接着按原表顺序填充,忽略重复字符,如下表


a b c d e f g h i j k l m n o p q r s t u v w x y z

S H A N B C D E F G I J K L M O P Q R T U V W X Y Z



首先将SHAN填入表中,因为A已经在前面出现,所以忽略,接着将除去S H A N四个字符的字母表按顺序填充


C语言程序:

#include<stdio.h>
#include<string.h>
#include<malloc.h>

#defineMAX101 /*明文字符串的最大长度*/

char*encrypt(char*source,char*key);
intcontain(char*source,intn,charch);

voidmain()
{
char*source; /*明文*/
char*dest; /*密文*/
char*key; /*密钥*/

source=(char*)malloc(sizeof(char)*MAX);
dest=(char*)malloc(sizeof(char)*MAX);
key=(char*)malloc(sizeof(char)*MAX);

printf("请输入明文字符串:");
gets(source);
printf("请输入密钥:");
gets(key);

dest=encrypt(source,key);

printf("密文字符串:");
puts(dest);
}

/*加密明文(单表置换加密),返回密文*/
char*encrypt(char*source,char*key)
{
char*dest;
inti,j;
intlen1=strlen(source);
intlen2=strlen(key);
charch;

dest=(char*)malloc(sizeof(char)*MAX);
source=strupr(source);
key=strupr(key);

for(i=0;i<len1;i++)
{
dest[i]=NULL;
}
dest[i]='';

for(i=0,j=0;i<len1&&j<len2;)
{
while(j<len2&&contain(dest,i,key[j])==1)
{
j++;
continue;
}
if(j>=len2)
{
break;
}
dest[i++]=key[j++];
}

for(j=i;j<len1;j++)
{
ch='A'+(j-i+1);
while(contain(dest,j,ch)==1)
{
ch=(ch=='Z'?'A':ch+1);
}
dest[j]=ch;
}
dest[j]='';

returndest;
}

/*查找字符ch在字符串source的前n个字符中是否存在*/
intcontain(char*source,intn,charch)
{
inti;

for(i=0;i<n;i++)
{
if(source[i]==ch)
{
return1;
}
}

return0;
}


运行测试:

⑧ C语言字符串加密 多实例

string i;
int k;
cin>>i>>k;
for (int q=0;q<i.length();q++)
{
if (i[q]>='a'&& i[q]<='z')
i[q]=char((i[q]-'a'+k)%26+'a');
else
if (i[q]>='A'&&i[q]<='Z')
i[q]=char((i[q]-'A'+k)%26+'A');
}

⑨ 算法设计分析期末考题目,“密码学中的算法”设计实现单表置换密码,能对英文和数字组成的短文进行加密解

很久没用C了,写的比较凌乱,呵呵

#include "stdio.h"
#include "conio.h"
#define MAP_SIZE 128
#define CONTENT_SIZE 1000
#define VALID_CHAR_COUNT 62
char map[MAP_SIZE], rmap[MAP_SIZE], keymask[MAP_SIZE];
char input[CONTENT_SIZE], output[CONTENT_SIZE];
void initialize(char *array)
{
int i;
for(i = 0; i < MAP_SIZE; ++i)
{
array[i] = i;
keymask[i] = 0;
}
}
int isvalidchar(char c)
{
/*判断字符是否字母或数字*/
return c >= '0' && c <= '9' || c >= 'a' && c <= 'z' ||c >= 'A' && c <= 'Z';
}
int convertindex(int i)
{
/*将0~61的数字转换到转换表中对应区段的下标
即0~9 => 数字,10~35 => 大写字母,36~61 => 小写字母*/
if(i < 10)
{
return i + '0';
}
i-=10;
if(i < 26)
{
return i + 'A';
}
i-=26;
if(i < 26)
{
return i + 'a';
}
}
void gen_map(char *key)
{
/*生成加密用的置换表*/
int index = 0, i;
initialize(map);
while(*key)
{
/*先将密钥中的字符加入转换表,并做标记*/
if(keymask[*key] == 0)
{
keymask[*key] = 1;
map[convertindex(index++)] = *key;
}
++key;
}
for(i = 0; i < MAP_SIZE; ++i)
{
/*将密钥中未出现的字符依次加入转换表*/
if(keymask[i] == 0 && isvalidchar(i))
{
map[convertindex(index++)] = i;
}
}
}
void gen_rmap(char *key)
{
/*生成解密用的置换表*/
int index = 0, i;
initialize(rmap);
while(*key)
{
/*先将密钥中的字符加入转换表,并做标记*/
if(keymask[*key] == 0)
{
keymask[*key] = 1;
rmap[*key] = convertindex(index++);
}
++key;
}
for(i = 0; i < MAP_SIZE; ++i)
{
/*将密钥中未出现的字符依次加入转换表*/
if(keymask[i] == 0 && isvalidchar(i))
{
rmap[i] = convertindex(index++);
}
}
}
void encrypt(char *key)
{
int i, len = strlen(input);
gen_map(key);
for(i = 0; i < len; ++i)
{
/*对每一个原文中的字符,从置换表中找到对应字符并加入输出缓冲*/
output[i] = map[input[i]];
}
output[len] = 0;
}
void decrypt(char *key)
{
int i, len = strlen(input);
gen_rmap(key);
for(i = 0; i < len; ++i)
{
/*对每一个原文中的字符,从置换表中找到对应字符并加入输出缓冲*/
output[i] = rmap[input[i]];
}
output[len] = 0;
}
int main()
{
char key[CONTENT_SIZE];
int opt;
while(1)
{
do
{
printf("Input 1 to encrypt, 2 to decrypt, or 0 to exit:");
scanf("%d%c", &opt);
}
while(opt != 1 && opt != 2 && opt != 0);
if(opt == 0)
{
return 0;
}
puts("Content:");
gets(input);
puts("Key:");
gets(key);
if(opt == 1)
{
encrypt(key);
}
else
{
decrypt(key);
}
puts(output);
}
}