當前位置:首頁 » 編程語言 » 單表代替密碼的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);
}
}