[C#] RC6 Encryption Class


SUBMITTED BY: eddysix

DATE: March 15, 2017, 6:54 p.m.

FORMAT: C#

SIZE: 6.7 kB

HITS: 645

  1. private static int w = 32, r = 20;
  2. private static readonly double e = Math.E;
  3. private const double goldenRatio = 1.6180339887496482;
  4. private static int Pw = unchecked((int)0xb7e15163), Qw = unchecked((int)0x9e3779b9);
  5. private static int[] S;
  6. private static int[] convBytesWords(sbyte[] key, int u, int c)
  7. {
  8. int[] tmp = new int[c];
  9. for (int i = 0; i < tmp.Length; i++)
  10. {
  11. tmp[i] = 0;
  12. }
  13. for (int i = 0, off = 0; i < c; i++)
  14. {
  15. tmp[i] = ((key[off++] & 0xFF)) | ((key[off++] & 0xFF) << 8) | ((key[off++] & 0xFF) << 16) | ((key[off++] & 0xFF) << 24);
  16. }
  17. return tmp;
  18. }
  19. private static int[] generateSubkeys(sbyte[] key)
  20. {
  21. int u = w / 8;
  22. int c = key.Length / u;
  23. int t = 2 * r + 4;
  24. int[] L = convBytesWords(key, u, c);
  25. int[] S = new int[t];
  26. S[0] = Pw;
  27. for (int i = 1; i < t; i++)
  28. {
  29. S[i] = S[i - 1] + Qw;
  30. }
  31. int A = 0;
  32. int B = 0;
  33. int k = 0, j = 0;
  34. int v = 3 * Math.Max(c, t);
  35. for (int i = 0; i < v; i++)
  36. {
  37. A = S[k] = rotl((S[k] + A + B), 3);
  38. B = L[j] = rotl(L[j] + A + B, A + B);
  39. k = (k + 1) % t;
  40. j = (j + 1) % c;
  41. }
  42. return S;
  43. }
  44. private static sbyte[] decryptBloc(sbyte[] input)
  45. {
  46. sbyte[] tmp = new sbyte[input.Length];
  47. int t, u;
  48. int aux;
  49. int[] data = new int[input.Length / 4];
  50. for (int i = 0; i < data.Length; i++)
  51. {
  52. data[i] = 0;
  53. }
  54. int off = 0;
  55. for (int i = 0; i < data.Length; i++)
  56. {
  57. data[i] = ((input[off++] & 0xff)) | ((input[off++] & 0xff) << 8) | ((input[off++] & 0xff) << 16) | ((input[off++] & 0xff) << 24);
  58. }
  59. int A = data[0], B = data[1], C = data[2], D = data[3];
  60. C = C - S[2 * r + 3];
  61. A = A - S[2 * r + 2];
  62. for (int i = r; i >= 1; i--)
  63. {
  64. aux = D;
  65. D = C;
  66. C = B;
  67. B = A;
  68. A = aux;
  69. u = rotl(D * (2 * D + 1), 5);
  70. t = rotl(B * (2 * B + 1), 5);
  71. C = rotr(C - S[2 * i + 1], t) ^ u;
  72. A = rotr(A - S[2 * i], u) ^ t;
  73. }
  74. D = D - S[1];
  75. B = B - S[0];
  76. data[0] = A;
  77. data[1] = B;
  78. data[2] = C;
  79. data[3] = D;
  80. for (int i = 0; i < tmp.Length; i++)
  81. {
  82. tmp[i] = unchecked((sbyte)(((int)((uint)data[i / 4] >> (i % 4) * 8)) & 0xff));
  83. }
  84. return tmp;
  85. }
  86. private static sbyte[] encryptBloc(sbyte[] input)
  87. {
  88. sbyte[] tmp = new sbyte[input.Length];
  89. int t, u;
  90. int aux;
  91. int[] data = new int[input.Length / 4];
  92. for (int i = 0; i < data.Length; i++)
  93. {
  94. data[i] = 0;
  95. }
  96. int off = 0;
  97. for (int i = 0; i < data.Length; i++)
  98. {
  99. data[i] = ((input[off++] & 0xff)) | ((input[off++] & 0xff) << 8) | ((input[off++] & 0xff) << 16) | ((input[off++] & 0xff) << 24);
  100. }
  101. int A = data[0], B = data[1], C = data[2], D = data[3];
  102. B = B + S[0];
  103. D = D + S[1];
  104. for (int i = 1; i <= r; i++)
  105. {
  106. t = rotl(B * (2 * B + 1), 5);
  107. u = rotl(D * (2 * D + 1), 5);
  108. A = rotl(A ^ t, u) + S[2 * i];
  109. C = rotl(C ^ u, t) + S[2 * i + 1];
  110. aux = A;
  111. A = B;
  112. B = C;
  113. C = D;
  114. D = aux;
  115. }
  116. A = A + S[2 * r + 2];
  117. C = C + S[2 * r + 3];
  118. data[0] = A;
  119. data[1] = B;
  120. data[2] = C;
  121. data[3] = D;
  122. for (int i = 0; i < tmp.Length; i++)
  123. {
  124. tmp[i] = unchecked((sbyte)(((int)((uint)data[i / 4] >> (i % 4) * 8)) & 0xff));
  125. }
  126. return tmp;
  127. }
  128. private static sbyte[] paddingKey(sbyte[] key)
  129. {
  130. int l = key.Length % 4;
  131. for (int i = 0; i < l; i++)
  132. {
  133. key[key.Length + i] = 0;
  134. }
  135. return key;
  136. }
  137. [WebMethod]
  138. public sbyte[] encrypt(sbyte[] data, sbyte[] key)
  139. {
  140. sbyte[] bloc = new sbyte[16];
  141. key = paddingKey(key);
  142. S = generateSubkeys(key);
  143. int lenght = 16 - data.Length % 16;
  144. sbyte[] padding = new sbyte[lenght];
  145. padding[0] = unchecked((sbyte)0x80);
  146. for (int i = 1; i < lenght; i++)
  147. {
  148. padding[i] = 0;
  149. }
  150. int count = 0;
  151. sbyte[] tmp = new sbyte[data.Length + lenght];
  152. //afiseazaMatrice(S);
  153. int j;
  154. for (j = 0; j < data.Length + lenght; j++)
  155. {
  156. if (j > 0 && j % 16 == 0)
  157. {
  158. bloc = encryptBloc(bloc);
  159. Array.Copy(bloc, 0, tmp, j - 16, bloc.Length);
  160. }
  161. if (j < data.Length)
  162. {
  163. bloc[j % 16] = data[j];
  164. }
  165. else
  166. {
  167. bloc[j % 16] = padding[count];
  168. count++;
  169. if (count > lenght - 1)
  170. {
  171. count = 1;
  172. }
  173. }
  174. }
  175. bloc = encryptBloc(bloc);
  176. Array.Copy(bloc, 0, tmp, j - 16, bloc.Length);
  177. return tmp;
  178. }
  179. public static sbyte[] decrypt(sbyte[] data, sbyte[] key)
  180. {
  181. sbyte[] tmp = new sbyte[data.Length];
  182. sbyte[] bloc = new sbyte[16];
  183. key = paddingKey(key);
  184. S = generateSubkeys(key);
  185. int i;
  186. for (i = 0; i < data.Length; i++)
  187. {
  188. if (i > 0 && i % 16 == 0)
  189. {
  190. bloc = decryptBloc(bloc);
  191. Array.Copy(bloc, 0, tmp, i - 16, bloc.Length);
  192. }
  193. if (i < data.Length)
  194. {
  195. bloc[i % 16] = data[i];
  196. }
  197. }
  198. bloc = decryptBloc(bloc);
  199. Array.Copy(bloc, 0, tmp, i - 16, bloc.Length);
  200. tmp = deletePadding(tmp);
  201. return tmp;
  202. }
  203. private static sbyte[] deletePadding(sbyte[] input)
  204. {
  205. int count = 0;
  206. int i = input.Length - 1;
  207. while (input[i] == 0)
  208. {
  209. count++;
  210. i--;
  211. }
  212. sbyte[] tmp = new sbyte[input.Length - count - 1];
  213. Array.Copy(input, 0, tmp, 0, tmp.Length);
  214. return tmp;
  215. }

comments powered by Disqus