jazzIIIlove
asked on
Simple Symmetric Encryption in Java problem
I have a simple symmetric encryption taken from the book "Beginning Cryptography with Java
by David Hook".
I have a problem that I cannot run the original code as expected.
In the very original code below:
I got the runtime error as:
Exception in thread "main" java.security.NoSuchProvid erExceptio n: No such provider: BC
at javax.crypto.Cipher.getIns tance(Unkn own Source)
at SimpleSymmetricExample.mai n(SimpleSy mmetricExa mple.java: 23)
If I convert the line from:
Cipher cipher = Cipher.getInstance("AES/EC B/NoPaddin g", "BC");
to:
Cipher cipher = Cipher.getInstance("AES");
I got the output as:
input text : 00112233445566778899aabbcc ddeeff
Exception in thread "main" java.security.InvalidKeyEx ception: Illegal key size or default parameters
at javax.crypto.Cipher.a(Unkn own Source)
at javax.crypto.Cipher.a(Unkn own Source)
at javax.crypto.Cipher.a(Unkn own Source)
at javax.crypto.Cipher.init(U nknown Source)
at javax.crypto.Cipher.init(U nknown Source)
at SimpleSymmetricExample.mai n(SimpleSy mmetricExa mple.java: 31)
line 31 is as follows:
cipher.init(Cipher.ENCRYPT _MODE, key);
Note that I have NOT used any library in the first place.
I also tried to use the jar files in the link: http://www.bouncycastle.org/download/crypto-144.zip
What should I do to fix it?
Best regards.
by David Hook".
I have a problem that I cannot run the original code as expected.
In the very original code below:
I got the runtime error as:
Exception in thread "main" java.security.NoSuchProvid
at javax.crypto.Cipher.getIns
at SimpleSymmetricExample.mai
If I convert the line from:
Cipher cipher = Cipher.getInstance("AES/EC
to:
Cipher cipher = Cipher.getInstance("AES");
I got the output as:
input text : 00112233445566778899aabbcc
Exception in thread "main" java.security.InvalidKeyEx
at javax.crypto.Cipher.a(Unkn
at javax.crypto.Cipher.a(Unkn
at javax.crypto.Cipher.a(Unkn
at javax.crypto.Cipher.init(U
at javax.crypto.Cipher.init(U
at SimpleSymmetricExample.mai
line 31 is as follows:
cipher.init(Cipher.ENCRYPT
Note that I have NOT used any library in the first place.
I also tried to use the jar files in the link: http://www.bouncycastle.org/download/crypto-144.zip
What should I do to fix it?
Best regards.
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* Basic symmetric encryption example
*/
////Utils
class Utils
{
private static String digits = "0123456789abcdef";
/**
* Return length many bytes of the passed in byte array as a hex string.
*
* @param data the bytes to be converted.
* @param length the number of bytes in the data block to be converted.
* @return a hex representation of length bytes of data.
*/
public static String toHex(byte[] data, int length)
{
StringBuffer buf = new StringBuffer();
for (int i = 0; i != length; i++)
{
int v = data[i] & 0xff;
buf.append(digits.charAt(v >> 4));
buf.append(digits.charAt(v & 0xf));
}
return buf.toString();
}
/**
* Return the passed in byte array as a hex string.
*
* @param data the bytes to be converted.
* @return a hex representation of data.
*/
public static String toHex(byte[] data)
{
return toHex(data, data.length);
}
}
//////SimpleSymmetricExample
public class SimpleSymmetricExample
{
public static void main(String[] args) throws Exception
{
byte[] input = new byte[] {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
(byte)0x88, (byte)0x99, (byte)0xaa, (byte)0xbb,
(byte)0xcc, (byte)0xdd, (byte)0xee, (byte)0xff };
byte[] keyBytes = new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC");
System.out.println("input text : " + Utils.toHex(input));
// encryption pass
byte[] cipherText = new byte[input.length];
cipher.init(Cipher.ENCRYPT_MODE, key);
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
System.out.println("cipher text: " + Utils.toHex(cipherText)
+ " bytes: " + ctLength);
// decryption pass
byte[] plainText = new byte[ctLength];
cipher.init(Cipher.DECRYPT_MODE, key);
int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
ptLength += cipher.doFinal(plainText, ptLength);
System.out.println("plain text : " + Utils.toHex(plainText)
+ " bytes: " + ptLength);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
As in the code snippet, I added Cipher cipher = Cipher.getInstance("DES"); and Security.addProvider(new org.bouncycastle.jce.provi der.Bouncy CastleProv ider());
but the result is as follows:
What am I doing wrong?
input text : 00112233445566778899aabbcc ddeeff
Exception in thread "main" java.security.InvalidKeyEx ception
at com.ibm.crypto.provider.DE SCipher.en gineGetKey Size(Unkno wn Source)
at javax.crypto.Cipher.b(Unkn own Source)
at javax.crypto.Cipher.a(Unkn own Source)
at javax.crypto.Cipher.a(Unkn own Source)
at javax.crypto.Cipher.a(Unkn own Source)
at javax.crypto.Cipher.init(U nknown Source)
at javax.crypto.Cipher.init(U nknown Source)
at SimpleSymmetricExample.mai n(SimpleSy mmetricExa mple.java: 74)
but the result is as follows:
What am I doing wrong?
input text : 00112233445566778899aabbcc
Exception in thread "main" java.security.InvalidKeyEx
at com.ibm.crypto.provider.DE
at javax.crypto.Cipher.b(Unkn
at javax.crypto.Cipher.a(Unkn
at javax.crypto.Cipher.a(Unkn
at javax.crypto.Cipher.a(Unkn
at javax.crypto.Cipher.init(U
at javax.crypto.Cipher.init(U
at SimpleSymmetricExample.mai
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* Basic symmetric encryption example
*/
////Utils
class Utils
{
private static String digits = "0123456789abcdef";
/**
* Return length many bytes of the passed in byte array as a hex string.
*
* @param data the bytes to be converted.
* @param length the number of bytes in the data block to be converted.
* @return a hex representation of length bytes of data.
*/
public static String toHex(byte[] data, int length)
{
StringBuffer buf = new StringBuffer();
for (int i = 0; i != length; i++)
{
int v = data[i] & 0xff;
buf.append(digits.charAt(v >> 4));
buf.append(digits.charAt(v & 0xf));
}
return buf.toString();
}
/**
* Return the passed in byte array as a hex string.
*
* @param data the bytes to be converted.
* @return a hex representation of data.
*/
public static String toHex(byte[] data)
{
return toHex(data, data.length);
}
}
//////SimpleSymmetricExample
public class SimpleSymmetricExample
{
public static void main(String[] args) throws Exception
{
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] input = new byte[] {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
(byte)0x88, (byte)0x99, (byte)0xaa, (byte)0xbb,
(byte)0xcc, (byte)0xdd, (byte)0xee, (byte)0xff };
byte[] keyBytes = new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
//Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC");
Cipher cipher = Cipher.getInstance("DES");
System.out.println("input text : " + Utils.toHex(input));
// encryption pass
byte[] cipherText = new byte[input.length];
cipher.init(Cipher.ENCRYPT_MODE, key);
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
System.out.println("cipher text: " + Utils.toHex(cipherText)
+ " bytes: " + ctLength);
// decryption pass
byte[] plainText = new byte[ctLength];
cipher.init(Cipher.DECRYPT_MODE, key);
int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
ptLength += cipher.doFinal(plainText, ptLength);
System.out.println("plain text : " + Utils.toHex(plainText)
+ " bytes: " + ptLength);
}
}
ASKER
the problem is in the line: cipher.init(Cipher.ENCRYPT _MODE, key);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Cipher c1 = Cipher.getInstance("DES");
for reference see
http://java.sun.com/j2se/1.4.2/docs/guide/security/jce/JCERefGuide.html#SimpleEncrEx