单表密码的C语言实现

#include
#include
#include

void Decryp( char str_Letter[], char str_Min[], char str_Mi[] )
{
      int i = 0, j = 0;
      for( ; str_Min[i] != '\0'; i++ )
      {
            int index = str_Min[i] - 'a';
            str_Mi[j] = str_Letter[index];
            j++;
      }
}

void ChangTable(char str_Letter[], char str_NewLetter[] )  //将明密文对偶表转换成密明文对偶表
{
      int i = 0;
      int OldIndex = 0;
      int NewIndex = 0;
      for( ; i < 26; i++ )
      {
            OldIndex = i;
            NewIndex = str_Letter[i] - 'a';
            //OldIndex = i;
            str_NewLetter[NewIndex] = OldIndex + 'a' ;
      }
}

int menu()
{
      int n = 1;

      printf("\t\t1.加密      2.脱密      3.退出\n");
      printf("请选择您的功能:");
      scanf("%d",&n);

      return n;
}


int main( void )
{
      static char str_Min[100];
      static char str_Mi[100];
      static char str_Letter[26] = { 'a', 'c', 'b', 't', 'i', 'f', 'l', 'h', 'e', 'k', 'o', 'm', 'r', 'n', 'j', 'p', 's', 'g', 'q', 'd', 'u', 'v', 'w', 'z', 'y', 'x'};
      char str_NewLetter[26];
      int n = 1;

label:
      n = menu();
      getchar();
      switch(n)
      {
      case 1:
            printf("请输入明文:");
            gets(str_Min);
           
            Decryp( str_Letter, str_Min, str_Mi );
            printf("密文:");
            puts(str_Mi);
            break;
           
      case 2:
            printf("请输入密文:");
            gets(str_Min);
            ChangTable( str_Letter, str_NewLetter );
            Decryp( str_NewLetter, str_Min, str_Mi );
            printf("脱密后的明文:");
            puts(str_Mi);
            break;

      case 4:
            exit(0);
            break;
           
      default:
            printf("您的选择出错,按任意键退出程序!");
            getchar();
              exit(-1);
            break;
      }
      goto label;
     
    return 0;
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单表代换密码是一种简单的密码算法,它通过将明文中的每个字母替换为一个固定的字母来进行加密。下面是一个使用C语言实现单表代换密码的例子[^2]: ```c #include <stdio.h> #include <string.h> void encrypt(char *plaintext, char *ciphertext, char *key) { int i; int len = strlen(plaintext); for (i = 0; i < len; i++) { if (plaintext[i] >= 'a' && plaintext[i] <= 'z') { ciphertext[i] = key[plaintext[i] - 'a']; } else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') { ciphertext[i] = key[plaintext[i] - 'A' + 26]; } else { ciphertext[i] = plaintext[i]; } } ciphertext[i] = '\0'; } void decrypt(char *ciphertext, char *plaintext, char *key) { int i; int len = strlen(ciphertext); for (i = 0; i < len; i++) { if (ciphertext[i] >= 'a' && ciphertext[i] <= 'z') { plaintext[i] = 'a' + strchr(key, ciphertext[i]) - key; } else if (ciphertext[i] >= 'A' && ciphertext[i] <= 'Z') { plaintext[i] = 'A' + strchr(key + 26, ciphertext[i]) - key - 26; } else { plaintext[i] = ciphertext[i]; } } plaintext[i] = '\0'; } int main() { char plaintext[] = "Hello, World!"; char ciphertext[100]; char decrypted[100]; char key[] = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"; encrypt(plaintext, ciphertext, key); printf("Encrypted text: %s\n", ciphertext); decrypt(ciphertext, decrypted, key); printf("Decrypted text: %s\n", decrypted); return 0; } ``` 这段代码中,我们定义了两个函数`encrypt`和`decrypt`来进行加密和解密操作。`encrypt`函数将明文中的每个字母根据密钥替换为对应的字母,`decrypt`函数则将密文中的每个字母根据密钥进行逆向替换。在`main`函数中,我们定义了一个明文字符串`plaintext`,并使用密钥`key`对其进行加密和解密操作。最后,我们打印出加密后的密文和解密后的明文。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值