cryptography - Feistel Cipher in c# -
i'm trying implement feistel cipher, have following code:
public static byte[] encode(byte[] bytes) { byte[] s = (byte[])bytes.clone(); int half = s.length / 2; if (s.length % 2 != 0) throw new exception("unbalanced code"); byte[] left = new byte[half]; byte[] right = new byte[half]; array.copy(s, left, half); array.copy(s, half, right, 0, half); (var k = 0; k < rounds; k++) { left = xor(left,f(right)); var temp = left; left = right; right = temp; } byte[] toreturn = new byte[s.length]; array.copy(left, 0, toreturn, half, half); array.copy(right, 0, toreturn, 0, half); return toreturn; }
in sample, rounds
21 , f function rearranges bytes in array of bytes.
unfortunately, while bytes should equal encode(encode(bytes)), it's not. , have no idea i'm doing wrong.
any advice?
edit: code f()
public static byte[] f(byte[] s) { byte[] toreturn = (byte[])s.clone(); byte temp = s[0]; (var k = 1; k < s.length; k++) { s[k-1] = s[k]; s[k - 1] = (byte)(s[k - 1] ^ 45); } s[s.length - 1] = temp; s[s.length - 1] = (byte)(s[s.length - 1] ^ 45); return toreturn; }
codesinchaos correct, problem variable being mutated.
Comments
Post a Comment