行置换密码 (列置换密码) 您所在的位置:网站首页 两列换位为什么不好使呢 行置换密码 (列置换密码)

行置换密码 (列置换密码)

2024-07-10 06:20| 来源: 网络整理| 查看: 265

很多教材上对于行置换密码、列置换密码的定义都不是完全相同,甚至核心思想根本不一样。笔者就自己学习的经历,简单介绍一种一些教材上所谓的“行置换密码”的算法,大家一起交流、学习。

 

利用这种加密方法,明文按行填写在一个矩阵中,而密文则是以预定的顺序按列读取生成的。例如如果矩阵是4列5行,那么明文“encryption algorithm”(省去空格后)可以如下写入该矩阵:

2314encryptionalgorithms

 

按一定的顺序读取列以生成密文。

对于这个示例,如果读取顺序为递增顺序,则明文就是:“ctarm eyogt npnoh rilis”(添加空格只是为了便于观察)。这种加密法的密钥是列数和读取列的顺序。如果列数很多,记起来可能会比较困难,因此它可以表示成一个关键词,该关键词的长度等于列数,而其字母顺序决定读取列的顺序。

例如,关键词“general”有7个字母,意味着矩阵有7列。由于“a”是“general”中字母顺序最低的,因此数字1放在第6列;从左往右,第一个“e”为其次,所以数字2放在第2列;第二个“e”则是使数字3放在第4列。最后的顺序如下:

g  e  n  e  r  a  l

4  2  6  3  7  1  5

_______________________________________________________________________________________

This scheme is to write the message in a rectangle, row by row, and read the message off, column by column, but permute the order of the columns.The order of the columns then becomes the key to the algorithm.For example, 

            Key:     4   3   1   2   5   6   7

        Plaintext:     a   t   t   a   c   k   p

                   o   s   t   p   o   n   e

                   d   u   n   t   i   l   t

                   w   o   a   m   x   y   z

     Ciphertext:  ttna aptm tsuo aodw coix knly petz (再次强调,空格只是为了便于观察)

Thus, in this example, the key is 4312567.To encrypt, start with the column that is labeled 1, in this case column 3. Write down all the letters in that column.

**************************************************************************************

上述的是一次加密,也可把上述密文当做新一轮加密的明文,再次进行行置换加密。

 

参考资料:

《Cryptography and Network Security Principles and Practice, Fifth Edition》

                                               ————William Stallings

《Classical And Contemporary Cryptology》      ————Richard Spillman

(下面这个代码运行时,代码所在文件夹下需要有“plain.txt”文件,里面必须全部是英文字母,可大小写混杂,但是不能有其他字符,如空格、回车、数字等)

//Z26上的行置换密码 #include #include #include #include int length;//明文长度 char plain[100000]; char cipher[100000]; char out[100000]; int l;//密钥长度 int key[100];//密钥 int done_key[100]= {0}; //标记是否已经被转换为数字 int num_key[100]= {0};//把密钥换成数字 int num[100];//临时矩阵,存放使顺序递增的下标序号 void gen();//密钥生成算法 void encryption(); void decryption(); int length_str(int a[]); int main() { int i; FILE *fp; fp=fopen("plain.txt", "r"); fscanf(fp, "%s", plain);//从文件读入明文 fclose(fp); length=strlen(plain); gen(); encryption(); decryption(); for(i=0;i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有