|
发表于 2020-4-1 18:00:45
|
显示全部楼层
就用这个密匙,在你看来输出什么才对?
- ./main
- wangbuliuxing
- 39 93 42 16 54 77 50 38 61 0 42 15 13
- J P D W D T J U D X Y C T
- JPDWDTJUDXYCT
复制代码
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #define OK 1
- #define ERROR 0
- #define SIZE 16
- typedef char Elemtype ;
- typedef struct node
- {
- Elemtype elem ;
- struct node *next ;
- struct node *previous ;
- }node , *Linklist ;
- static char* input_plaintext (void) ;
- static void create_BidCirlinklist ( Linklist *L) ;
- static int * create_CipherKey ( int len ) ;
- static node* find_ch (Linklist L , Elemtype ch) ;
- static void create_one_cipher (node*S , int n , Elemtype *ch) ;
- static Elemtype* create_Cipher (Linklist L, int len , int *key , char * passward) ;
- int main (void)
- {
- FILE *fp ;
- int len , *key , i;
- Elemtype *password , *new_password ;
- Linklist l = NULL;
- srand(time(NULL)) ; // 放在这里更好
- if ( (fp = fopen("Encrypted.txt", "wb")) == NULL)
- {
- printf("打开文件失败\n") ;
- exit(EXIT_FAILURE) ;
- }
- password = input_plaintext() ;
- len = strlen(password) ;
- key = create_CipherKey(len) ;
- fwrite(password, 1, len, fp) ;
- fwrite(key,sizeof(int),len,fp) ;
- fclose(fp) ; // 至此写入明文和密匙,以二进制的形式
- create_BidCirlinklist(&l) ;
- new_password = create_Cipher(l,len,key,password) ;
- printf ("%s\n",password) ; // 从这里开始便是验证输入和输出结果
- i = 0 ;
- while ( i < len )
- {
- printf("%2d ",key[i]) ;
- i++;
- }
- printf("\n") ;
- for( i = 0; i < len ; i++)
- {
- printf("%c ",new_password[i]) ; // 单个字母输出并无问题
- }
- printf("\n") ;
- printf("%s\n",new_password) ; // 问题出现在这个输出上 // 你期望这个printf输出什么内容?
- free(password); // 少了这个吧?
- free(new_password);
- free(key);
- return 0;
- }
- // 输入明文
- static char* input_plaintext (void)
- {
- #if 0
- int Len ;
- char *Str ,*str;
- str = (char*)malloc(SIZE) ; // 永远都不需要乘一个sizeof(char)
- printf ("请输入明文(至多输入16位):") ;
- scanf ("%s",str) ;
- Len = strlen(str) ;
- Str = (char*)malloc(Len + 1) ; // 这里不需要加1吗?
- strcpy(Str,str) ;
- free(str); // 这里不需要free吗?
- return Str ;
- #endif
- char *str = malloc(14);
- strcpy(str, "wangbuliuxing");
- return str;
- }
- // 建立解密环
- static void create_BidCirlinklist ( Linklist *L) // L是指针的指针
- {
- node *flag , *new_node ;
- int i = 0 ;
- while ( i< 26 )
- {
- new_node = (node *)malloc(sizeof(node )) ;
- new_node->elem = 'A'+i ;
- if ((*L) == NULL )
- {
- flag = (*L) = new_node ;
- flag->next = flag->previous = NULL ;
- }
- else
- {
- new_node->next = flag->next ;
- flag->next = new_node ;
- new_node->previous = flag ;
- flag = new_node ;
- }
- i++ ;
- }
- flag->next = (*L) ;
- (*L)->previous = flag ;
- }
- // 生成随机密匙
- static int * create_CipherKey ( int len )
- {
- int i ;
- int *Key = (int *)malloc(sizeof(int) * len) ;
- for( i = 0 ; i < len ; i++)
- {
- Key[i] = rand()%100 ;
- }
- return Key ;
- }
- // 生成密文
- static Elemtype* create_Cipher (Linklist L, int len , int *key , char * passward)
- {
- int i ;
- Elemtype *new_cipher = (Elemtype*)malloc( sizeof(Elemtype)*len ) ;
- node *flag ;
- for ( i = 0 ; i < len ; i++)
- {
- flag = find_ch(L , passward[i]) ;
- create_one_cipher(flag,key[i],&new_cipher[i]) ;
- }
- return new_cipher ;
- }
- // 找到对应的字母,便于生成新字母
- static node* find_ch (Linklist l , Elemtype ch)
- {
- node *flag1 = l ;
- if ( ch > 'Z')
- {
- ch -= 32 ;
- }
- if ( ch <= 'M')
- {
- while (flag1->elem != ch)
- flag1 = flag1->next ;
- }
- else
- {
- while (flag1->elem != ch)
- flag1 = flag1->previous ;
- }
- return flag1 ;
- }
- // 生成新一位密文并用ch返回
- static void create_one_cipher (node*S , int n , Elemtype *ch) // 当n的绝对值大于13时 向反方向移动 提高效率
- {
- node *flag2 = S ;
- n = n > 0 ? n : -n;
- n %= 26 ;
- while (n--)
- flag2 = flag2->next ;
- *ch = flag2->elem ;
- #if 0
- node *flag2 = S ;
- n = n > 0 ? n : -n;
- n %= 26 ;
- if ( n >13) // 这个if-else可以提高效率?
- {
- while (n--)
- flag2 = flag2->previous;
- }
- else
- {
- while (n--)
- flag2 = flag2->next ;
- }
- *ch = flag2->elem ;
- #endif
- #if 0
- node *flag2 = S ;
- if ( n > 0)
- {
- n %= 26 ;
- if ( n >13)
- {
- while (n--)
- flag2 = flag2->previous;
- }
- else
- {
- while (n--)
- flag2 = flag2->next ;
- }
- }
- else
- {
- n %= -26 ;
- if ( n >13)
- {
- //while (n++) // 确定这里是++ ?
- while(n--)
- flag2 = flag2->previous;
- }
- else
- {
- //while (n++) // 还有这里
- while(n--)
- flag2 = flag2->next ;
- }
- }
- *ch = flag2->elem ;
- #endif
- }
复制代码 |
|