经典密码学——行置换加密法

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

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


2 3 1 4
e n c r
y p t i
o n a l
g o r i
t h m s


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


对于这个示例,如果读取顺序为递增顺序,则明文就是:“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


//Z26上的行置换密码
#includestdio.h
#includestring.h
#includestdlib.h
#includetime.h
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(); 
    printf("以上正确"); 
    decryption(); 
     for(i=0;ilength;i++) 
    { 
        printf("%c", out[i]); 
    } 
    return 0; 
} 
 
void gen()//密钥生成算法
{ 
    int i; 
    printf("请输入想生成的随机密钥的长度:"); 
    scanf("%d", l); 
    srand(time(0)); 
    for(i=0; il; i++) 
    { 
        key[i]=rand()%26; 
    } 
    printf("\n随机产生的密钥串为:"); 
    for(i=0; il; i++) 
    { 
        printf("%c ", key[i]+97); 
    } 
    printf("\n\n"); 
} 
char a[50000][100];//临时矩阵,为了更方便的把密文转换出来
//这个数组必须设置为全局的,在函数中声明的话申请内存会出错!!!
void encryption() 
{ 
    ///转换:把密钥字符串排序,变成数字
    int k=1; 
    int i, j, m; 
    int small;//每轮循环给"最小"的字母编号(未编号的、靠前的、序号小的字母为最小)
    for(i=0; il; i++) //把字母换成从1开始的数字
    { 
        m=0; 
        while(done_key[m]==1) 
            m++; 
        small=m; 
        for(j=0; jl; j++) 
        { 
            if(done_key[j]==0)//没转换则继续
                if(key[j]key[small]) 
                    small=j; 
        } 
        num_key[small]=k; 
        done_key[small]=1; 
        k++; 
    }//
    printf("The order of the key is :\n"); 
    for(i=0; il; i++) 
    { 
        printf("%d ", num_key[i]); 
    } 
    printf("\n"); 
    for(i=0; ilength; i++) //忽略非字母字符,把大写转换为小写
    { 
        if(plain[i]=65plain[i]=90) 
        { 
            plain[i]+=32; 
        } 
    } 
    while(length%l) 
    { 
        strcat(plain,"p");//不能整除时补上无效字符p
        length++; 
    } 
    //生成密文矩阵
    k=0; 
 
    for(i=0; ilength/l; i++) //行
        for(j=0; jl; j++) //列
        { 
            a[i][j]=plain[k++]; 
        } 
    k=0; 
    for(i=0;il;i++)//列
    { 
        for(j=0;jl;j++) 
            if(num_key[j]==i+1) 
                num[i]=j; 
    } 
    k=0; 
    for(m=0;ml;m++)//列
        for(j=0;jlength/l;j++)//行
            cipher[k++]=a[j][num[m]]; 
} 
char b[50000][100]; 
void decryption()//解密函数
{ 
    int i, j, k; 
    k=0; 
    for(i=0;il;i++)//num[i]作为列
        for(j=0;jlength/l;j++)//行
            b[j][num[i]]=cipher[k++]; 
    k=0; 
    for(i=0;ilength/l;i++)//行
        for(j=0;jl;j++)//列
            out[k++]=b[i][j]; 
} 
 //同文件夹下需要有“plain.txt”文件,里面必须全部是英文字母,可大小写混杂,但是不能出现其他字符,如空格、回车换行、数字等。


posted @ 2013-05-12 00:27:46 kuye 阅读(5455) 评论(0)
发表评论
昵称
邮箱
网址