Guest User

Untitled

a guest
Nov 22nd, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class SPN {
  4. // Private Fields
  5. private int x; // plaintext
  6. private int y; // ciphertext
  7. private int k; // key
  8. private int r; // number of rounds
  9. private int m; // number of substrings
  10. private int l; // length of substrings (# of bits in substring)
  11. private int[] sbox; // sbox operation
  12. private int[] perm; // permutation operation
  13. private ArrayList<String> results = new ArrayList<String>();
  14.  
  15. // Private Methods
  16.  
  17. // Converts integer to binary string of m*l bits
  18. private String Bin2String(int bin, int len){
  19. String binString = Integer.toBinaryString(bin);
  20. while (binString.length() < len){
  21. binString = "0" + binString;
  22. }
  23. return binString;
  24. }
  25.  
  26. // XOR Operation
  27. private int XOR(int w, int k) {
  28. return w^k;
  29. }
  30.  
  31. // Sbox Operation
  32. private int Sbox(int u) {
  33. int v = 0;
  34. int mask = (int) (Math.pow(2, this.l) - 1);
  35. for(int i = 0; i < this.m; i++) {
  36. v += this.sbox[(mask & u) >> i*this.l] << i*this.l;
  37. mask = mask << this.l;
  38. }
  39. return v;
  40. }
  41.  
  42. // Permutation Operation
  43. private int Permutation(int v) {
  44. String temp = Bin2String(v, this.l*this.m);
  45. String w = Bin2String(0, this.l*this.m);
  46. for(int i=0; i < this.perm.length; i++) {
  47. w = w.substring(0, this.perm[i]) + temp.charAt(i) + w.substring(this.perm[i]+1);
  48. }
  49. return Integer.parseInt(w, 2);
  50. }
  51.  
  52. // Default Key Scheduler
  53. // for given round i
  54. private int KeyScheduler(int i){
  55. String key = Bin2String(this.k, this.l*(this.r + this.m));
  56. int start = (i-1)*this.l;
  57. int end = start + this.l*this.m;
  58. return Integer.parseInt(key.substring(start, end), 2);
  59. }
  60.  
  61. // Reset results to perform another encryption/decryption
  62. private void reset() {
  63. this.results.clear();
  64. this.results.add("Stage: \t\t Binary: \t\t Hex: ");
  65. }
  66.  
  67. // Print spaces between binary strings for easy parsing
  68. private String insertSpaces(String str) {
  69. String temp = "";
  70. for(int i = 1; i < this.m; i++) {
  71. temp += str.substring((i-1)*this.l, i*this.l) + " ";
  72. }
  73. return temp + str.substring((this.m-1)*this.l);
  74. }
  75.  
  76. // Convert binary to Hex Strings
  77. private String Bin2HexString(int bin) {
  78. String hex = Integer.toHexString(bin);
  79. while (hex.length() < 4) {
  80. hex = "0" + hex;
  81. }
  82. return hex;
  83. }
  84. // Public Methods
  85.  
  86. // Main Constructor
  87. public SPN(int x, int y, int k, int r, int m, int l, int[] sbox, int[] perm) {
  88. this.x = x;
  89. this.y = y;
  90. this.k = k;
  91. this.r = r;
  92. this.m = m;
  93. this.l = l;
  94. this.sbox = sbox;
  95. this.perm = perm;
  96. this.results.add("Stage: \t\t Binary: \t\t Hex: ");
  97. }
  98.  
  99. // Set new plaintext
  100. public void setPlaintext(int x) {
  101. this.x = x;
  102. }
  103.  
  104. // Set new ciphertext
  105. public void setCiphertext(int y) {
  106. this.y = y;
  107. }
  108.  
  109. // Set new key
  110. public void setKey(String k) {
  111. this.k = Integer.parseUnsignedInt(k, 2);
  112. }
  113.  
  114. // Encrypt with given plaintext
  115. public void encrypt(){
  116. this.reset();
  117. int len = this.l*this.m;
  118. int w = this.x;
  119. this.results.add("X: \t\t " + insertSpaces(Bin2String(w, len)) + "\t "
  120. + Bin2HexString(w));
  121. for (int i = 1; i < r; i++){
  122. int key = KeyScheduler(i);
  123. this.results.add("K" + i + ": \t\t " + insertSpaces(Bin2String(key, len))
  124. + "\t " + Bin2HexString(key));
  125. int u = XOR(w, key);
  126. this.results.add("U" + i + ": \t\t " + insertSpaces(Bin2String(u, len))
  127. + "\t " + Bin2HexString(u));
  128. int v = Sbox(u);
  129. this.results.add("V" + i + ": \t\t " + insertSpaces(Bin2String(v, len))
  130. + "\t " + Bin2HexString(v));
  131. w = Permutation(v);
  132. this.results.add("W" + i + ": \t\t " + insertSpaces(Bin2String(w, len))
  133. + "\t " + Bin2HexString(w));
  134. }
  135. this.results.add("K" + this.r + ": \t\t "
  136. + insertSpaces(Bin2String(KeyScheduler(this.r), len)) + "\t "
  137. + Bin2HexString(KeyScheduler(this.r)));
  138. int u = XOR(w, KeyScheduler(this.r));
  139. this.results.add("U" + this.r + ": \t\t " + insertSpaces(Bin2String(u, len))
  140. + "\t " + Bin2HexString(u));
  141. int v = Sbox(u);
  142. this.results.add("V" + this.r + ": \t\t " + insertSpaces(Bin2String(v, len))
  143. + "\t " + Bin2HexString(v));
  144. this.results.add("K" + (this.r + 1) + ": \t\t "
  145. + insertSpaces(Bin2String(KeyScheduler(this.r + 1), len))
  146. + "\t " + Bin2HexString(KeyScheduler(this.r + 1)));
  147. this.y = XOR(v, KeyScheduler(this.r + 1));
  148. this.results.add("Y: \t\t " + insertSpaces(Bin2String(this.y, len))
  149. + "\t " + Bin2HexString(this.y));
  150. }
  151.  
  152. // Decrypt with given ciphertext
  153. public void decrypt(){
  154. this.reset();
  155. int len = this.l*this.m;
  156. int y = this.y;
  157. this.results.add("Y: \t\t " + insertSpaces(Bin2String(y, len)) + "\t "
  158. + Bin2HexString(y));
  159. int v = XOR(y, KeyScheduler(this.r + 1));
  160. this.results.add("K" + (this.r + 1) + ": \t\t "
  161. + insertSpaces(Bin2String(KeyScheduler(this.r + 1), len))
  162. + "\t " + Bin2HexString(KeyScheduler(this.r + 1)));
  163. this.results.add("V" + this.r + ": \t\t " + insertSpaces(Bin2String(v, len))
  164. + "\t " + Bin2HexString(v));
  165. int u = Sbox(v); // inverse sbox
  166. this.results.add("U" + this.r + ": \t\t " + insertSpaces(Bin2String(u, len))
  167. + "\t " + Bin2HexString(u));
  168. this.results.add("K" + this.r + ": \t\t "
  169. + insertSpaces(Bin2String(KeyScheduler(this.r), len)) + "\t "
  170. + Bin2HexString(KeyScheduler(this.r)));
  171. int w = XOR(u, KeyScheduler(this.r));
  172. this.results.add("W" + (this.r-1) + ": \t\t " + insertSpaces(Bin2String(w, len))
  173. + "\t " + Bin2HexString(w));
  174. for (int i = r - 1; i >= 1; i--){
  175. int key = KeyScheduler(i);
  176. v = Permutation(w);
  177. this.results.add("V" + i + ": \t\t " + insertSpaces(Bin2String(v, len))
  178. + "\t " + Bin2HexString(v));
  179. u = Sbox(v);
  180. this.results.add("U" + i + ": \t\t " + insertSpaces(Bin2String(u, len))
  181. + "\t " + Bin2HexString(u));
  182. this.results.add("K" + i + ": \t\t " + insertSpaces(Bin2String(key, len))
  183. + "\t " + Bin2HexString(key));
  184. w = XOR(u, KeyScheduler(i));
  185. if(i != 1) {
  186. this.results.add("W" + (i-1) + ": \t\t " + insertSpaces(Bin2String(w, len))
  187. + "\t " + Bin2HexString(w));
  188. }
  189. }
  190. int x = w;
  191. this.results.add("X: \t\t " + insertSpaces(Bin2String(x, len)) + "\t "
  192. + Bin2HexString(x));
  193. }
  194.  
  195. // Display only plaintext and resulting ciphertext
  196. public void displaySimpleResults() {
  197. System.out.println("Plaintext: " + Bin2String(x, this.l*this.m)
  198. + "\nCiphertext: " + Bin2String(y, this.l*this.m));
  199. }
  200.  
  201. // Display detailed internal results of SPN
  202. public void displayDetailedResults() {
  203. for(int i = 0; i < this.results.size(); i++) {
  204. System.out.println(this.results.get(i));
  205. }
  206. }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment