Guest User

Untitled

a guest
Feb 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. BigInteger [] K ; //128 bits key
  2. private String plainText;
  3. public static final BigInteger delta = new BigInteger("9e3779b9",16);
  4.  
  5.  
  6. //constructor receives a string of plaintext and 128 bit key in hexadecimal
  7. public TEA(String plainText, String key)
  8. {
  9. parseKey(key);
  10.  
  11.  
  12. }
  13.  
  14. //constructor receives a hexadecimal
  15. public TEA(String key)
  16. {
  17.  
  18. parseKey(key);
  19.  
  20. }
  21.  
  22. //parses a 128 bit key, given in hexadecimal form, and store its value in 4 integers (total of 128 bits),
  23. private void parseKey(String key)
  24. {
  25. if(key.substring(0,2).equals("0x"))
  26. key= key.substring(2);
  27.  
  28. //validating input
  29. if(key.length() != 32)
  30. {
  31. System.out.println("Invalid key size!");
  32. return;
  33. }
  34.  
  35.  
  36. //dividing the key into 4 strings
  37. String[] kStr = new String[4];
  38. int index=-1;
  39. for(int i=0; i<key.length(); i++)
  40. {
  41. if(i%8 == 0)
  42. {
  43. index++;
  44. kStr[index]="";
  45.  
  46. }
  47. kStr[index] = kStr[index] + key.charAt(i);
  48. }
  49.  
  50.  
  51. //converting the 4 hex strings into 4 integers
  52. K= new BigInteger[4];
  53. for(int i=0; i<4; i++)
  54. K[i] = new BigInteger(kStr[i], 16);
  55.  
  56. }
  57.  
  58. //receives a plaintext block of 64 bits in hexadecimal to be encrypted
  59. //returns the cipher block
  60. String encryptBlock(String plainTextBlock)
  61. {
  62. if(plainTextBlock.substring(0,2).equals("0x"))
  63. plainTextBlock= plainTextBlock.substring(2);
  64.  
  65. //validating input
  66. if(plainTextBlock.length()!=16)
  67. {
  68. System.out.println("Invalid block size!");
  69. return null;
  70.  
  71. }
  72.  
  73. //separating the string block into left and right blocks
  74. String LStr = plainTextBlock.substring(0, 8); //left block (32 bit)
  75. String RStr = plainTextBlock.substring(8); //right block (32 bit)
  76.  
  77. //converting left and right blocks to integers
  78. BigInteger L = new BigInteger(LStr, 16);
  79. BigInteger R = new BigInteger(RStr, 16);
  80.  
  81.  
  82. BigInteger sum= new BigInteger("0");
  83. //32 rounds
  84. for(int i=0; i<32; i++)
  85. {
  86. sum = sum.add(delta);
  87. L= sum(L, (sum(shiftLeft(R,4),K[0])) .xor(sum(R,sum)) .xor(sum(shiftRight(R,5),K[1]))) ;
  88. R= sum(R, (sum(shiftLeft(L,4),K[2])) .xor(sum(L,sum)) .xor(sum(shiftRight(L,5),K[3]))) ;
  89.  
  90. //R= R.add( (shiftLeft(R,4).add(K[2])).xor(L.add(sum)).xor(shiftRight(L,5).add(K[3])) );
  91.  
  92.  
  93. }
  94.  
  95. //joining back the blocks as hex
  96. String cipherBlock = "0x"+L.toString(16)+R.toString(16)+"";
  97.  
  98.  
  99. return cipherBlock;
  100. }
  101.  
  102.  
  103. //receives a ciphertext block of 64 bits in hexadecimal to be decrypted
  104. //returns the plaintext block
  105. String decryptBlock(String cipherBlock)
  106. {
  107. if(cipherBlock.substring(0,2).equals("0x"))
  108. cipherBlock= cipherBlock.substring(2);
  109.  
  110. //validating input
  111. if(cipherBlock.length()!=16)
  112. {
  113. System.out.println("Invalid block size!");
  114. return null;
  115.  
  116. }
  117.  
  118. //separating the string block into left and right blocks
  119. String LStr = cipherBlock.substring(0, 8); //left block (32 bit)
  120. String RStr = cipherBlock.substring(8); //right block (32 bit)
  121.  
  122. //converting left and right blocks to integers
  123. BigInteger L = new BigInteger(LStr, 16);
  124. BigInteger R = new BigInteger(RStr, 16);
  125.  
  126. BigInteger sum= shiftLeft(delta,5);
  127. //32 rounds
  128. for(int i=0; i<32; i++)
  129. {
  130.  
  131. R= subtract(R, (sum(shiftLeft(L,4),K[2])) .xor(sum(L,sum)) .xor(sum(shiftRight(L,5),K[3]))) ;
  132. L= subtract(L, (sum(shiftLeft(R,4),K[0])) .xor(sum(R,sum)) .xor(sum(shiftRight(R,5),K[1]))) ;
  133.  
  134.  
  135. //R= R.subtract( (L.shiftLeft(4).add(K[2])).xor(L.add(sum)).xor(L.shiftRight(5).add(K[3])) );
  136. //L= L.subtract( (R.shiftLeft(4).add(K[0])).xor(R.add(sum)).xor(R.shiftRight(5).add(K[1])) );
  137. sum = sum.subtract(delta);
  138. }
  139.  
  140. //joining back the blocks as hex
  141. String plainTextBlock = "0x"+L.toString(16)+R.toString(16)+"";
  142.  
  143.  
  144. return plainTextBlock;
  145. }
  146.  
  147.  
  148. private BigInteger shiftLeft(BigInteger x, int steps)
  149. {
  150.  
  151. BigInteger shifted=null;
  152. boolean negative =false;
  153.  
  154. String xStr = x.toString(2);
  155.  
  156. //removing negative sign while shifting (currently)
  157. if(xStr.charAt(0)=='-')
  158. {
  159. negative= true;
  160. xStr = xStr.substring(1);
  161. }
  162.  
  163.  
  164. int additionalSize = 32- xStr.length();
  165.  
  166. for(int i=0; i<additionalSize; i++)
  167. xStr= "0"+xStr;
  168.  
  169.  
  170.  
  171. for(int i=0; i<steps; i++)
  172. {
  173. xStr = xStr.substring(1);
  174. xStr = xStr+"0";
  175. }
  176.  
  177. //one last addition of negative sign if the number is negative
  178. if(negative==true)
  179. xStr= "-"+xStr;
  180.  
  181. //System.out.println(xStr);
  182. shifted = new BigInteger(xStr,2);
  183.  
  184. return shifted;
  185. }
  186.  
  187.  
  188. private BigInteger shiftRight(BigInteger x, int steps)
  189. {
  190. BigInteger shifted=null;
  191. boolean negative = false;
  192.  
  193. String xStr = x.toString(2);
  194.  
  195. //removing negative sign while shifting (currently)
  196. if(xStr.charAt(0)=='-')
  197. {
  198. negative= true;
  199. xStr = xStr.substring(1);
  200. }
  201.  
  202. int additionalSize = 32- xStr.length();
  203.  
  204. for(int i=0; i<additionalSize; i++)
  205. xStr= "0"+xStr;
  206.  
  207.  
  208. for(int i=0; i<steps; i++)
  209. {
  210. xStr = xStr.substring(0,xStr.length()-1);
  211. xStr = "0"+xStr;
  212. }
  213.  
  214. //one last addition of negative sign if the number is negative
  215. if(negative==true)
  216. xStr= "-"+xStr;
  217.  
  218. shifted = new BigInteger(xStr,2);
  219.  
  220. return shifted;
  221. }
  222.  
  223. private BigInteger sum(BigInteger a, BigInteger b)
  224. {
  225.  
  226. BigInteger sum = a.add(b);
  227. String sumStr = sum.toString(2);
  228. if(sumStr.length()>32)
  229. {
  230. int diff = sumStr.length()- 32;
  231. sumStr = sumStr.substring(diff);
  232. }
  233.  
  234. BigInteger newSum = new BigInteger(sumStr,2);
  235.  
  236. return newSum;
  237. }
  238.  
  239. private BigInteger subtract(BigInteger a, BigInteger b)
  240. {
  241.  
  242. BigInteger sub = a.subtract(b);
  243.  
  244. String subStr = sub.toString(2);
  245. if(subStr.length()>32)
  246. {
  247. int diff = subStr.length()- 32;
  248. subStr = subStr.substring(diff);
  249. }
  250.  
  251. BigInteger newSub = new BigInteger(subStr,2);
  252.  
  253. return newSub;
  254. }
  255.  
  256.  
  257.  
  258. public static void main(String[] args)
  259. {
  260.  
  261. String plainText="0x0123456789ABCDEF";
  262. String key= "0xA56BABCD00000000FFFFFFFFABCDEF01";
  263. TEA tea = new TEA(key);
  264. String cipherText = tea.encryptBlock(plainText);
  265. System.out.println("Original Plain Text:"+plainText);
  266. System.out.println("CipherText:"+cipherText);
  267. System.out.println("Decrypted CipherText is:"+tea.decryptBlock(cipherText));
  268.  
  269.  
  270.  
  271.  
  272. }
Add Comment
Please, Sign In to add comment