1import java.security.spec.KeySpec;
2import javax.crypto.Cipher;
3import javax.crypto.SecretKey;
4import javax.crypto.SecretKeyFactory;
5import javax.crypto.spec.DESedeKeySpec;
6import org.apache.commons.codec.binary.Base64;
7
8public class TrippleDes {
9
10 private static final String UNICODE_FORMAT = "UTF8";
11 public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
12 private KeySpec ks;
13 private SecretKeyFactory skf;
14 private Cipher cipher;
15 byte[] arrayBytes;
16 private String myEncryptionKey;
17 private String myEncryptionScheme;
18 SecretKey key;
19
20 public TrippleDes() throws Exception {
21 myEncryptionKey = "ThisIsSpartaThisIsSparta";
22 myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
23 arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
24 ks = new DESedeKeySpec(arrayBytes);
25 skf = SecretKeyFactory.getInstance(myEncryptionScheme);
26 cipher = Cipher.getInstance(myEncryptionScheme);
27 key = skf.generateSecret(ks);
28 }
29
30
31 public String encrypt(String unencryptedString) {
32 String encryptedString = null;
33 try {
34 cipher.init(Cipher.ENCRYPT_MODE, key);
35 byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
36 byte[] encryptedText = cipher.doFinal(plainText);
37 encryptedString = new String(Base64.encodeBase64(encryptedText));
38 } catch (Exception e) {
39 e.printStackTrace();
40 }
41 return encryptedString;
42 }
43
44
45 public String decrypt(String encryptedString) {
46 String decryptedText=null;
47 try {
48 cipher.init(Cipher.DECRYPT_MODE, key);
49 byte[] encryptedText = Base64.decodeBase64(encryptedString);
50 byte[] plainText = cipher.doFinal(encryptedText);
51 decryptedText= new String(plainText);
52 } catch (Exception e) {
53 e.printStackTrace();
54 }
55 return decryptedText;
56 }
57
58
59 public static void main(String args []) throws Exception
60 {
61 TrippleDes td= new TrippleDes();
62
63 String target="imparator";
64 String encrypted=td.encrypt(target);
65 String decrypted=td.decrypt(encrypted);
66
67 System.out.println("String To Encrypt: "+ target);
68 System.out.println("Encrypted String:" + encrypted);
69 System.out.println("Decrypted String:" + decrypted);
70
71 }
72
73}
74
1package com.javapapers.java.security;
2
3import java.io.FileInputStream;
4import java.io.FileOutputStream;
5import java.util.Random;
6
7import javax.crypto.Cipher;
8import javax.crypto.SecretKey;
9import javax.crypto.SecretKeyFactory;
10import javax.crypto.spec.PBEKeySpec;
11import javax.crypto.spec.PBEParameterSpec;
12
13public class FileEncryption {
14
15 public static void main(String[] args) throws Exception {
16
17 FileInputStream inFile = new FileInputStream("plainfile.txt");
18 FileOutputStream outFile = new FileOutputStream("plainfile.des");
19
20 String password = "javapapers";
21 PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
22 SecretKeyFactory secretKeyFactory = SecretKeyFactory
23 .getInstance("PBEWithMD5AndTripleDES");
24 SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
25
26 byte[] salt = new byte[8];
27 Random random = new Random();
28 random.nextBytes(salt);
29
30 PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 100);
31 Cipher cipher = Cipher.getInstance("PBEWithMD5AndTripleDES");
32 cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);
33 outFile.write(salt);
34
35 byte[] input = new byte[64];
36 int bytesRead;
37 while ((bytesRead = inFile.read(input)) != -1) {
38 byte[] output = cipher.update(input, 0, bytesRead);
39 if (output != null)
40 outFile.write(output);
41 }
42
43 byte[] output = cipher.doFinal();
44 if (output != null)
45 outFile.write(output);
46
47 inFile.close();
48 outFile.flush();
49 outFile.close();
50 }
51
52}