C密码表分配

  • 本文关键字:分配 码表 密码 c
  • 更新时间 :
  • 英文 :


如何制作将int转换为char的密码表?

我已经制作了一个加密表,如下所示。

int ciper[]={
['h']=11, ['e']=12, ['l']=13, ['o']=14, 
};
int decript[]={
['11']=a,
};

如何制作用于解密的密码表?代码从一个文件读取字符串hello,并将script消息发送到另一个文件1112131314

我现在想为此做一个描述,我需要一个描述表['11']=h。。to decipt 1112131314=你好`

#include <stdio.h>
#include <assert.h>
int encryption_data[] = {
//-------------------------start alphabet lower and  upper case--------------------------------------------
['a'] = 11, ['b'] = 17, ['c'] = 23, ['d'] = 29, ['e'] = 35, ['f'] = 41, ['g'] = 47, ['h'] = 53, ['i'] = 58,
['j'] = 12, ['k'] = 18, ['l'] = 24, ['m'] = 30, ['n'] = 36, ['o'] = 42, ['p'] = 48, ['q'] = 54, ['r'] = 59,
['s'] = 13, ['t'] = 19, ['u'] = 25, ['v'] = 31, ['w'] = 37, ['x'] = 43, ['y'] = 49, ['z'] = 55, ['A'] = 60,
['B'] = 14, ['C'] = 20, ['D'] = 26, ['E'] = 32, ['F'] = 38, ['G'] = 44, ['H'] = 50, ['I'] = 56, ['J'] = 61,
['K'] = 15, ['L'] = 21, ['M'] = 27, ['N'] = 33, ['O'] = 39, ['P'] = 45, ['Q'] = 51, ['R'] = 57, ['S'] = 62,
['T'] = 16, ['U'] = 22, ['V'] = 28, ['W'] = 34, ['X'] = 40, ['Y'] = 46, ['Z'] = 52, 
//diacritics here------------------------------------------------------------------------------------------
//-------------------------end alphabet lower and upper case-----------------------------------------------
//---------------------------------------------start symbols-----------------------------------------------
['!'] = 63, ['@'] = 67, ['#'] = 71, ['$'] = 75, ['%'] = 78, ['^'] = 81, ['&'] = 84, ['*'] = 87, ['('] = 90,
[')'] = 64, ['_'] = 68, ['+'] = 72, ['-'] = 76, [':'] = 79, ['='] = 82, ['['] = 85, [']'] = 88, ['{'] = 91,
['}'] = 65, ['\'] = 69, ['|'] = 73, [';'] = 77, ['<'] = 80, ['>'] = 83, [','] = 86, ['.'] = 89, ['?'] = 92,
['/'] = 66, ['"'] = 70, ['''] = 74, ['`'] = 402, ['~'] = 403, [' '] = 404, 
//---------------------------------------------end symbols--------------------------------------------------
};
char decryption_data[]= {
//-------------------------start alphabet lower and  upper case--------------------------------------------
["11"]='a',
};
void crypt_key()
{
FILE *fin = fopen("sursa.txt","r");
assert(fin != NULL);
FILE *fout = fopen("rezultat.txt","w");
assert(fout != NULL);
for (int c; (c = getc(fin)) != EOF;) {
if (c == 'n') {
if (fputc('n', fout) == EOF) {
fprintf(stderr, "Error writing to file fout with fputcn");
getchar();
}
continue;
}
if (fprintf(fout, "%5d ", encryption_data[c]) < 0) {
fprintf(stderr, "Error writing to file fout with fprintfn");
getchar();
}
}
fclose(fin);
fclose(fout);
}
void decrypt_key()
{
FILE *fin = fopen("rezultat.txt","r");
assert(fin != NULL);
FILE *fout = fopen("decrypted.txt","w");
assert(fout != NULL);
for (int c; (c = getc(fin)) != EOF;) {
if (c == 'n') {
if (fputc('n', fout) == EOF) {
fprintf(stderr, "Error writing to file fout with fputcn");
getchar();
}
continue;
}
if (fprintf(fout, "%5c ", decryption_data[c]) < 0) {
fprintf(stderr, "Error writing to file fout with fprintfn");
getchar();
}
}
fclose(fin);
fclose(fout);
}
void main()
{
int x;
printf("1 - Encryptionn");
printf("2 - Decryptionn");
printf("Option = ");
scanf("%d", &x);
switch(x)
{
case 1:
if ( x == 1 )
{
crypt_key();
}
printf("Encryption done - check rezultat.txtn");
break;
case 2:
if ( x == 2 )
{
decrypt_key();
}
break;
default: 
printf("This option does not exist!n");
break;
}
}

Program should encrypt from file print output and decrypt from file with output put output doesnt work

更改

int decript[]={
['11']=a,
};

int decript[]={
[11]='a',
};

根据代码,使用fprintf输出加密数据,格式为"%5d "。您需要使用格式为"%5d "或仅为"%d"的补充fscanf,而不是当前的getc,以便读取中的加密数据

您在问题中所问的["11"]='a'初始值设定项类似于关联数组,但遗憾的是,这些数组在C语言中不可用。

有很多方法可以创建转换表,其中一些涉及结构化编程(即转换结构数组的定义(


让我们从这些数组的定义中抽象出来,假设您能够正确地从源中检索整数,如果它们是字符串形式,则确保它们都是相同的长度(即,都具有相同的位数(。根据这些假设,执行转换的更通用方法是使用switch-case语句:

加密:

int encrypt;
switch (inputChar)
{
case 'a':
encrypt = 77;
break;
case 'e':
encrypt = 12;
break;
case 'h':
encrypt = 11;
break;
case 'l':
encrypt = 13;
break;
case 'o':
encrypt = 14;
break;
/*  ...  */
default:
/* Raise an error in case of unmanaged input char*/
}

解密:

char decryptedChar;
switch (inputInt)
{
case 77:
decryptedChar = 'a';
break;
case 12:
decryptedChar = 'e';
break;
case 11:
decryptedChar = `h`;
break;
case 13:
decryptedChar = 'l';
break;
case 14:
decryptedChar = 'o';
break;
/*  ...  */
default:
/* Raise an error in case of unmanaged input int*/
}

最新更新