svn commit: r670148 - in /ofbiz/trunk/framework: base/src/base/org/ofbiz/base/util/StringUtil.java entity/src/org/ofbiz/entity/util/EntityCrypto.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r670148 - in /ofbiz/trunk/framework: base/src/base/org/ofbiz/base/util/StringUtil.java entity/src/org/ofbiz/entity/util/EntityCrypto.java

jonesde
Author: jonesde
Date: Fri Jun 20 23:07:16 2008
New Revision: 670148

URL: http://svn.apache.org/viewvc?rev=670148&view=rev
Log:
Fixed problem with old credit card and other old encrypted fields from before fixing the hex encoding, ie using what the rest of the world uses instead of the buggy old hex encoding that was previously used in OFBiz; now credit card numbers and other entity fields that are encrypted should unencrypt properly, ie it tries the new way and if decryption fails then is tries the old way

Modified:
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityCrypto.java

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java?rev=670148&r1=670147&r2=670148&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java Fri Jun 20 23:07:16 2008
@@ -352,7 +352,23 @@
             throw new GeneralRuntimeException(e);
         }
     }
-
+    
+    public static byte[] fromHexStringOldFunnyVariety(String str) {
+        str = cleanHexString(str);
+        int stringLength = str.length();
+        if ((stringLength & 0x1) != 0) {
+            throw new IllegalArgumentException("fromHexString requires an even number of hex characters");
+        }
+        byte[] b = new byte[stringLength / 2];
+
+        for (int i = 0, j = 0; i < stringLength; i+= 2, j++) {
+            int high = convertChar(str.charAt(i));
+            int low = convertChar(str.charAt(i+1));
+            b[j] = (byte) ((high << 4) | low);
+        }
+        return b;
+    }
+    
     private static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
     public static int convertChar(char c) {
         if ( '0' <= c && c <= '9' ) {

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityCrypto.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityCrypto.java?rev=670148&r1=670147&r2=670148&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityCrypto.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityCrypto.java Fri Jun 20 23:07:16 2008
@@ -71,7 +71,9 @@
     /** Encrypts a String into an encrypted hex encoded byte array */
     public String encrypt(String keyName, Object obj) throws EntityCryptoException {
         try {
-            return StringUtil.toHexString(DesCrypt.encrypt(this.getKey(keyName), UtilObject.getBytes(obj)));
+            byte[] encryptedBytes = DesCrypt.encrypt(this.getKey(keyName), UtilObject.getBytes(obj));
+            String hexString = StringUtil.toHexString(encryptedBytes);
+            return hexString;
         } catch (GeneralException e) {
             throw new EntityCryptoException(e);
         }
@@ -80,7 +82,15 @@
     /** Decrypts a hex encoded byte array into a String */
     public Object decrypt(String keyName, String str) throws EntityCryptoException {
         try {
-            return UtilObject.getObject(DesCrypt.decrypt(this.getKey(keyName), StringUtil.fromHexString(str)));
+            byte[] encryptedBytes = StringUtil.fromHexString(str);
+            byte[] decryptedBytes = DesCrypt.decrypt(this.getKey(keyName), encryptedBytes);
+            if (encryptedBytes.equals(decryptedBytes)) {
+                // try using the old/bad hex encoding approach
+                encryptedBytes = StringUtil.fromHexStringOldFunnyVariety(str);
+                decryptedBytes = DesCrypt.decrypt(this.getKey(keyName), encryptedBytes);
+            }
+            
+            return UtilObject.getObject(decryptedBytes);
         } catch (GeneralException e) {
             throw new EntityCryptoException(e);
         }