Results 1 to 2 of 2
  1. #1
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    52
    Rep Power
    0

    Default Encyption test cases not working

    Hi All,
    I have following Cipher Code and test for it. But I am getting following exception. Any idea what I am doing wrong?

    Java Code:
    java.lang.IllegalStateException: Cipher not initialized
    	at javax.crypto.Cipher.checkCipherState(Cipher.java:1672)
    	at javax.crypto.Cipher.doFinal(Cipher.java:2079)
    	at com.anjib.util.CipherUtil.encrypt(CipherUtil.java:67)
    	at com.anjib.util.CipherTest.testEncryptDecrypt(CipherTest.java:23)
    Java Code:
    public class CipherUtil {
    	private static Logger log = Logger.getLogger(CipherUtil.class);
    	private static final String SECRET_KEY = "XXX";
    	private Cipher cipher;
        private SecretKeySpec secretKeySpec;
        
    	private static CipherUtil cipherUtil;
    	
    	private CipherUtil() {
    		try {
                cipher = Cipher.getInstance("AES");
            } catch (NoSuchAlgorithmException | NoSuchPaddingException ex) {
                log.error(ex);
            }
            byte[] key = null;
            try {
                key = Hex.decodeHex(SECRET_KEY.toCharArray());
            } catch (DecoderException ex) {
            	log.error(ex);
            }
            secretKeySpec = new SecretKeySpec(key, "AES");
    	}
    	
    	public static synchronized CipherUtil getCipherUtilObject() {
    		if (cipherUtil == null) {
    			cipherUtil = new CipherUtil();
    		}
    		return cipherUtil;
    	}
    	
    	public Object clone() throws CloneNotSupportedException {
    		throw new CloneNotSupportedException();
    	}
        
        public String encrypt(String plainText) {
        	if (plainText == null) return null;
        	String encryptedText = null;
        	byte[] encrypted = null;
        	
        	synchronized (cipher) {
        		try {
    				cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    			} catch (InvalidKeyException e) {
    				log.error(e.getMessage());
    			}
        	}
    		
        	synchronized (cipher) {
        		try {
    				encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
    				encryptedText = new String(Base64.encodeBase64(encrypted));
    			} catch (IllegalBlockSizeException | BadPaddingException
    					| UnsupportedEncodingException e) {
    				log.error(e.getMessage());
    			}
        	}
    				
    		return encryptedText;
    	}
    
    	public synchronized  String decrypt(String encryptedText) {
    		if (encryptedText == null) return null;
    		byte[] toDecrypt = null;
    		byte[] original = null;
    		String decryptedText = null;
    		
    		synchronized (cipher) {
    			try {
    				cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    			} catch (InvalidKeyException e) {
    				log.error(e.getMessage());
    			}
    		}
    		toDecrypt = Base64.decodeBase64(encryptedText);
    		synchronized (cipher) {
    			try {
    				original = cipher.doFinal(toDecrypt);
    			} catch (IllegalBlockSizeException | BadPaddingException e) {
    				log.error(e.getMessage());
    			}
    		}
    		try {
    			decryptedText = new String(original,"UTF-8");
    		} catch (UnsupportedEncodingException e) {
    			log.error(e.getMessage());
    		}
    		
    		return decryptedText;
    	}
    }
    Java Code:
    public class CipherTest {
    	CipherUtil cipherUtil;
    	
    	@Before 
    	public void setUp() {
    		cipherUtil = CipherUtil.getCipherUtilObject();
    	}
    	
    	@Test
    	public void testEncryptDecrypt() {
    		String plainText = "Secret Message";
    		String encryptedText = cipherUtil.encrypt(plainText);
    		assertThat(encryptedText, not(equalTo(plainText)));
    		String decryptedText = cipherUtil.decrypt(encryptedText);
    		assertThat(decryptedText, is(equalTo(plainText)));
    		assertThat(encryptedText, not(equalTo(decryptedText)));
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Encyption test cases not working

    Apparently, somewhere you are trying to use the cipher before it is initialized. Interesting though how you are getting
    an error from line 23 in a class that only has 18 lines. You may want to sprinkle some print statements around your code
    to try and determine when you are instantiating and initializing the cipher compared to when you are using it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. How to work with eclipse plugin Junit test cases
    By mohanever4u in forum Eclipse
    Replies: 0
    Last Post: 11-06-2013, 02:37 PM
  2. Where do I start with test cases?!
    By Libertad in forum New To Java
    Replies: 0
    Last Post: 04-18-2013, 11:49 AM
  3. Replies: 1
    Last Post: 04-08-2013, 04:59 AM
  4. test cases for threads
    By phoenox7 in forum Java Software
    Replies: 1
    Last Post: 02-24-2013, 08:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •