CryptographyBlockCipherFeistel.blogspot.com
http://www.leonardo.labolida.com
There is an easy way to explain to programmers how to understand block cipher...
/**
Feistel structure - Simple example
Leonardo Labolida - Cryptography
http://www.leonardo.labolida.com
See also:
https://en.wikipedia.org/wiki/Feistel_cipher
**/
public class BlockCipherCryptography {
public static void main(String[] args) {
byte s[] = new byte[8]; // Plain Text - The message. 8bytes * 8bits = 64bits
byte k[] = new byte[4]; // The secret key. 4bytes * 8bits = 32bits
//------------->01234567
s = new String("LEONARDO").getBytes();
System.out.println("The message "+ new String(s)+" has " + s.length + " bytes = " + (s.length *8) + " bits" );
//------------->0123
k = new String("BzX3").getBytes();
System.out.println("The KEY "+ new String(k)+" has " + k.length + " bytes = " + (k.length *8) + " bits" );
debug(k);
debug(s);
s=encode(s,k);
debug(s);
s=encode(s,k);
debug(s);
s=encode(s,k);
debug(s);
}
// Symmetric encode/decode
private static byte[] encode( byte s[] , byte k[] ) {
for (int i=0; i<4; i++){ // 4 bytes = 32 bits
byte x1 = (byte) (s[i]^k[i]); // XOR LEFT+K
//x1 = (byte)(x1^1); // Function f (OPTIONAL)
byte x2 = (byte) (x1^s[i+4]) ; // XOR R+RIGHT
s[i] = x2; // L=xRight
s[i+4]=x1; // R=xLeft
}
return s;
}
// Show me
private static void debug( byte b[] ) {
System.out.println("---");
for (int i=0; i<b.length; i++){
System.out.print( " BYTE " + i + " is:Char:" + (char)b[i] + " is:ASCII:" + b[i] + " , bits: " );
for (int f=128; f>0; f=f/2){ // bit representation
if ( (b[i]&f)==f )
System.out.print("1");
else
System.out.print("0");
}
System.out.println();
}
}
}
The message LEONARDO has 8 bytes = 64 bits
The KEY BzX3 has 4 bytes = 32 bits
---
BYTE 0 is:Char:B is:ASCII:66 , bits: 01000010
BYTE 1 is:Char:z is:ASCII:122 , bits: 01111010
BYTE 2 is:Char:X is:ASCII:88 , bits: 01011000
BYTE 3 is:Char:3 is:ASCII:51 , bits: 00110011
---
BYTE 0 is:Char:L is:ASCII:76 , bits: 01001100
BYTE 1 is:Char:E is:ASCII:69 , bits: 01000101
BYTE 2 is:Char:O is:ASCII:79 , bits: 01001111
BYTE 3 is:Char:N is:ASCII:78 , bits: 01001110
BYTE 4 is:Char:A is:ASCII:65 , bits: 01000001
BYTE 5 is:Char:R is:ASCII:82 , bits: 01010010
BYTE 6 is:Char:D is:ASCII:68 , bits: 01000100
BYTE 7 is:Char:O is:ASCII:79 , bits: 01001111
---
BYTE 0 is:Char:O is:ASCII:79 , bits: 01001111
BYTE 1 is:Char:m is:ASCII:109 , bits: 01101101
BYTE 2 is:Char:S is:ASCII:83 , bits: 01010011
BYTE 3 is:Char:2 is:ASCII:50 , bits: 00110010
BYTE 4 is:Char: is:ASCII:14 , bits: 00001110
BYTE 5 is:Char:? is:ASCII:63 , bits: 00111111
BYTE 6 is:Char: is:ASCII:23 , bits: 00010111
BYTE 7 is:Char:} is:ASCII:125 , bits: 01111101
---
BYTE 0 is:Char: is:ASCII:3 , bits: 00000011
BYTE 1 is:Char:( is:ASCII:40 , bits: 00101000
BYTE 2 is:Char: is:ASCII:28 , bits: 00011100
BYTE 3 is:Char:| is:ASCII:124 , bits: 01111100
BYTE 4 is:Char: is:ASCII:13 , bits: 00001101
BYTE 5 is:Char: is:ASCII:23 , bits: 00010111
BYTE 6 is:Char: is:ASCII:11 , bits: 00001011
BYTE 7 is:Char: is:ASCII:1 , bits: 00000001
---
BYTE 0 is:Char:L is:ASCII:76 , bits: 01001100
BYTE 1 is:Char:E is:ASCII:69 , bits: 01000101
BYTE 2 is:Char:O is:ASCII:79 , bits: 01001111
BYTE 3 is:Char:N is:ASCII:78 , bits: 01001110
BYTE 4 is:Char:A is:ASCII:65 , bits: 01000001
BYTE 5 is:Char:R is:ASCII:82 , bits: 01010010
BYTE 6 is:Char:D is:ASCII:68 , bits: 01000100
BYTE 7 is:Char:O is:ASCII:79 , bits: 01001111