public static byte[] encrypt(byte[] input, Object key) { int rest = 0; if (input.Length % 16 != 0) { rest = 16 - (input.Length % 16); } byte[] input_copy = new byte[input.Length + rest]; byte[] output = new byte[input_copy.Length]; Array.Copy(input, 0, input_copy, 0, input.Length); for (int i = 0; i < rest; i++) { input_copy[input.Length + i] = 32; } int count = input_copy.Length / 16; for (int idx = 0; idx < count; idx++) { Array.Copy(Twofish_Algorithm.blockEncrypt(input_copy, (uint)idx * 16, key), 0, output, idx * 16, 16); } return(output); }
private static object ConvertedKey(string key) { if (!convertedKeys.ContainsKey(key)) { convertedKeys.TryAdd(key, Twofish_Algorithm.makeKey(convertKey(key))); } return(convertedKeys[key]); }
private static String toString(byte[] ba, int offset, int length) { char[] buf = new char[length * 2]; for (int i = offset, j = 0, k; i < offset + length;) { k = ba[i++]; buf[j++] = HEX_DIGITS[(Twofish_Algorithm.ror((uint)k, 32, 4) /* >>> 4*/) & 0x0F]; buf[j++] = HEX_DIGITS[k & 0x0F]; } return(new String(buf)); }
public static byte[] decrypt(byte[] input, Object key) { byte[] output = new byte[input.Length]; int count = input.Length / 16; for (int idx = 0; idx < count; idx++) { Array.Copy(Twofish_Algorithm.blockDecrypt(input, (uint)idx * 16, key), 0, output, idx * 16, 16); } return(output); }