Source Code : Encryption and Decryption using Symmetric Keys

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Encryption and Decryption using Symmetric Keys

      
 
import java.security.InvalidKeyException;
import java.security.Key;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;

public class Main {
  static String algorithm = "DESede";
  static Key key = KeyGenerator.getInstance(algorithm).generateKey();
  static Cipher cipher = Cipher.getInstance(algorithm);

  public static void main(String[] args) throws Exception {
    byte[] encryptionBytes = encrypt("input");
    System.out.println("Recovered: " + decrypt(encryptionBytes));
  }

  private static byte[] encrypt(String input) throws InvalidKeyException, BadPaddingException,
      IllegalBlockSizeException {
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] inputBytes = input.getBytes();
    return cipher.doFinal(inputBytes);
  }

  private static String decrypt(byte[] encryptionBytes) throws InvalidKeyException,
      BadPaddingException, IllegalBlockSizeException {
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
    String recovered = new String(recoveredBytes);
    return recovered;
  }
}

   
    
    
    
    
    
  

Thank with us