casper. Powered by Blogger.

Blog ini bertujuan untuk share ilmu dan pengetahuan. Semua source/aplikasi di blog ini bebas disebar dan mencamtumkan sumber "fandy-alfa.blogspot.com".

RSS

Java - Encrypt Decrypt, DES

/*
 * Author : Fandy Adam
 * Email  : casperadam91@gmail.com
 * netbeans 7.3.x JDK 1.7
 */
package encryptde;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import sun.misc.BASE64Encoder;

/**
 *
 * @author casper
 */
public class DES {
  
    public static void main(String[] args) {
      
        String strDataToEncrypt = new String();
        String strCipherText = new String();
        String strDecryptedText = new String();
      
        try{
        /**
         *  Step 1. Generate a DES key using KeyGenerator
         *
         */
        KeyGenerator keyGen = KeyGenerator.getInstance("DES");
        SecretKey secretKey = keyGen.generateKey();
      
        /**
         *  Step2. Create a Cipher by specifying the following parameters
         *             a. Algorithm name - here it is DES
         *             b. Mode - here it is CBC
         *             c. Padding - PKCS5Padding
         */
      
        Cipher desCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
      
        /**
         *  Step 3. Initialize the Cipher for Encryption
         */
      
        desCipher.init(Cipher.ENCRYPT_MODE,secretKey);
      
        /**
         *  Step 4. Encrypt the Data
         *          1. Declare / Initialize the Data. Here the data is of type String
         *          2. Convert the Input Text to Bytes
         *          3. Encrypt the bytes using doFinal method
         */
        strDataToEncrypt = "Hello World of Encryption using DES ";
        byte[] byteDataToEncrypt = strDataToEncrypt.getBytes();
        byte[] byteCipherText = desCipher.doFinal(byteDataToEncrypt);
        strCipherText = new BASE64Encoder().encode(byteCipherText);
        System.out.println("Cipher Text generated using DES with CBC mode and PKCS5 Padding is " +strCipherText);
      
        /**
         *  Step 5. Decrypt the Data
         *          1. Initialize the Cipher for Decryption
         *          2. Decrypt the cipher bytes using doFinal method
         */
        desCipher.init(Cipher.DECRYPT_MODE,secretKey,desCipher.getParameters());
         //desCipher.init(Cipher.DECRYPT_MODE,secretKey);
        byte[] byteDecryptedText = desCipher.doFinal(byteCipherText);
        strDecryptedText = new String(byteDecryptedText);
        System.out.println(" Decrypted Text message is " +strDecryptedText);
        }
      
        catch (NoSuchAlgorithmException noSuchAlgo)
        {
            System.out.println(" No Such Algorithm exists " + noSuchAlgo);
        }
      
            catch (NoSuchPaddingException noSuchPad)
            {
                System.out.println(" No Such Padding exists " + noSuchPad);
            }
      
                catch (InvalidKeyException invalidKey)
                {
                    System.out.println(" Invalid Key " + invalidKey);
                }
              
                catch (BadPaddingException badPadding)
                {
                    System.out.println(" Bad Padding " + badPadding);
                }
              
                catch (IllegalBlockSizeException illegalBlockSize)
                {
                    System.out.println(" Illegal Block Size " + illegalBlockSize);
                }
              
                catch (InvalidAlgorithmParameterException invalidParam)
                {
                    System.out.println(" Invalid Parameter " + invalidParam);
                }
    }
}
sumber : https://www.owasp.org/index.php/Using_the_Java_Cryptographic_Extensions
  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comments:

Post a Comment