1 Reply Latest reply: Feb 9, 2013 8:29 AM by 890619 RSS

    Cipher.doFinal(byte[]) returns null :(

    890619
      Hello experts!

      I am requesting your help because faced one issue with javax.crypto.Cipher.

      I have to generate key check value for the 3DES key, but, doFinal(byte[]) returns null. My code is following:
      private static final byte[] ZEROES = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
          public static String generateKeyCheckValue (byte[] key) {
               if (key.length == 16)
                    key = make3LengthKey(key);
               Log.DEBUG("Key: " + Convertor.byte2hex(key));
               String kcv = Convertor.byte2hex(desEdeEcbEncrypt(key, ZEROES));
               Log.DEBUG("KCV: " + kcv);
               return kcv;
          }
          public static byte[] desEdeEcbEncrypt (byte[] key, byte[] data) {
               try {
                    Log.debug("key = ", key);
                    Log.debug("data = ", data);
                    Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
                    SecretKeySpec ready_key = new SecretKeySpec(key, "DESede");
                    cipher.init(Cipher.ENCRYPT_MODE, ready_key);
                    data = cipher.doFinal();
                    Log.debug("data = ", data);
                    return data;
               }
               catch (Exception e) {
                    Log.ERROR("Exception while DESede/ECB encryption: " + e.getMessage(), e);
                    return null;
               }
          }
      And here are fragment from log file, of the KCV generation:
      11:50:07:929|DEBUG|c.t.t.h.crypto.Crypto              |New key created: 1A5407A4C8975D10F7766DBF49107FC7
      11:50:07:930|DEBUG|c.t.t.h.crypto.Crypto              |Checking week and semi-week DES key table...
      11:50:07:930|DEBUG|c.t.t.h.crypto.Crypto              |Picked block of key
      11:50:07:931|DEBUG|c.t.t.h.crypto.Crypto              |Picked block of key
      11:50:07:931|DEBUG|c.t.t.h.crypto.Crypto              |Key: 1A5407A4C8975D10F7766DBF49107FC71A5407A4C8975D10
      11:50:07:932|debug|c.t.t.h.crypto.Crypto              |key = 1a 54 7 a4 c8 97 5d 10 f7 76 6d bf 49 10 7f c7 1a 54 7 a4 c8 97 5d 10
      11:50:07:933|debug|c.t.t.h.crypto.Crypto              |data = 0 0 0 0 0 0 0 0
      11:50:07:933|debug|c.t.t.h.crypto.Crypto              |data =
      11:50:07:934|DEBUG|c.t.t.h.crypto.Crypto              |KCV:
      11:50:07:934|DEBUG|c.t.t.h.crypto.schemas.ThalesKey   |Generated new key: 1A5407A4C8975D10F7766DBF49107FC7
      11:50:07:935|DEBUG|c.t.t.h.crypto.schemas.ThalesKey   |Key check value:
      11:50:07:935|DEBUG|c.t.t.h.host.commands.A0           |Parsed master key flag: 0
      11:50:07:940|DEBUG|c.t.t.h.host.Server                |Response: 0006A100U1A5407A4C8975D10F7766DBF49107FC7
      I am using Java 1.6.0_29.

      Please advice, what am i doing wrong.

      Thanx!

      Juris