Details
-
Type:
Bug
-
Status: Closed
-
Priority:
Major
-
Resolution: Not a bug
-
Affects Version/s: 1.45
-
Fix Version/s: None
-
Labels:None
-
Environment:Linux 2.4.21-47.ELsmp i686
Description
Hi,
I develop a really simple class to do encrypt and decrypt with some unit test to do simple test to verify everything is working. My codes work fine on my development box OS: Windows XP Workstation - Service Pack 3. But when my build server is picking it up and build the codes, unit tests failed. My build server has OS: Linux 2.4.21-47.ELsmp i686.
I've got someone to do a test on his dev machine and he is using Lunix. He also have errors when try to run this test. The problem is that when we deploy, we will deploy to Lunix box and I can't get away until solving this problem.
I keep getting the following errors:
java.lang.RuntimeException: last block incomplete in decryption
at net.flitech.tundra.corona.Cryptography.decrypt(Cryptography.java:40)
at net.flitech.tundra.corona.CryptographyUnitTest.testEncryptDecrypt(CryptographyUnitTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:36)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:420)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:911)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:768)
Caused by: javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at net.flitech.tundra.corona.Cryptography.decrypt(Cryptography.java:37)
... 21 more
--------------------------------------------------------------------
This is my main code:
--------------------------------------------------------------------
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
public class Cryptography {
private static final byte[] SECRET_KEY = Hex.decode("874ba21fab0ec277c153b0d95ff1ade2");
public String encrypt(String decryptedString) {
if (decryptedString == null) throw new IllegalArgumentException("encrypted string is null");
Security.addProvider(new BouncyCastleProvider());
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
SecretKeySpec spec = new SecretKeySpec(SECRET_KEY, "AES");
cipher.init(Cipher.ENCRYPT_MODE, spec);
byte[] encryptedText = cipher.doFinal(decryptedString.getBytes());
return new String(encryptedText);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public String decrypt(String encryptedString) {
if (encryptedString == null) throw new IllegalArgumentException("encrypted string is null");
Security.addProvider(new BouncyCastleProvider());
byte[] ciphertext = encryptedString.getBytes();
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(SECRET_KEY, "AES"));
byte[] cleartext = cipher.doFinal(ciphertext);
return new String(cleartext);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
----------------------------------------------------------------------------------
This is my unit test class
----------------------------------------------------------------------------------
import static org.junit.Assert.assertEquals;
import java.security.Security;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;
public class CryptographyUnitTest {
private Logger logger = Logger.getLogger(CryptographyUnitTest.class);
@Test
public void generateKey() {
try {
Security.addProvider(new BouncyCastleProvider());
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "BC");
keyGenerator.init(128);
SecretKey key = keyGenerator.generateKey();
byte[] encodedKey = key.getEncoded();
System.out.println("key=<" + new String(Hex.encode(encodedKey), "UTF-8") + ">");
logger.debug("key=<" + new String(Hex.encode(encodedKey), "UTF-8") + ">");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testEncryptDecrypt() {
String text = "This is something interesting";
Cryptography crypto = new Cryptography();
String encryptedText = crypto.encrypt(text);
// System.out.println("encryptedText=" + new String(Hex.encode(encryptedText.getBytes())));
String decryptedText = crypto.decrypt(encryptedText);
// System.out.println("decryptedText=" + decryptedText);
assertEquals(text, decryptedText);
}
}
I develop a really simple class to do encrypt and decrypt with some unit test to do simple test to verify everything is working. My codes work fine on my development box OS: Windows XP Workstation - Service Pack 3. But when my build server is picking it up and build the codes, unit tests failed. My build server has OS: Linux 2.4.21-47.ELsmp i686.
I've got someone to do a test on his dev machine and he is using Lunix. He also have errors when try to run this test. The problem is that when we deploy, we will deploy to Lunix box and I can't get away until solving this problem.
I keep getting the following errors:
java.lang.RuntimeException: last block incomplete in decryption
at net.flitech.tundra.corona.Cryptography.decrypt(Cryptography.java:40)
at net.flitech.tundra.corona.CryptographyUnitTest.testEncryptDecrypt(CryptographyUnitTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:36)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:420)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:911)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:768)
Caused by: javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at net.flitech.tundra.corona.Cryptography.decrypt(Cryptography.java:37)
... 21 more
--------------------------------------------------------------------
This is my main code:
--------------------------------------------------------------------
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
public class Cryptography {
private static final byte[] SECRET_KEY = Hex.decode("874ba21fab0ec277c153b0d95ff1ade2");
public String encrypt(String decryptedString) {
if (decryptedString == null) throw new IllegalArgumentException("encrypted string is null");
Security.addProvider(new BouncyCastleProvider());
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
SecretKeySpec spec = new SecretKeySpec(SECRET_KEY, "AES");
cipher.init(Cipher.ENCRYPT_MODE, spec);
byte[] encryptedText = cipher.doFinal(decryptedString.getBytes());
return new String(encryptedText);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public String decrypt(String encryptedString) {
if (encryptedString == null) throw new IllegalArgumentException("encrypted string is null");
Security.addProvider(new BouncyCastleProvider());
byte[] ciphertext = encryptedString.getBytes();
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(SECRET_KEY, "AES"));
byte[] cleartext = cipher.doFinal(ciphertext);
return new String(cleartext);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
----------------------------------------------------------------------------------
This is my unit test class
----------------------------------------------------------------------------------
import static org.junit.Assert.assertEquals;
import java.security.Security;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;
public class CryptographyUnitTest {
private Logger logger = Logger.getLogger(CryptographyUnitTest.class);
@Test
public void generateKey() {
try {
Security.addProvider(new BouncyCastleProvider());
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "BC");
keyGenerator.init(128);
SecretKey key = keyGenerator.generateKey();
byte[] encodedKey = key.getEncoded();
System.out.println("key=<" + new String(Hex.encode(encodedKey), "UTF-8") + ">");
logger.debug("key=<" + new String(Hex.encode(encodedKey), "UTF-8") + ">");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testEncryptDecrypt() {
String text = "This is something interesting";
Cryptography crypto = new Cryptography();
String encryptedText = crypto.encrypt(text);
// System.out.println("encryptedText=" + new String(Hex.encode(encryptedText.getBytes())));
String decryptedText = crypto.decrypt(encryptedText);
// System.out.println("decryptedText=" + decryptedText);
assertEquals(text, decryptedText);
}
}