fork(83) download
  1. class HC128
  2. {
  3. public static void main (String[] args) throws java.lang.Exception
  4. {
  5. String iv_srt = "@#$$54214AEFDCAE";
  6. String key_srt = "AAAAAAAAqweAAAAT";
  7. HC128 hc_enc = new HC128(iv_srt.getBytes(), key_srt.getBytes());
  8.  
  9. String s = "Hello World";
  10.  
  11. byte[] ed = encrypt(hc_enc, s.getBytes());
  12.  
  13. hc_enc.reset();
  14.  
  15. byte[] ed33 = encrypt(hc_enc, ed);
  16. System.out.println(new String(ed33));
  17. }
  18.  
  19. public static byte[] encrypt(HC128 hc, byte[] data) {
  20.  
  21. for (int i = 0; i < data.length; i++) {
  22. data[i] = hc.returnByte(data[i]);
  23. }
  24. return data;
  25. }
  26.  
  27. private static int f1(int x) {
  28. return Integer.rotateRight(x, 7) ^ Integer.rotateRight(x, 18) ^ (x >>> 3);
  29. }
  30.  
  31. private static int f2(int x) {
  32. return Integer.rotateRight(x, 17) ^ Integer.rotateRight(x, 19) ^ (x >>> 10);
  33. }
  34.  
  35. private int g1(int x, int y, int z) {
  36. return (Integer.rotateRight(x, 10) ^ Integer.rotateRight(z, 23))
  37. + Integer.rotateRight(y, 8);
  38. }
  39.  
  40. private int g2(int x, int y, int z) {
  41. return (Integer.rotateLeft(x, 10) ^ Integer.rotateLeft(z, 23))
  42. + Integer.rotateLeft(y, 8);
  43. }
  44.  
  45. private int h1(int x) {
  46. return q[x & 0xFF] + q[((x >> 16) & 0xFF) + 256];
  47. }
  48.  
  49. private int h2(int x) {
  50. return p[x & 0xFF] + p[((x >> 16) & 0xFF) + 256];
  51. }
  52.  
  53. private static int mod1024(int x) {
  54. return x & 0x3FF;
  55. }
  56.  
  57. private static int mod512(int x) {
  58. return x & 0x1FF;
  59. }
  60.  
  61. private static int dim(int x, int y) {
  62. return mod512(x - y);
  63. }
  64.  
  65. private int step() {
  66. int j = mod512(cnt);
  67. int ret;
  68. if (cnt < 512) {
  69. p[j] += g1(p[dim(j, 3)], p[dim(j, 10)], p[dim(j, 511)]);
  70. ret = h1(p[dim(j, 12)]) ^ p[j];
  71. } else {
  72. q[j] += g2(q[dim(j, 3)], q[dim(j, 10)], q[dim(j, 511)]);
  73. ret = h2(q[dim(j, 12)]) ^ q[j];
  74. }
  75. cnt = mod1024(cnt + 1);
  76.  
  77. return ret;
  78. }
  79.  
  80. private byte[] key, iv;
  81. private int[] p = new int[512];
  82. private int[] q = new int[512];
  83. private int cnt = 0;
  84. private byte[] buf = new byte[4];
  85. private int idx = 0;
  86.  
  87. public int getCnt() {
  88. return cnt;
  89. }
  90.  
  91. public int getIdx() {
  92. return idx;
  93. }
  94.  
  95. public void setIdx(int idx) {
  96. this.idx = idx;
  97. }
  98.  
  99. public void setCnt(int cnt) {
  100. this.cnt = cnt;
  101. }
  102.  
  103. public HC128(byte[] iv, byte[] key) {
  104. this.iv = iv;
  105. this.key = key;
  106. init();
  107. }
  108.  
  109. private void init() {
  110. if (key.length != 16) {
  111. throw new java.lang.IllegalArgumentException(
  112. "The key must be 128 bit long");
  113. }
  114. cnt = 0;
  115. int[] w = new int[1280];
  116. for (int i = 0; i < 16; i++) {
  117. w[i >> 3] |= key[i] << (i & 0x7);
  118. }
  119. System.arraycopy(w, 0, w, 4, 4);
  120.  
  121. for (int i = 0; i < Math.min(16, iv.length); i++) {
  122. w[(i >> 3) + 8] |= iv[i] << (i & 0x7);
  123. }
  124. System.arraycopy(w, 8, w, 12, 4);
  125.  
  126. for (int i = 16; i < 1280; i++) {
  127. w[i] = f2(w[i - 2]) + w[i - 7] + f1(w[i - 15]) + w[i - 16] + i;
  128. }
  129.  
  130. System.arraycopy(w, 256, p, 0, 512);
  131. System.arraycopy(w, 768, q, 0, 512);
  132.  
  133. for (int i = 0; i < 512; i++) {
  134. p[i] = step();
  135. }
  136. for (int i = 0; i < 512; i++) {
  137. q[i] = step();
  138. }
  139.  
  140. cnt = 0;
  141. }
  142.  
  143. private byte getByte() {
  144. if (idx == 0) {
  145. int step = step();
  146. buf[3] = (byte) (step & 0xFF);
  147. step >>= 8;
  148. buf[2] = (byte) (step & 0xFF);
  149. step >>= 8;
  150. buf[1] = (byte) (step & 0xFF);
  151. step >>= 8;
  152. buf[0] = (byte) (step & 0xFF);
  153. }
  154. byte ret = buf[idx];
  155. idx = idx + 1 & 0x3;
  156. return ret;
  157. }
  158.  
  159. public void reset() {
  160. idx = 0;
  161. init();
  162. }
  163.  
  164. public byte returnByte(byte in) {
  165. return (byte) (in ^ getByte());
  166. }
  167. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
Hello World