单表置换加密与凯撒码加密

 

一.单表置换密码
    单表置换密码也是一种传统的代替密码算法,在算法中维护着一个置换表,这个置换表记录了明文和密文的对照关系。当没有发生加密(即没有发生置换)之前,其置换表如 1-1-4所示。

表1-1-4

    在单表置换算法中,密钥是由一组英文字符和空格组成的,称之为密钥词组,例如当输入密钥词组:I LOVE MY COUNTRY后,对应的置换表如表 1-1-5所示。

表1-1-5

    在表1-1-5中 ILOVEMYCUNTR 是密钥词组 LOVE MY COUNTRY 略去前面已出现过的字符O和Y依次写下的。

后面ABD……WXZ则是密钥词组中未出现的字母按照英文字母表顺序排列成的,密钥词组可作为密码的标志,记住这个密钥词组就能掌握字母加密置换的全过程。
    这样对于明文:data security has evolved rapidly,按照表1-1-5的置换关系,就可以得到密文:VIKI JEOPHUKX CIJ EQDRQEV HIFUVRX。

 

 

二.Kaiser(恺撒)密码
    Kaiser密码是传统的代替加密法,当没有发生加密(即没有发生移位)之前,其置换表如 1-1-1所示。

表1-1-1

    加密时每一个字母向前推移k位,例如k=5,置换表如1-1-2所示。

表1-1-2

 

    于是对明文:data security has evolved rapidly
    可以得到密文:IFYF XJHZWNYD MFX JATQAJI WFUNIQD
    若令26个字母分别对应整数 0 ~ 25,如表 1-1-3所示。

