置换加密算法2.doc_第1页
置换加密算法2.doc_第2页
置换加密算法2.doc_第3页
置换加密算法2.doc_第4页
置换加密算法2.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

置换密码算法的原理是不改变明文字符,而是按照某一规则重新排列消息中的比特或字符顺序,才而实现明文信息的加密。置换密码有时又称为换位密码。矩阵换位法是实现置换密码的一种常用方法。它将明文中的字母按照给定的顺序安排在一个矩阵中,然后用根据密钥提供的顺序重新组合矩阵中的字母,从而形成密文。例如,明文为attack begins at five,密钥为cipher,将明文按照每行6个字母的形式排在矩阵中,形成如下形式:根据密钥cipher中各个字母在字母表中出现的先后顺序,给定一个置换:由密钥cipher通过getTheMatrix方法得到f矩阵。根据上面的置换,将原有居住中的字母按照第列、第裂、第裂、第裂、第列、第列的顺序排列,则有下面的形式:这里经过了一点处理就是当输入的字母数不是密钥长度的整数倍时,在输入的备加密字符串加上几个“#”来完成补全,然后参加到加密的过程中去,从而得到如下的密文:a siteaetb vci #agt#knf#其解密过程是根据密钥的字母数作为列数,将密文按照列、行的顺序写出,再根据由 密钥给出的矩阵置换产生新的矩阵,从而恢复明文。 本实验中也使用了上述的测试用例,主要经过两次依据矩阵的变换,都是使用矩阵f中的下面一行中的数值所在列来替换上面一行数值表示的列的值,从而来改变起序列、完成加密。解密中用到groupedSourceij=tempisecretMatrixsecretMatrixj;来完成将原来经过矩阵转变来的字符序列逆转,从而得到起加密前的矩阵。【代码部分】package Practice1;import java.util.Arrays;import java.util.Scanner;public class ReplaceDemo private String sourceString = attack begins at five;private String keyString = cipher;private int secretMatrix;private char groupedSource;private void getSource()/Get the source StringScanner scan = new Scanner(System.in);System.out.print(Please input the string what you want to encrypt:t);sourceString = scan.nextLine();System.out.print(Please input the key string for you:t);keyString = scan.next();public ReplaceDemo()/constructor for this classgetSource();private void groupSourceString()/make groups for sourceString according by the keyStringint sourceLen = sourceString.length();int keyLen = keyString.length();groupedSource = new charsourceLen/keyLen+1keyLen;if (sourceLen%keyLen!=0)for (int i = 0;i (keyLen - sourceLen%keyLen);i+)sourceString += #;sourceLen = sourceString.length();for(int i = 6,j=0;i = sourceLen;i+=keyLen,j+)groupedSourcej = sourceString.substring(i-keyLen, i).toCharArray();private void getTheMatrix()/get the Matrix for the encryptchar temp01 = keyString.toCharArray();secretMatrix = new intkeyString.length();Arrays.sort(temp01);for (int i = 0;i keyString.length();i+)for (int j = 0;jkeyString.length();j+)if (keyString.toCharArray()i=temp01j)secretMatrixi = j;private void changeSourceMatix()groupSourceString();char temp = new chargroupedSource.lengthkeyString.length();for (int i = 0;i groupedSource.length;i+)for (int j = 0;j keyString.length();j+)tempij = groupedSourceij;/请教Java老师for (int i = 0;i groupedSource.length - 1;i+)for (int j = 0;j keyString.length();j+)groupedSourceij = tempisecretMatrixj;private void getEncryptedString()/get the matrix which will be printed.groupSourceString();changeSourceMatix();char temp = new chargroupedSource.lengthkeyString.length();for (int i = 0;i groupedSource.length;i+)for (int j = 0;j keyString.length();j+)tempij = groupedSourceij;for (int i = 0;i groupedSource.length - 1;i+)for (int j = 0;j keyString.length();j+)groupedSourceij = tempisecretMatrixj;public void print1()/print the code have EncryptedgetEncryptedString();for(int i = 0;i keyString.length();i+)for(int j = 0;j groupedSource.length-1;j+)System.out.print(groupedSourceji);System.out.println();/decryption processprivate void groupEncryptedString()int sourceLen = sourceString.length();int keyLen = keyString.length();char temp = new charkeyLensourceLen/keyLen+1;for (int i = 0;i keyLen;i+)tempi = sourceString.substring(i*sourceLen/keyLen, (i+1)*sourceLen/keyLen).toCharArray();groupedSource = new charsourceLen/keyLen+1keyLen;for(int j = 0; j sourceLen/keyLen;j+)for (int i = 0;i keyLen;i+)groupedSourceji = tempij;private void getDecryptString()/get the matrix which will be printed.groupEncryptedString();char temp = new chargroupedSource.lengthkeyString.length();for (int i = 0;i groupedSource.length;i+)for (int j = 0;j keyString.length();j+)tempij = groupedSourceij;for (int i = 0;i groupedSource.length - 1;i+)for (int j = 0;j keyString.length();j+)groupedSourceij = tempisecretMatrixsecretMatrixj;public void print2()/print the code have EncryptedgetDecryptString();for(int j = 0;j groupedSource.length-1;j+)for(int i = 0;i keyString.length();i+)System.out.print(groupedSourceji);System.out.println();public static void main(String args)ReplaceDemo RD = null;Scanner scanin = null;System.out.println(please select the method that you want to operate:t(select “0” for encrypte and select “1” for decrypt.);scanin = new Scanner(System.in);int selected = scanin.nextInt();while(selected=0 | selected=1)RD = new ReplaceDemo();RD.getTheMatrix();if (selected = 0)RD.print1();elseRD.print2();System.out.println(p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论