Advertisement
tnngo2

APJ 2 - Simple Cryptography

Mar 23rd, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. import java.security.InvalidKeyException;
  2. import java.security.NoSuchAlgorithmException;
  3. import javax.crypto.*;
  4.  
  5. public class Encryption {
  6.     private static SecretKey sKey;
  7.     private Cipher objCipher;
  8.    
  9.     public Encryption () throws NoSuchAlgorithmException, NoSuchPaddingException {
  10.         // Generate the secret key
  11.         KeyGenerator generator = KeyGenerator.getInstance("DES");
  12.         sKey = generator.generateKey();
  13.        
  14.         // Initialize the cipher instance to use DES algorithm, ECB mode,
  15.         // and PKCS#5 padding scheme
  16.         objCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
  17.         //System.out.print("Algorithm => " + objCipher.getProvider());
  18.     }
  19.    
  20.     public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException{
  21.         String txt = "Here is the text";
  22.         System.out.println("Plain text => " + txt);
  23.         Encryption encrypt = new Encryption();
  24.        
  25.         //String enTxt = encrypt.encrypt(txt);
  26.         byte[] enTxt = encrypt.encrypt(txt);
  27.         String out_1 = new String(enTxt);
  28.         System.out.println("Encrypted text => " + out_1);
  29.        
  30.         String deTxt = encrypt.decrypt(enTxt);
  31.         System.out.println("Decrypted text => " + deTxt);
  32.     }
  33.    
  34.     public byte[] encrypt (String txt) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
  35.        
  36.         // Initialize the cipher with secret key to decrypt data
  37.         objCipher.init(Cipher.ENCRYPT_MODE,sKey);
  38.        
  39.         // Read the data into byte array
  40.         byte[] text = txt.getBytes();
  41.  
  42.         // Store the encrypted data in a byte array
  43.         byte[] encryptedData = objCipher.doFinal(text);
  44.         return encryptedData;
  45.         // Display the encrypted text in the textarea
  46.         //String encryptedText = new String(encryptedData);
  47.         //return encryptedText;
  48.     }
  49.    
  50.    
  51.     public String decrypt (byte[] txt) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
  52.         // Initialize the cipher with secret key to decrypt data
  53.         objCipher.init(Cipher.DECRYPT_MODE,sKey);
  54.  
  55.         // Read the encrypted data into byte array
  56.         byte[] decryptedText = txt;
  57.  
  58.         // Store the decrypted data into byte array
  59.         byte[] plainData = objCipher.doFinal(decryptedText);
  60.  
  61.         // Display the decrypted data in the textarea
  62.         String plainText = new String(plainData);
  63.         return plainText;
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement