Repository: commons-crypto
Updated Branches:
  refs/heads/master 1335e165e -> f54e331d4


CRYPTO-56: add informative error message, keep consistent with JDK JDK's error 
message is more informative. it would be better to keep consistent with JDK


Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/f54e331d
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/f54e331d
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/f54e331d

Branch: refs/heads/master
Commit: f54e331d4a6650000bc1819d74307e963435a258
Parents: 1335e16
Author: kexianda <xianda...@intel.com>
Authored: Fri May 6 20:48:58 2016 +0800
Committer: Sun Dapeng <s...@apache.org>
Committed: Wed May 11 13:24:59 2016 +0800

----------------------------------------------------------------------
 .../native/org/apache/commons/crypto/cipher/OpensslNative.c    | 6 ++++--
 .../org/apache/commons/crypto/cipher/OpensslCipherTest.java    | 4 ++--
 2 files changed, 6 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/f54e331d/src/main/native/org/apache/commons/crypto/cipher/OpensslNative.c
----------------------------------------------------------------------
diff --git a/src/main/native/org/apache/commons/crypto/cipher/OpensslNative.c 
b/src/main/native/org/apache/commons/crypto/cipher/OpensslNative.c
index c4000e9..8ed8cff 100644
--- a/src/main/native/org/apache/commons/crypto/cipher/OpensslNative.c
+++ b/src/main/native/org/apache/commons/crypto/cipher/OpensslNative.c
@@ -248,11 +248,13 @@ JNIEXPORT jlong JNICALL 
Java_org_apache_commons_crypto_cipher_OpensslNative_init
   int jIvLen = (*env)->GetArrayLength(env, iv);
   if (jKeyLen != KEY_LENGTH_128 && jKeyLen != KEY_LENGTH_192
         && jKeyLen != KEY_LENGTH_256) {
-    THROW(env, "java/security/InvalidKeyException", "Invalid key length.");
+    char str[64] = {0};
+    snprintf(str, sizeof(str), "Invalid AES key length: %d bytes", jKeyLen);
+    THROW(env, "java/security/InvalidKeyException", str);
     return (jlong)0;
   }
   if (jIvLen != IV_LENGTH) {
-    THROW(env, "java/security/InvalidAlgorithmParameterException", "Wrong IV 
length.");
+    THROW(env, "java/security/InvalidAlgorithmParameterException", "Wrong IV 
length: must be 16 bytes long");
     return (jlong)0;
   }
 

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/f54e331d/src/test/java/org/apache/commons/crypto/cipher/OpensslCipherTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/commons/crypto/cipher/OpensslCipherTest.java 
b/src/test/java/org/apache/commons/crypto/cipher/OpensslCipherTest.java
index c66936a..0eaccb3 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/OpensslCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/OpensslCipherTest.java
@@ -149,7 +149,7 @@ public class OpensslCipherTest extends AbstractCipherTest {
             cipher.init(Openssl.ENCRYPT_MODE, invalidKey, IV);
             Assert.fail("java.security.InvalidKeyException should be thrown.");
         } catch (Exception e) {
-            Assert.assertTrue(e.getMessage().contains("Invalid key length."));
+            Assert.assertTrue(e.getMessage().contains("Invalid AES key length: 
" + invalidKey.length + " bytes"));
             throw e;
         }
     }
@@ -167,7 +167,7 @@ public class OpensslCipherTest extends AbstractCipherTest {
             cipher.init(Openssl.ENCRYPT_MODE, KEY, invalidIV);
             Assert.fail("java.security.InvalidAlgorithmParameterException 
should be thrown.");
         } catch (Exception e) {
-            Assert.assertTrue(e.getMessage().contains("Wrong IV length."));
+            Assert.assertTrue(e.getMessage().contains("Wrong IV length: must 
be 16 bytes long"));
             throw e;
         }
     }

Reply via email to