Using JCA in Java- 5pgs due in 5hrs

profileguri_69
solution-l2-PBE.java

import java.security.Key; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.PBEParameterSpec; import javax.crypto.spec.SecretKeySpec; /** * Example of using Password-based encryption */ public class PBE { public static void main( String[] args) throws Exception { PBEKeySpec pbeKeySpec; PBEParameterSpec pbeParamSpec; SecretKeyFactory keyFac; // Salt byte[] salt = { (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c, (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99 }; // Iteration count int count = 2048; // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt, count); //Initialization of the password char[] password = "newpassword".toCharArray(); //Create parameter for key generation pbeKeySpec = new PBEKeySpec(password); // Create instance of SecretKeyFactory for password-based encryption // using DES and MD5 keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); // Generate a key Key pbeKey = keyFac.generateSecret(pbeKeySpec); // Create PBE Cipher Cipher pbeCipher_en = Cipher.getInstance("PBEWithMD5AndDES"); Cipher pbeCipher_de = Cipher.getInstance("PBEWithMD5AndDES"); // Initialize PBE Cipher with key and parameters pbeCipher_en.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); pbeCipher_de.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); //ENCRYPTION // Our plaintext String plaintext="The codes for the safe are...."; System.out.println("Plain text:"+ plaintext); byte[] cleartext = plaintext.getBytes(); // Encrypt the plaintext byte[] ciphertext = pbeCipher_en.doFinal(cleartext); System.out.println("cipher : " + Utils.toHex(ciphertext)); //DECRYPTION byte[] decrypted_text=pbeCipher_de.doFinal(ciphertext); String decryptedTextString = new String (decrypted_text); System.out.println("Decrypted text: "+decryptedTextString); } }