表1-1-3

 

    则Kaiser加密变换实际上是
        c = (m + k) mod 26
    其中m是明文对应的数据,c是与明文对应的密文数据,k是加密用的参数,也称为密钥。
    很容易得到相应的Kaiser解密变换是:
        m = D(c) = (c – k) mod 26
    例如明文:data security 对应的数据序列:
        3  0  19  0  18  4  2  20  17  8  19  24
    k = 5 时得到密文序列:
        8  5  24  5  23  9  7  25  22  13  24  3
    对应的密文为:
        I  F  Y  F  X  J  H  Z  W  N  Y  D

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 凯撒(Caesar Cipher) 凯撒是一种简单的替换加密方法,是最古老的加密技术之一。它是通过将字母按照一定的偏移量进行替换来实现加密的。例如,将字母表中的每个字母向右偏移3个位置,A变成D,B变成E,以此类推,最终得到一个新的密文。 加密方法: ```c void caesar_encrypt(char *plaintext, int shift) { int i = 0; while (plaintext[i] != '\0') { if (isalpha(plaintext[i])) { if (isupper(plaintext[i])) plaintext[i] = ((plaintext[i] - 'A' + shift) % 26) + 'A'; else plaintext[i] = ((plaintext[i] - 'a' + shift) % 26) + 'a'; } i++; } } ``` 解密方法: ```c void caesar_decrypt(char *ciphertext, int shift) { int i = 0; while (ciphertext[i] != '\0') { if (isalpha(ciphertext[i])) { if (isupper(ciphertext[i])) ciphertext[i] = ((ciphertext[i] - 'A' - shift + 26) % 26) + 'A'; else ciphertext[i] = ((ciphertext[i] - 'a' - shift + 26) % 26) + 'a'; } i++; } } ``` 2. 字母倒排序(Reverse Alphabet Cipher) 字母倒排序是一种替换加密方法,它通过将字母表中的每个字母倒序排列,然后将明文中的每个字母替换成其对应的倒序字母,从而实现加密。例如,将字母A替换成Z,将B替换成Y,以此类推。 加密方法: ```c void reverse_alphabet_encrypt(char *plaintext) { int i = 0; while (plaintext[i] != '\0') { if (isalpha(plaintext[i])) { if (isupper(plaintext[i])) plaintext[i] = 'Z' - (plaintext[i] - 'A'); else plaintext[i] = 'z' - (plaintext[i] - 'a'); } i++; } } ``` 解密方法: ```c void reverse_alphabet_decrypt(char *ciphertext) { int i = 0; while (ciphertext[i] != '\0') { if (isalpha(ciphertext[i])) { if (isupper(ciphertext[i])) ciphertext[i] = 'A' + ('Z' - ciphertext[i]); else ciphertext[i] = 'a' + ('z' - ciphertext[i]); } i++; } } ``` 3. 单表置换(Monoalphabetic Substitution Cipher) 单表置换是一种替换加密方法,它通过将明文中的每个字母替换成一个固定的密文字母,从而实现加密。例如,将字母A替换成密文字母X,将B替换成密文字母Y,以此类推。 加密方法: ```c void monoalphabetic_encrypt(char *plaintext, char *key) { int i = 0; while (plaintext[i] != '\0') { if (isalpha(plaintext[i])) { if (isupper(plaintext[i])) plaintext[i] = toupper(key[plaintext[i] - 'A']); else plaintext[i] = tolower(key[plaintext[i] - 'a']); } i++; } } ``` 解密方法: ```c void monoalphabetic_decrypt(char *ciphertext, char *key) { int i = 0; while (ciphertext[i] != '\0') { if (isalpha(ciphertext[i])) { int j = 0; if (isupper(ciphertext[i])) { while (toupper(key[j]) != ciphertext[i]) j++; ciphertext[i] = 'A' + j; } else { while (tolower(key[j]) != ciphertext[i]) j++; ciphertext[i] = 'a' + j; } } i++; } } ``` 4. 维吉利亚密(Vigenère Cipher) 维吉利亚密是一种多表置换加密方法,它通过使用一系列不同的单表置换来实现加密。具体地,将明文中的每个字母按照一个密钥字符串中的字母进行偏移,然后使用对应位置的单表置换进行替换,最终得到一个新的密文。 加密方法: ```c void vigenere_encrypt(char *plaintext, char *key) { int i = 0; int j = 0; int key_len = strlen(key); while (plaintext[i] != '\0') { if (isalpha(plaintext[i])) { int shift; if (isupper(key[j % key_len])) shift = key[j % key_len] - 'A'; else shift = key[j % key_len] - 'a'; if (isupper(plaintext[i])) plaintext[i] = ((plaintext[i] - 'A' + shift) % 26) + 'A'; else plaintext[i] = ((plaintext[i] - 'a' + shift) % 26) + 'a'; j++; } i++; } } ``` 解密方法: ```c void vigenere_decrypt(char *ciphertext, char *key) { int i = 0; int j = 0; int key_len = strlen(key); while (ciphertext[i] != '\0') { if (isalpha(ciphertext[i])) { int shift; if (isupper(key[j % key_len])) shift = key[j % key_len] - 'A'; else shift = key[j % key_len] - 'a'; if (isupper(ciphertext[i])) ciphertext[i] = ((ciphertext[i] - 'A' - shift + 26) % 26) + 'A'; else ciphertext[i] = ((ciphertext[i] - 'a' - shift + 26) % 26) + 'a'; j++; } i++; } } ``` 5. 转换加密方法(Transposition Cipher) 转换加密方法是一种基于重新排列明文中的字符来实现加密的方法。具体地,将明文中的字符按照一定的规则重新排列,然后得到一个新的密文。 加密方法: ```c void transposition_encrypt(char *plaintext, char *key) { int i, j, k, len; int key_len = strlen(key); char **matrix; // 计算矩阵的行数 len = strlen(plaintext); i = len / key_len; if (len % key_len != 0) i++; // 分配矩阵的内存 matrix = (char **)malloc(i * sizeof(char *)); for (j = 0; j < i; j++) matrix[j] = (char *)malloc(key_len * sizeof(char)); // 将明文填充进矩阵 k = 0; for (j = 0; j < i; j++) { for (k = 0; k < key_len; k++) { if (plaintext[j * key_len + k] == '\0') matrix[j][k] = ' '; else matrix[j][k] = plaintext[j * key_len + k]; } } // 对矩阵的每一列进行置换 for (k = 0; k < key_len; k++) { int index = strchr(key, key[k]) - key; for (j = 0; j < i; j++) { char tmp = matrix[j][k]; matrix[j][k] = matrix[j][index]; matrix[j][index] = tmp; } } // 将矩阵中的字符按行顺序输出 k = 0; for (j = 0; j < i; j++) { for (k = 0; k < key_len; k++) printf("%c", matrix[j][k]); } } ``` 解密方法: ```c void transposition_decrypt(char *ciphertext, char *key) { int i, j, k, len; int key_len = strlen(key); char **matrix; // 计算矩阵的行数 len = strlen(ciphertext); i = len / key_len; // 分配矩阵的内存 matrix = (char **)malloc(i * sizeof(char *)); for (j = 0; j < i; j++) matrix[j] = (char *)malloc(key_len * sizeof(char)); // 将密文填充进矩阵 k = 0; for (j = 0; j < i; j++) { for (k = 0; k < key_len; k++) matrix[j][k] = ciphertext[j * key_len + k]; } // 对矩阵的每一列进行置换 for (k = key_len - 1; k >= 0; k--) { int index = strchr(key, key[k]) - key; for (j = 0; j < i; j++) { char tmp = matrix[j][k]; matrix[j][k] = matrix[j][index]; matrix[j][index] = tmp; } } // 将矩阵中的字符按列顺序输出 k = 0; for (k = 0; k < key_len; k++) { for (j = 0; j < i; j++) printf("%c", matrix[j][k]); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值