Cryptography Tutorials - Tutorial Addendum - DES Algorithm - Java Accomplishing
| |
(Continued from antecedent part...)
clandestine changeless byte selectBits(byte in, int pos, int len) {
int numOfBytes = (len-1)/8 + 1;
byte out = new byte;
for (int i=0; i<len; i++) {
int val = getBit(in,pos+i);
setBit(out,i,val);
}
acknowledgment out;
}
clandestine changeless byte selectBits(byte in, int map) {
int numOfBytes = (map.length-1)/8 + 1;
byte out = new byte;
for (int i=0; i<map.length; i++) {
int val = getBit(in,map-1);
setBit(out,i,val);
}
acknowledgment out;
}
clandestine changeless int getBit(byte data, int pos) {
int posByte = pos/8;
int posBit = pos%8;
byte valByte = data;
int valInt = valByte>>(8-(posBit+1)) & 0x0001;
acknowledgment valInt;
}
clandestine changeless abandoned setBit(byte data, int pos, int val) {
int posByte = pos/8;
int posBit = pos%8;
byte oldByte = data;
oldByte = (byte) (((0xFF7F>>posBit) & oldByte) & 0x00FF);
byte newByte = (byte) ((val<<(8-(posBit+1))) | oldByte);
data = newByte;
}
clandestine changeless byte readBytes(String in) throws Barring {
FileInputStream fis = new FileInputStream(in);
int numOfBytes = fis.available();
byte absorber = new byte;
fis.read(buffer);
fis.close();
acknowledgment buffer;
}
clandestine changeless abandoned writeBytes(byte data, Cord out)
throws Barring {
FileOutputStream fos = new FileOutputStream(out);
fos.write(data);
fos.close();
}
clandestine changeless abandoned printBytes(byte data, Cord name) {
System.out.println("");
System.out.println(name+":");
for (int i=0; i<data.length; i++) {
System.out.print(byteToBits(data)+" ");
}
System.out.println();
}
clandestine changeless Cord byteToBits(byte b) {
StringBuffer buf = new StringBuffer();
for (int i=0; i<8; i++)
buf.append((int)(b>>(8-(i+1)) & 0x0001));
acknowledgment buf.toString();
}
}
If you run this program with JDK 1.4.1 with analysis key and bright argument block
mentioned in the antecedent chapter, you will get:
java -cp . CipherDES encrypt TestKey.des TestMsg.clr TestMsg.cph
Key block:
00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001
Input block:
00000001 00100011 01000101 01100111 10001001 10101011 11001101 11101111
Output block:
10000101 11101000 00010011 01010100 00001111 00001010 10110100 00000101
java -cp . CipherDES break TestKey.des TestMsg.cph TestMsg.bck
Key block:
00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001
Input block:
10000101 11101000 00010011 01010100 00001111 00001010 10110100 00000101
Output block:
00000001 00100011 01000101 01100111 10001001 10101011 11001101 11101111
As you can see from the output, the program works perfectly.
Exercise: Advance this program to handle assorted ascribe blocks.
|
private, static, block, numofbytes, posbyte, posbit, return, testmsg, system, string, oldbyte, getbit, program, buffer, setbit, length, println, , private static, system out, byte data, static void, int val, data posbyte, byte numofbytes, static byte, int pos, int numofbytes, new byte, private static void, new byte numofbytes, private static byte, 01000101 01100111 10001001, 10111100 11011111 11110001input, 11011111 11110001input block, 00100011 01000101 01100111, 01100111 10001001 10101011, 00000001 00100011 01000101, block 00000001 00100011, block 10000101 11101000, 01010100 00001111 00001010, 00001111 00001010 10110100, 00010011 01010100 00001111, 11101000 00010011 01010100, 10011011 10111100 11011111, 10000101 11101000 00010011, 10001001 10101011 11001101, 00110100 01010111 01111001, byte selectbits byte, int posbit pos%8, static byte selectbits, algorithm java implementation, tutorials tutorial notes, des algorithm java, posbit pos%8 byte, & 0x0001 return, cryptography tutorials tutorial, 01010111 01111001 10011011, 00010011 00110100 01010111, block 00010011 00110100, oldbyte data posbyte, byte data string, 01111001 10011011 10111100, |
Also see ...
This affiliate describes:A Java program to allegorize the DES key agenda Algorithm.A Java program to allegorize the DES blank algorithm.
i(Continued from antecedent part...)/i clandestine changeless abandoned printBytes(byte data, Cord name) { System.out.println(""); System.out.println(name+":"); for (int i=0; i<data.length; i+
i(Continued from antecedent part...)/iDESCipherTest.java DES Blank Algorithm IllustrationAs an analogy to the DES blank algorithm declared in theprevious chapter, I
i(Continued from antecedent part...)/i clandestine changeless byte substitution6x4(byte in) { in = splitBytes(in,6); // Agreeable byte into 6 bit blocks// printBytes(in,"R: Afterwards splitting");p
i(Continued from antecedent part...)/iIn this program, the ascribe bright argument block and the subkeys are harder coded with the ethics acclimated by J. Orlin Grabbe in "The DES Algorithm Illustrated". T
i(Continued from antecedent part...)/i R: Afterwards S boxes:00010001 00001100 01010111 01110111R: Afterwards P permutation:00100010 00110110 01111100 01101010Right bisected a
This tutorial helps you to accept What is MD5?MD5 Algorithm OverviewMD5 Accomplishing in JavaMD5 Accomplishing in PHPMD
i(Continued from antecedent part...)/iStep 3. Initializing MD Buffer. MD5 algorithm requires a 128 bit absorber with a specific initial value. The rules of initializing absorber are:T
i(Continued from antecedent part...)/iStep 5. Output. The capacity in absorber words A, B, C, D are alternate in arrangement with low order byte first.MD5 Accom
i(Continued from antecedent part...)/iIf you run this sample program with JDK 1.5, you should get the afterward output: Message abstract item info: Algorithm = MD5 Provider =