class="p1">有短句“Sun Yat-sen University is a vibrant community of scholarship”,请用下列方法加密:
– 简单代替密码(移位密码),k=5,q=26;
– 多表代替密码,其中m=6, K=cipher;
– 纵行置换密码,假定每行10个字节。
请写出加密后密文。
?
加密结果:
简单代替加密:Xzs Dfy-xjs Zsnajwxnyd nx f angwfsy htrrzsnyd tk xhmtqfwxmnu
多表代替加密:Ucc Crv-hlr Wvxciiuqif zu p zzdzpux ewbtyekbn sw aroscczhomg
纵向置换加密:Sntbucu yrnhnU aio nintlYistyaav ? rteacos-r ofhssvm ieiimsp
?
?
本作业是使用java编程求解的,以为是求解算法(java语言描述):
?
/* * 简单代替加密 */ public class Ssc { private String name; private int k; private int q; public Ssc(){ this.k = 5; this.q = 26; this.name = "简单代替加密"; } /** * k为位移量,q为字母表长度 * @param k * @param q */ public Ssc(int k, int q){ this.k = k; this.q = q; this.name = "简单代替加密"; } /** * 加密 * @param str * @return */ public String encrypt(String str){ String code = ""; char[] c = str.toCharArray(); for (int i = 0; i < c.length; i++) { int hc = (int)c[i]; if(hc >= 97 && hc <= 122){ int t = hc - 97; int tt = (t + k) % q; hc = 97 + tt; } if(hc >= 65 && hc <= 90){ int t = hc - 65; int tt = (t + k) % q; hc = 65 + tt; } char temp = (char)hc; code += temp; } return code; } public int getK() { return k; } public void setK(int k) { this.k = k; } public int getQ() { return q; } public void setQ(int q) { this.q = q; } public String getName() { return name; } public void setName(String name) { this.name = name; } } /* * 多表代替加密 */ public class Psc { private String name; private int m; private String K; public Psc(){ this.m = 6; this.K = "run"; this.name = "多表代替加密"; } /** * m为密钥长度,K为密钥 * @param m * @param K */ public Psc(int m, String K){ this.m = m; this.K = K; this.name = "多表代替加密"; } /** * 加密 * @param str * @return */ public String encrypt(String str){ String code = ""; char[] c = str.toCharArray(); char[] k = K.toCharArray(); for (int i = 0; i < c.length; i++) { int hc = (int)c[i]; int ki = i; if(i >= k.length){ ki = i % k.length; } int kt = k[ki] - 97; if(hc >= 97 && hc <= 122){ int t = hc - 97; int tt = (t + kt) % 26; hc = 97 + tt; } if(hc >= 65 && hc <= 90){ int t = hc - 65; int tt = (t + kt) % 26; hc = 65 + tt; } char temp = (char) hc; code += temp; } return code; } public int getM() { return m; } public void setM(int m) { this.m = m; } public String getK() { return K; } public void setK(String k) { K = k; } public String getName() { return name; } public void setName(String name) { this.name = name; } } /* * 纵向置换加密 */ public class Wrc { private String name; private int skip; public Wrc(){ this.skip = 10; this.name = "纵向置换加密"; } /** * skip为间隔 * @param skip */ public Wrc(int skip){ this.skip = skip; this.name = "纵向置换加密"; } /** * 加密 * @param str * @return */ public String encrypt(String str){ String code = ""; int len = str.length(); int group_len = ((len%skip)==0)? len/skip : len/skip + 1; char groups[][] = new char[100][100]; char[] c = str.toCharArray(); int cc = c.length / skip; int cr = c.length % skip; if(cr != 0){ for (int i = 0; i < skip - cr; i++) { c[c.length + i] = 0; } } for (int i = 0; i < group_len; i++) { for (int j = 0; j < skip; j++) { groups[i][j] = c[i*skip + j]; // System.out.print(groups[i][j]); } } for (int j = 0; j < skip; j++){ for (int i = 0; i < group_len; i++) { code += groups[i][j]; } } return code; } public int getSkip() { return skip; } public void setSkip(int skip) { this.skip = skip; } public String getName() { return name; } public void setName(String name) { this.name = name; } } /* * main方法 */ public class Main { public static void main(String[] args) { String str ="Sun Yat-sen University is a vibrant community of scholarship"; Ssc ssc = new Ssc(5, 26); System.out.println(ssc.getName() + ":" + ssc.encrypt(str)); Psc psc = new Psc(6, "cipher"); System.out.println(psc.getName() + ":" + psc.encrypt(str) ); Wrc wrc = new Wrc(10); System.out.println(wrc.getName() + ":" + wrc.encrypt(str)); } }
?