Monday, September 8, 2008

Encrypting Data Using DES in Java

In this tutorial i will show you how to encrypt and decrypt text using DES in java, encrypt and decrypt is very simple, lets begin.
1. Create a new class, i called DES:
/*
* 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();
}
}
2.Defining a DES key, atleast 8 character:
byte[] key = "MyPassword".getBytes(); // DES key atleast 8 character
3. 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:
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)
Conclusion:
To encrypt data using DES is simple, create cipher DES instance, init for encrypt or decrypt mode and finally encrypt or decrypt using doFinal method
Complete 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();
}
}
Diposting oleh M. Jumari di Monday, September 08, 2008
Label: English, Java, Tips and Tricks, Tutorial

4 komentar:

vemo80 said...

sipp tutorialnya walau ra mudeng java

September 10, 2008 9:24 PM
maarimuthu udayakumar, said...

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....

December 30, 2008 2:27 PM
M. Jumari said...

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.

December 30, 2008 3:00 PM
maarimuthu udayakumar, said...

hi ,
thanks for your answer..it is working perfectly now..thanks a lot...keep in touch..take care....

December 30, 2008 4:38 PM

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

About Me

My Photo
My name is M. Jumari @cyberworld i'am also known as Kazao aka Kazao™ aka Mr. Kazao. I'am a simply man enjoying life in a beautifull country.
     All source code you get from here is absolutely free. You may view my complete profile or view my website.

Messenger

m.jumari

Archive

  • April 2009 (6)
  • March 2009 (1)
  • January 2009 (3)
  • December 2008 (4)
  • October 2008 (1)
  • September 2008 (5)
  • August 2008 (12)
  • July 2008 (20)
  • May 2008 (2)

Labels

  • Bahasa Indonesia (29)
  • Berita (1)
  • Cuap-Cuap (3)
  • Curhat (10)
  • English (18)
  • Filosofi (1)
  • Foto (2)
  • Gambar (5)
  • Java (17)
  • Joke (2)
  • JPanel (2)
  • JTable (2)
  • JTextArea (1)
  • JTree (3)
  • Printing (2)
  • Reflection (1)
  • Swing (10)
  • Tips and Tricks (21)
  • Trojan (1)
  • Tutorial (17)
  • Video (1)
  • Virus (1)

Friends

  • Mama Vino
    KBB#18 – French Macarons
    10 hours ago
  • Herman Saksono
    Salt
    1 day ago
  • Tikabanget™
    Kelas Kemewahan Internet ituh..
    5 days ago
  • Obie XP
    819936000 + n detik (n=n+1)
    3 months ago
  • Oktarianti
    Banda Aceh
    4 months ago
  • Herwin Saputra
    YellowNarita and Riyantie
    5 months ago
  • Indra Wahyudi
    Pesan Itu Mengisyaratkan "BAHAYA"
    11 months ago
  • A r l e y N o v a ™
    why marry?
    1 year ago
  • Dony Iswantoro
    Tentang seorang "Pangeran"
    1 year ago
  • Akhmad Fathonih
    Finally, a proper kde 4.2
    1 year ago
  • The Vemo™
    Terima Kasih
    1 year ago
  • Harvey
    Kekuatan Maaf
    2 years ago
  • Teguh Susanto
    Bermain Seks Gunakan Otak....
    2 years ago
Show 5 Show All

Subscribe

Posts
Atom
Posts
Comments
Atom
Comments

Ketawa.com

Loading...

World Clocks

Followers

Widgets

Google Groups
Berlangganan ke informatika98
Email:
Kunjungi grup ini
Google Groups
informatics-uii
Kunjungi grup ini
Kazao™

YouTube

Loading...
Copyright © M. Jumari 2008 Powered by www.blogger.com
Visit my web @ mr.kazao.net and you can call me @ +6281904091661 or +622743251763