標籤:

大神們幫忙解一下這串密碼,應該是四方或者二方密碼(我也不知道對不對)?

一個朋友寫給我的,困擾我好幾天了,跪請哥哥姐姐們幫幫忙

密鑰:right password


謝邀,基本可以確定,這是用四方密碼加密的消息。

關於四方密碼,具體鏈接如下:

四方密碼_百度百科。

四方密碼需要剔除一個字母,具體思路是遍歷字元串找出沒有出現的字母。先寫程序分析一下:

String msg = "YH HI JB ZA CN DH OE SK XB EM GG,EO LB EL GG IB OO JI IZ NY EA JB EP QR JB QK,HI JB GB ZM QH NB HT DS GR";

Set& set = new HashSet&();

for (int i = 0; i &< msg.length(); i++)

if (msg.charAt(i) != " ")

set.add(msg.charAt(i));

for (char j = "A"; j &<= "Z"; j++)

if (!set.contains(j))

System.out.println(j);

輸出如下:

F

U

V

W

也就是上述一個字母已經被剔除。

接下來是實現四方加密的代碼,分為三個類,一個加密表SquareGrid,一個加密器SquareCipher以及一個測試類。

加密表主要用於生成一個加密矩陣,給出兩個字元它能把它轉化成加密後的字元,同時給出加密後的字元它也能夠還原。加密器主要用於創建加密表實例同時執行加密解密方法。

public class SquareGrid {

private char[] g0;

private char[] g1;

private char[] g2;

private static final char[] a2z = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",

"q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

private static final char[] A2Z = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",

"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

public void init(char c, String lts, String rbs) {

g0 = new char[25];

g1 = new char[25];

g2 = new char[25];

lts = lts.toUpperCase().replaceAll("[^A-Z]", "");

rbs = rbs.toUpperCase().replaceAll("[^A-Z]", "");

int j = 0;

for (int i = j; i &< a2z.length; i++) {

if (a2z[i] != c j &< 25) {

g0[j] = a2z[i];

j++;

}

}

char uC = (char) (c - 32);

occupy(g1, uC, lts);

occupy(g2, uC, rbs);

}

private static void occupy(char[] cs, char c, String s) {

int j = 0;

char temp;

for (int i = 0; i &< s.length(); i++) {

temp = s.charAt(i);

if (!contains(cs, temp) c != temp j &< cs.length) {

cs[j] = temp;

j++;

}

}

for (int i = 0; i &< A2Z.length; i++) {

if (!contains(cs, A2Z[i]) c != A2Z[i] j &< cs.length) {

cs[j] = A2Z[i];

j++;

}

}

}

private static boolean contains(char[] cs, char c) {

int i = 0;

while (i &< cs.length cs[i] != "") {

if (cs[i] == c)

return true;

i++;

}

return false;

}

public char[] encrypt(char c1, char c2) {

int i1 = 5, j1 = 5, i2 = 5, j2 = 5;

for (int i = 0; i &< 25; i++) {

if (g0[i] == c1) {

i1 = i / 5;

j1 = i % 5;

}

if (g0[i] == c2) {

i2 = i / 5;

j2 = i % 5;

}

}

if (i1 == 5 || i2 == 5)

return null;

char[] cs = new char[2];

cs[0] = g1[5 * i1 + j2];

cs[1] = g2[5 * i2 + j1];

return cs;

}

public char[] decrypt(char c1, char c2) {

int i1 = 5, j1 = 5, i2 = 5, j2 = 5;

for (int i = 0; i &< 25; i++) {

if (g1[i] == c1) {

i1 = i / 5;

j1 = i % 5;

}

if (g2[i] == c2) {

i2 = i / 5;

j2 = i % 5;

}

}

if (i1 == 5 || i2 == 5)

return null;

char[] cs = new char[2];

cs[0] = g0[5 * i1 + j2];

cs[1] = g0[5 * i2 + j1];

return cs;

}

@Override

public String toString() {

StringBuffer sb = new StringBuffer();

for (int i = 0; i &< 10; i++) {

if (i &< 5) {

for (int j = 0; j &< 10; j++) {

if (j &< 5)

sb.append(g0[5 * i + j]);

else

sb.append(g1[5 * i + j - 5]);

if (j != 9)

sb.append(",");

}

} else {

for (int j = 0; j &< 10; j++) {

if (j &< 5)

sb.append(g2[5 * (i - 5) + j]);

else

sb.append(g0[5 * (i - 5) + j - 5]);

if (j != 9)

sb.append(",");

}

}

sb.append("
");

}

return sb.toString();

}

}

//===============================================================

public class SquareCipher {

public static SquareGrid getGridInstance(String kickOut, String key1, String key2) {

SquareGrid grid = new SquareGrid();

kickOut = kickOut.toLowerCase().replaceAll("[^a-z]", "");

grid.init(kickOut.charAt(0), key1, key2);

return grid;

}

public static String encrypt(SquareGrid grid, String msg) {

if (msg == null || "".equals(msg))

return null;

msg = msg.toLowerCase().replaceAll("[^a-z]", "");

StringBuffer sb = new StringBuffer();

char[] temp;

int i = 0;

while (i + 1 &< msg.length()) {

temp = grid.encrypt(msg.charAt(i), msg.charAt(i + 1));

sb.append(temp);

sb.append(" ");

i += 2;

}

return sb.toString();

}

public static String decrypt(SquareGrid grid, String cipher) {

if (cipher == null || "".equals(cipher))

return null;

cipher = cipher.toUpperCase().replaceAll("[^A-Z]", "");

StringBuffer sb = new StringBuffer();

char[] temp;

int i = 0;

while (i + 1 &< cipher.length()) {

temp = grid.decrypt(cipher.charAt(i), cipher.charAt(i + 1));

sb.append(temp);

i += 2;

}

return sb.toString();

}

}

//============================================================

public static void main(String[] args) {

String msg = "YH HI JB ZA CN DH OE SK XB EM GG,EO LB EL GG IB OO JI IZ NY EA JB EP QR JB QK,HI JB GB ZM QH NB HT DS GR";

String[] msgs = msg.split(",");

Set& set = new HashSet&();

for (int i = 0; i &< msg.length(); i++)

if (msg.charAt(i) != " ")

set.add(msg.charAt(i));

for (char j = "A"; j &<= "Z"; j++)

if (!set.contains(j))

System.out.println(j);

SquareGrid grid = SquareCipher.getGridInstance("F", "right", "password");

System.out.println(grid);

// String cipher = SquareCipher.encrypt(grid, msg);

// System.out.println(cipher);

for (int i = 0; i &< msgs.length; i++)

System.out.println(SquareCipher.decrypt(grid, msgs[i]));

}

用四個可能被剔除的字母去嘗試,採用F的時候有如下結果:

wocongweishouguoxihuan

keniguanchuanlewozhenggeqingun

congciwurnnketidai

仔細讀吧,大概是:

我從未說過喜歡,可你貫穿了我整個青春,從此無可替代。

有一些不通順應該是因為舍掉了F的原因或者程序bug,我隱約感受到一段愛情,

加油!


推薦閱讀:

TAG:加密解密 |