1. Create a new class, i called DES:
/*2.Defining a DES key, atleast 8 character:
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.kazao.tips.tip027;
/**
*
* @author Mr. Kazao
*/
public class DES {
public DES() {
}
public static void main(String... args) {
new DES();
}
}
byte[] key = "MyPassword".getBytes(); // DES key atleast 8 character3. Defining plaintext:
byte[] plaintext = "Who are you?".getBytes();4. Creating SecreeyKey:
SecretKey sKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key));5.Instanting cipher using DES algorithm:Cipher cipher = Cipher.getInstance("DES");6. Initialize cipher for encryption:cipher.init(Cipher.ENCRYPT_MODE, sKey);7. Encrypt plaintext into ciphertext:
byte[] ciphertext = cipher.doFinal(plaintext);8. Initialize cipher for decryption:
cipher.init(Cipher.DECRYPT_MODE, sKey);9. Decrypt ciphertext into plaindecrypt:
byte[] plaindecrypt = cipher.doFinal(ciphertext);10. Printing to STDOUT:
System.out.println("Plain Text : "+convert(plaintext));
System.out.println("Cipher Text : "+convert(ciphertext));
System.out.println("Plain Decrypted: "+convert(plaindecrypt));11. Here the result:run-single:Conclusion:
Plain Text : 57 68 6f 20 61 72 65 20 79 6f 75 3f
Cipher Text : 8d fa 05 d2 3f 4a bc cc 09 ff 46 e3 27 c3 7b 84
Plain Decrypted: 57 68 6f 20 61 72 65 20 79 6f 75 3f
BUILD SUCCESSFUL (total time: 10 seconds)
To encrypt data using DES is simple, create cipher DES instance, init for encrypt or decrypt mode and finally encrypt or decrypt using doFinal methodComplete Source:
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.kazao.tips.tip027;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
*
* @author Mr. Kazao
*/
public class DES {
public DES() {
byte[] key = "MyPassword".getBytes(); // DES key atleast 8 character
try {
// defining plaintext
byte[] plaintext = "Who are you?".getBytes();
// creating SecretKey
SecretKey sKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key));
// instanting cipher using DES algorithm
Cipher cipher = Cipher.getInstance("DES");
// initialize cipher for encryption
cipher.init(Cipher.ENCRYPT_MODE, sKey);
// encryption
byte[] ciphertext = cipher.doFinal(plaintext);
// initialize cipher for decryption
cipher.init(Cipher.DECRYPT_MODE, sKey);
// decryption
byte[] plaindecrypt = cipher.doFinal(ciphertext);
// printing to STDOUT
System.out.println("Plain Text : "+convert(plaintext));
System.out.println("Cipher Text : "+convert(ciphertext));
System.out.println("Plain Decrypted: "+convert(plaindecrypt));
} catch (Exception e) {
}
}
private int byte2int(byte data) {
return data < 0 ? 256 + data : data;
}
private String convert(byte[] data) {
StringBuffer buffer = new StringBuffer();
for (byte b : data) {
String s = Integer.toHexString(byte2int(b));
if (s.length() < 2) {
buffer.append("0" + s);
} else {
buffer.append(s);
}
buffer.append(" ");
}
return buffer.toString();
}
public static void main(String... args) {
new DES();
}
}

4 komentar:
sipp tutorialnya walau ra mudeng java
hi ,
I have seen your code for encrypt and decrypt ,, it was facinating..but after implement your code .i got exception like " javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher"
pls help me,
what shoule i do?
thanks in advance.
P.Marimuthu....
Whatever length plaintext, length byte[] ciphertext return from method "cipher.doFinal(plaintext);" is always in multiple 8, a mistake maybe in data type declaring for chipertext, try to use array of byte (byte[]) because return and parameter using array of byte too.
hi ,
thanks for your answer..it is working perfectly now..thanks a lot...keep in touch..take care....
Post a Comment