note: anyone who exactly how its work can correct me. this is how i understand.
Download code -> With key
Download code -> No key
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | /* authors by hafiq Colummnar Transposition without key and possible decrytion any expert can advise me to improve this code...tq.. */ import java.util.*; import java.lang.*; public class CLCipher { public static void main(String[] args) { String str = "REPEAT ATTACK TONIGHT"; System.out.println("Encryption Text: " + encrypt(str)); String get[] = decrypt(encrypt(str)); System.out.println("Possible Decyrption Text:"); for (int x = 0; x < get.length; x++) System.out.println((x + 1) + ". " + get[x]); } public static String encrypt(String str) { String[] get = str.split(" "); int maxlen = 0; String init = ""; for (String a: get) { if (a.length() >= init.length()) init = a; } //if maxlen == 7, so the column must be 7 //get.length will be the row number; maxlen = init.length(); char[][] grid = new char[get.length][maxlen]; for (int x = 0; x < get.length; x++) { String gt = get[x]; for (int y = 0; y < maxlen; y++) { if (y != gt.length()) grid[x][y] = gt.charAt(y); else // can be replace with random alphabet also grid[x][y] = 'X'; } } StringBuilder cb = new StringBuilder(); for (int x = 0; x < maxlen; x++) { for (int y = 0; y < get.length; y++) { cb.append(grid[y][x]); } } return cb.toString(); } public static String[] decrypt(String str) { int getnum = str.length(); //get possible division number List < Integer > val = new ArrayList < Integer > (); for (int x = 1; x < getnum; x++) { if (getnum % x == 0) val.add(x); } String[] dec = new String[val.size()]; for (int x = 0; x < val.size(); x++) { int now = (int) val.get(x); String regex = "(?<=\\G.{" + now + "})"; String[] get = str.split(regex); //tranpose char grid[][] = new char[now][get.length]; for (int y = 0; y < get.length; y++) { String nw = get[y]; for (int z = 0; z < nw.length(); z++) { grid[z][y] = nw.charAt(z); } } //combine dec[x] = ""; for (int y = 0; y < now; y++) { for (int z = 0; z < get.length; z++) { dec[x] = dec[x] + grid[y][z]; } dec[x] = dec[x] + " "; } } return dec; } } |
No comments:
Post a Comment