I am confused by JCE, I have tried a number of examples and example codes of encrypting and decrypting some text using JCE encryption decryption techniques but reaching on a confusing conclusion or may be missing the whole concept either. Actually all I wanted was to encrypt some text using various famous algorithms with a human readable alphanumeric key and then decrypt it back with same key. Here is the first example I tried
String text = "Hello World";
String key = "Bar12345kjkj5454hggx1234";
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "DESede");
Cipher cipher = Cipher.getInstance("DESede");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.err.println("Using Tripple DES algorithm and with key <"+key+">, <"+text+"> converted into <"+new String(encrypted)+">");
// decrypt the text
String key1 = "Bar12345kkkj5454hggx1234"; // 128 bit key
Key aesKey1 = new SecretKeySpec(key1.getBytes(), "DESede");
Cipher cipher1 = Cipher.getInstance("DESede");
cipher1.init(Cipher.DECRYPT_MODE, aesKey1);
String decrypted = new String(cipher1.doFinal(encrypted));
System.err.println("Using Tripple DES algorithm and with key <"+key1+">, encrypted text <"+new String(encrypted)+"> decrypted into <"+decrypted+">");
The confusing thing is with a different key also I can decrypt the data which was originally encrypted using a different key, does it make any sense. It should fail if the supplied key for decryption is not exactly same as the one which was used for encryption.
Can somebody please help me to understand why it is happening like this and what is the purpose of having this algorithms if we can decrypt the data with a wrong key as well, what's the point of security then?
Thanks in advance! Manish