Beranda > Computer > Encrypt dan Decrypt dengan Metode Pass Phrase

Encrypt dan Decrypt dengan Metode Pass Phrase

// CIPHER / GENERATORS
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.KeyGenerator;
 
// KEY SPECIFICATIONS
import java.security.spec.KeySpec;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEParameterSpec;
 
// EXCEPTIONS
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
public class CEncrypt2 {
   
    Cipher ecipher;
    Cipher dcipher;
    /** Creates a new instance of CEncrypt2 */
    CEncrypt2(String passPhrase) {
        // 8-bytes Salt
        byte[] salt = {
            (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
            (byte)0x56, (byte)0x34, (byte)0xE3, (byte)0x03
        };
 
        // Iteration count
        int iterationCount = 19;
 
        try {
 
            KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
            SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndTripleDES").generateSecret(keySpec);
 
            ecipher = Cipher.getInstance(key.getAlgorithm());
            dcipher = Cipher.getInstance(key.getAlgorithm());
 
            // Prepare the parameters to the cipthers
            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
 
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
 
        } catch (InvalidAlgorithmParameterException e) {
            System.out.println("EXCEPTION: InvalidAlgorithmParameterException");
        } catch (InvalidKeySpecException e) {
            System.out.println("EXCEPTION: InvalidKeySpecException");
        } catch (NoSuchPaddingException e) {
            System.out.println("EXCEPTION: NoSuchPaddingException");
        } catch (NoSuchAlgorithmException e) {
            System.out.println("EXCEPTION: NoSuchAlgorithmException");
        } catch (InvalidKeyException e) {
            System.out.println("EXCEPTION: InvalidKeyException");
        }
    }
   
    public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");
 
            // Encrypt
            byte[] enc = ecipher.doFinal(utf8);
 
            // Encode bytes to base64 to get a string
            return new sun.misc.BASE64Encoder().encode(enc);
 
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
   
    public String decrypt(String str) {
 
        try {
 
            // Decode base64 to get bytes
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
           
            //dec = reverse(dec); //this used
 
            // Decrypt
            byte[] utf8 = dcipher.doFinal(dec);
 
            // Decode using utf-8
            return new String(utf8, "UTF8");
 
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
   
    public byte[] reverse (byte[] data)
        {
            int size = data.length;
            byte[] reversedData = new byte[size];
            //try
                       // {
            for (int i = 0; i < size; i++)
            {
                reversedData[i] = data[size – i – 1];
            }
            return reversedData;
                       // } catch (BadPaddingException e) {
                       //     e.printStackTrace();
                       // }
        }
 
 
    /**
     * The following method is used for testing the String Encrypter class.
     * This method is responsible for encrypting and decrypting a sample
     * String using using a Pass Phrase.
     */
    public static void testUsingPassPhrase() {
 
        System.out.println();
        System.out.println("+—————————————-+");
        System.out.println("|  — Test Using Pass Phrase Method —   |");
        System.out.println("+—————————————-+");
        System.out.println();
 
        String secretString = "veuJ6SLbijyF29b";
        String passPhrase   = "yahoo";
 
        // Create encrypter/decrypter class
        CEncrypt2 desEncrypter = new CEncrypt2(passPhrase);
 
        // Encrypt the string
        String desEncrypted       = desEncrypter.encrypt(secretString);
        //String desEncrypted       = "X29NAt2lELGUa0FX10Z+KQh3IAl6yoS7R8UE6FsAkWPIuOddobSWrpmWvpv1CxH/iUUP4sC9D58=";
       
        // Decrypt the string
        String desDecrypted       = desEncrypter.decrypt(desEncrypted);
 
        // Print out values
        System.out.println("PBEWithMD5AndTripleDES Encryption algorithm");
        System.out.println("Original String  : " + secretString);
        System.out.println("Encrypted String : " + desEncrypted);
        System.out.println("Decrypted String : " + desDecrypted);
        System.out.println();
 
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        testUsingPassPhrase();
    }
   
}

Iklan
Kategori:Computer
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: