栅栏密码(解码技术)

栅栏密码解码技术

所谓栅栏密码,就是把要加密的明文分成N个一组,然后把每组的第1个字连起来,形成一段无规律的话。不过栅栏密码本身有一个潜规则,就是组成栅栏的字母一般不会太多。(一般不超过30个,也就是一、两句话)

中文名

栅栏密码

外文名

The rail fence cipher

性质

密码

领域

加密学

扩展

1.在选择行数时可以使多行,这样对于加密强度有所提高。2.可以在加密之后在使用其他密码进行加密,增加强度。[1]

实现的部分源代码

// 获取密钥和明文

                String str_p = et_plaintext.getText().toString();

                char[] str_p_char = null;

                    {

                    try 

                    {

                    str_p_char = str_p.toCharArray();

                    }

                    catch (Exception e) {

                        System.out.println("Exception");

                    }

                    int len=str_p_char.length;

                    System.out.println("len:"+len);

                    StringBuffer sb_1 = new StringBuffer();

                    StringBuffer sb_2 = new StringBuffer();

                    if(len%2==1)

                    {

                    for (int i = 0; i <len;i=i+1)

                    {

                    if(i%2==0)

                    {

                        sb_1.append(str_p_char[i]);

                    }

                    else

                    {

                        sb_2.append(str_p_char[i]);

                    }

                    }

                    }

                    else

                    {

                        for (int i = 0; i <len; i=i+2)

                        {

                        sb_1.append(str_p_char[i]);

                        sb_2.append(str_p_char[i+1]);

                        }

                    }

                                        

                    str_p = sb_1.toString()+sb_2.toString();

解密部分:

                                   String str_c = et_ciphertext.getText().toString();

                char []str_c_char = null;

                

                try

                {

                    str_c_char=str_c.toCharArray();

                }

                    catch(Exception e)

                    {

                    System.out.println("Exception");

                    }

                    int len=str_c_char.length;

                    int half=len/2;

                    StringBuffer sb = new StringBuffer();

                    if(len%2==1)

                    {

                    int i=0;

                    for (i = 0; i <half;i=i+1)

                    {

                    sb.append(str_c_char[i]);

                    sb.append(str_c_char[i+half+1]);

                    }

                    sb.append(str_c_char[half]);

                    }

                    else

                    {

                    for (int i = 0; i <half; i=i+1)

                        {

                            sb.append(str_c_char[i]);

                            sb.append(str_c_char[i+half]);

                        }

                            }

                        str_c=sb.toString();

参考资料

1.密码进阶课·知网空间

关键词:栅栏密码