Tuesday, April 7, 2009

Simple JAVA 3DES application

Because of there are lots of request for this application..I surrender..hehe..here are the codes..i will not hold responsible if something happens to your assignment..hehe..this is for education only..there are some simple errors..so watch out..you can also donate some $$ in my paypal..hehe..

ATTENTION to all lecturer, plz check you students JAVA projects related to 3des cause the student might be using this code as their work...haha..






*************************

package xxx;
import bbb.TripleDES;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import javax.swing.SwingUtilities;
/**
*
* @author nawuza
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
TripleDES frame = new TripleDES();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(400,300);
frame.setTitle("Triple DES Algorithm :");
frame.setSize(400,250);
frame.setVisible(true);

}

}

***********************************************

package xxx;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.KeySpec;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import org.bouncycastle.util.encoders.Base64;

public class StringEncrypter
{

public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";

private KeySpec keySpec;
private SecretKeyFactory keyFactory;
private Cipher cipher;

private static final String UNICODE_FORMAT = "UTF8";

public StringEncrypter( String encryptionScheme, String encryptionKey )
throws EncryptionException
{

if(encryptionKey.trim().length()==1)
encryptionKey = encryptionKey + "@#$%^&*()?Ç~!Äú±?{ò†÷µ¥";
else if(encryptionKey.trim().length()==2)
encryptionKey = encryptionKey + "@#$%^&*()?Ç~!Äú±?{ò†÷µ";
else if(encryptionKey.trim().length()==3)
encryptionKey = encryptionKey + "@#$%^&*()?Ç~!Äú±?{ò†÷";
else if(encryptionKey.trim().length()==4)
encryptionKey = encryptionKey + "@#$%^&*()?Ç~!Äú±?{ò†";
else if(encryptionKey.trim().length()==5)
encryptionKey = encryptionKey + "@#$%^&*()?Ç~!Äú±?{ò";
else if(encryptionKey.trim().length()==6)
encryptionKey = encryptionKey + "@#$%^&*()?Ç~!Äú±?{";
else if(encryptionKey.trim().length()==7)
encryptionKey = encryptionKey + "@#$%^&*()?Ç~!Äú±?";
else if(encryptionKey.trim().length()==8)
encryptionKey = encryptionKey + "@#$%^&*()?Ç~!Äú±";
else if(encryptionKey.trim().length()==9)
encryptionKey = encryptionKey + "@#$%^&*()?Ç~!Äú";
else if(encryptionKey.trim().length()==10)
encryptionKey = encryptionKey + "@#$%^&*()?Ç~!Ä";
else if(encryptionKey.trim().length()==11)
encryptionKey = encryptionKey + "@#$%^&*()?Ç~!";
else if(encryptionKey.trim().length()==12)
encryptionKey = encryptionKey + "@#$%^&*()?Ç~";
else if(encryptionKey.trim().length()==13)
encryptionKey = encryptionKey + "@#$%^&*()?Ç";
else if(encryptionKey.trim().length()==14)
encryptionKey = encryptionKey + "@#$%^&*()?";
else if(encryptionKey.trim().length()==15)
encryptionKey = encryptionKey + "@#$%^&*()";
else if(encryptionKey.trim().length()==16)
encryptionKey = encryptionKey + "@#$%^&*(";
else if(encryptionKey.trim().length()==17)
encryptionKey = encryptionKey + "@#$%^&*";
else if(encryptionKey.trim().length()==18)
encryptionKey = encryptionKey + "@#$%^&";
else if(encryptionKey.trim().length()==19)
encryptionKey = encryptionKey + "@#$%^";
else if(encryptionKey.trim().length()==20)
encryptionKey = encryptionKey + "@#$%";
else if(encryptionKey.trim().length()==21)
encryptionKey = encryptionKey + "@#$";
else if(encryptionKey.trim().length()==22)
encryptionKey = encryptionKey + "@#";
else if(encryptionKey.trim().length()==23)
encryptionKey = encryptionKey + "@";
else
System.out.println("Okay : ");

try
{
byte[] keyAsBytes = encryptionKey.getBytes( UNICODE_FORMAT );

if ( encryptionScheme.equals( DESEDE_ENCRYPTION_SCHEME) )
{
keySpec = new DESedeKeySpec( keyAsBytes );
}

else
{
throw new IllegalArgumentException( "Encryption scheme not supported: "
+ encryptionScheme );
}

keyFactory = SecretKeyFactory.getInstance( encryptionScheme );
cipher = Cipher.getInstance( encryptionScheme );

}
catch (InvalidKeyException e)
{
throw new EncryptionException( e );
}
catch (UnsupportedEncodingException e)
{
throw new EncryptionException( e );
}
catch (NoSuchAlgorithmException e)
{
throw new EncryptionException( e );
}
catch (NoSuchPaddingException e)
{
throw new EncryptionException( e );
}

}

public String encrypt( String unencryptedString ) throws EncryptionException
{
if ( unencryptedString == null || unencryptedString.trim().length() == 0 )
throw new IllegalArgumentException(
"unencrypted string was null or empty" );

try
{
SecretKey key = keyFactory.generateSecret( keySpec );
cipher.init( Cipher.ENCRYPT_MODE, key );
byte[] cleartext = unencryptedString.getBytes( UNICODE_FORMAT );
byte[] ciphertext = cipher.doFinal( cleartext );

BASE64Encoder base64encoder = new BASE64Encoder();
return base64encoder.encode( ciphertext );
}
catch (Exception e)
{
throw new EncryptionException( e );
}
}

public String decrypt( String encryptedString ) throws EncryptionException
{
if ( encryptedString == null || encryptedString.trim().length() <= 0 )
throw new IllegalArgumentException( "encrypted string was null or empty" );

try
{
SecretKey key = keyFactory.generateSecret( keySpec );
cipher.init( Cipher.DECRYPT_MODE, key );
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] cleartext = base64decoder.decodeBuffer( encryptedString );
byte[] ciphertext = cipher.doFinal( cleartext );

return bytes2String( ciphertext );
}
catch (Exception e)
{
return new String("Error...");
}
}

private static String bytes2String( byte[] bytes )
{
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < bytes.length; i++)
{
stringBuffer.append( (char) bytes[i] );
}
return stringBuffer.toString();
}

public static class EncryptionException extends Exception
{
public EncryptionException( Throwable t )
{
super( t );
}
}

}

***************************

package xxx;

import ccc.StringEncrypter;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import javax.swing.SwingUtilities;
import xxx.StringEncrypter.EncryptionException;

public class TripleDES extends JFrame implements ActionListener {

private JButton enc = new JButton("Encrypt");
private JButton dec = new JButton("Decrypt");

private JTextField input = new JTextField();
private JTextField output = new JTextField();
private JPasswordField pass = new JPasswordField();

private String password ;
private String inputText;
private String outputText;
private String ct;
private String pt;

public TripleDES(){

JPanel satu = new JPanel();
satu.setLayout(new BorderLayout());
satu.setBorder(new TitledBorder("Input :"));
satu.add(input,BorderLayout.CENTER);

JPanel dua = new JPanel();
dua.setLayout(new BorderLayout());
dua.setBorder(new TitledBorder("Password :"));
dua.add(pass,BorderLayout.CENTER);
pass.setEchoChar('|');

JPanel satudua = new JPanel();
satudua.setLayout(new GridLayout(2,1));
satudua.add(satu);
satudua.add(dua);

JPanel tiga = new JPanel();
tiga.setLayout(new BorderLayout());
tiga.setBorder(new TitledBorder("Choose :"));
tiga.add(enc, BorderLayout.WEST);
tiga.add(dec,BorderLayout.EAST);

JPanel empat = new JPanel();
empat.setLayout(new BorderLayout());
empat.setBorder(new TitledBorder("Output :"));
empat.add(output, BorderLayout.CENTER);

JPanel tigaempat = new JPanel();
tigaempat.setLayout(new GridLayout(2,1));
tigaempat.add(tiga);
tigaempat.add(empat);

this.getContentPane().add(satudua,BorderLayout.CENTER);
this.getContentPane().add(tigaempat,BorderLayout.SOUTH);

enc.addActionListener(this);
dec.addActionListener(this);

}

public void actionPerformed(ActionEvent e){
String z;
//Handle encrypt button action.
if (e.getSource() == enc) {

inputText=input.getText();
char [] pwd = pass.getPassword();
String password = new String(pwd);

if(inputText==null || inputText.trim().length()==0){
JOptionPane.showMessageDialog(null, "Input must be filled","Warning!!",JOptionPane.ERROR_MESSAGE);

}

if(password==null || password.trim().length()==0){
JOptionPane.showMessageDialog(null, "Password must be filled","Warning!!",JOptionPane.ERROR_MESSAGE);

}

if(password.trim().length()!=0 && inputText.trim().length()!=0 ){
z = Enc(inputText,password);
output.setText(z);
output.setEditable(false);
}

}
//Handle decrypt button action.
else if (e.getSource() == dec) {
inputText=input.getText();
inputText=input.getText();
char [] pwd = pass.getPassword();
String password = new String(pwd);

if(inputText==null || inputText.trim().length()==0){
JOptionPane.showMessageDialog(null, "Input must be filled","Warning!!",JOptionPane.ERROR_MESSAGE);

}

if(password==null || password.trim().length()==0){
JOptionPane.showMessageDialog(null, "Password must be filled","Warning!!",JOptionPane.ERROR_MESSAGE);

}

if(password.trim().length()!=0 && inputText.trim().length()!=0 ){
z = Dec(inputText,password);
output.setText(z);
output.setEditable(false);
}

}

else
JOptionPane.showMessageDialog(null, "-message","-title",JOptionPane.ERROR_MESSAGE);

}

public String Enc(String a,String b){
try {

StringEncrypter encode = new StringEncrypter("DESede",b);
ct = encode.encrypt(a);
}
catch (EncryptionException ex) {
ex.printStackTrace();
}

return ct;
}

public String Dec(String x,String y){
try {

StringEncrypter decode = new StringEncrypter("DESede",y);
pt = decode.decrypt(x);
}
catch (EncryptionException ex) {
ex.printStackTrace();
}

return pt;
}

public static void main (String[] args){
TripleDES frame = new TripleDES();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Triple DES Algorithm :");
frame.setSize(400,250);
frame.setVisible(true);
}

}

2 comments: