JavaBlog.fr / Java.lu Cryptology,DEVELOPMENT,Java Java/Crypto: Encrypt your message simply with Cipher

Java/Crypto: Encrypt your message simply with Cipher

Hi,

After a long time, during which I was very busy by my personal project (building my house), I am come back with a simple article concerning the encrypting of message via the standard Cipher class. This class provides the functionality of a cryptographic cipher for encryption and decryption. It forms the core of the Java Cryptographic Extension (JCE) framework.

In the following example, I will encrypt and decrypt a message via the symmetric algorithm “AES/ECB/PKCS5Padding” (via a secret key). A transformation is of the form “algorithm/mode/padding” or “algorithm”.

01package com.ho.crypto.test1;
02 
03import java.security.InvalidKeyException;
04import java.security.NoSuchAlgorithmException;
05import java.util.Random;
06 
07import javax.crypto.BadPaddingException;
08import javax.crypto.Cipher;
09import javax.crypto.IllegalBlockSizeException;
10import javax.crypto.NoSuchPaddingException;
11import javax.crypto.spec.SecretKeySpec;
12 
13 
14/**
15 * Encrypt / Decrypt your message with Cipher Class.
16 * @author huseyin
17 *
18 */
19public class MyProtectorSingleton {
20 
21     
22        // --------------------------------- PRIVATE ATTRIBUTES
23        private static MyProtectorSingleton instance = null;
24         
25        private SecretKeySpec certificate = null;
26        private Cipher aesEncrypt = null;
27        private Cipher aesDecrypt = null;
28         
29        // ----------------------------------- CONSTRUCTORS
30        private MyProtectorSingleton() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
31            Random myRandom = new Random();
32            byte[] myBytesArray = new byte[16];
33            myRandom.nextBytes(myBytesArray);
34            certificate = new SecretKeySpec(myBytesArray, 0, 16, "AES");
35             
36            aesEncrypt = Cipher.getInstance("AES/ECB/PKCS5Padding");
37            aesEncrypt.init(Cipher.ENCRYPT_MODE, certificate);
38             
39            aesDecrypt = Cipher.getInstance("AES/ECB/PKCS5Padding");
40            aesDecrypt.init(Cipher.DECRYPT_MODE,certificate);
41        }
42         
43        public static synchronized MyProtectorSingleton getInstance() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
44            if(instance == null){
45                instance = new MyProtectorSingleton();
46            }
47            return instance;
48        }
49         
50        // ----------------------------- PUBLIC FUNCTIONS
51        public byte[] protect(String toProtect) throws IllegalBlockSizeException, BadPaddingException{
52            byte[] cipherText = aesEncrypt.doFinal(toProtect.getBytes());
53            return cipherText;
54        }
55         
56        public String unprotect(byte[] toUnprotect) throws IllegalBlockSizeException, BadPaddingException{
57            String cleartext = new String(aesDecrypt.doFinal(toUnprotect));
58            return cleartext;
59        }
60         
61        public static void main(String[] args) {
62            try{
63                String toProtect = "this is my message";
64                MyProtectorSingleton myProtector = MyProtectorSingleton.getInstance();
65                 
66                //
67                byte[] msgProtected = myProtector.protect(toProtect);
68                System.out.println("msgProtected="+msgProtected);
69                 
70                //
71                String str = myProtector.unprotect(msgProtected);
72                System.out.println(str);
73            }catch(Exception ex){
74                ex.printStackTrace();
75            }
76        }
77}

That’s all!!!!

Kind regards,

Huseyin OZVEREN

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post