Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38361 > unrolled thread

Cipher.getInstance causing exception saying "length=4 index=7"

Started bytedpottel5@gmail.com
First post2018-06-12 04:38 -0700
Last post2018-06-12 08:33 -0700
Articles 7 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Cipher.getInstance causing exception saying "length=4 index=7" tedpottel5@gmail.com - 2018-06-12 04:38 -0700
    Re: Cipher.getInstance causing exception saying "length=4 index=7" Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-06-12 08:57 -0400
      Re: Cipher.getInstance causing exception saying "length=4 index=7" Eric Douglas <e.d.programmer@gmail.com> - 2018-06-12 07:36 -0700
        Re: Cipher.getInstance causing exception saying "length=4 index=7" Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-06-12 11:57 -0400
          Re: Cipher.getInstance causing exception saying "length=4 index=7" v_borchert@despammed.com (Volker Borchert) - 2018-06-12 19:32 +0000
    Re: Cipher.getInstance causing exception saying "length=4 index=7" Eric Douglas <e.d.programmer@gmail.com> - 2018-06-12 07:41 -0700
      Re: Cipher.getInstance causing exception saying "length=4 index=7" Eric Douglas <e.d.programmer@gmail.com> - 2018-06-12 08:33 -0700

#38361 — Cipher.getInstance causing exception saying "length=4 index=7"

Fromtedpottel5@gmail.com
Date2018-06-12 04:38 -0700
SubjectCipher.getInstance causing exception saying "length=4 index=7"
Message-ID<20305580-c3c7-4309-83ba-e3cbe594ec21@googlegroups.com>
I have a small test program to try and encrypt the message hello world.  It keeps causing a exception when it calls getinstance.  code

       void test()     
            {
            try{    
            Cipher desCipher;
            byte[] internalKey={23,55,34,66,33,77};
            String str=("hellow world");
            myDesKey = new SecretKeySpec(internalKey, 0,4,"DES");
            desCipher = Cipher.getInstance("DES");

            // exception goes off here
            desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
          
  byte[] text = str.getBytes("UTF8");
            byte[] textEncrypted = desCipher.doFinal(text);

        } catch(Exception e)
        {
           
            String m=e.getMessage();
            return false;
        }
        return true;
    }

[toc] | [next] | [standalone]


#38362

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2018-06-12 08:57 -0400
Message-ID<pfog0l$q4i$1@dont-email.me>
In reply to#38361
On 6/12/2018 7:38 AM, tedpottel5@gmail.com wrote:
> I have a small test program to try and encrypt the message hello world.  It keeps causing a exception when it calls getinstance.  code

     It would help if (1) you revealed the nature of the exception and
(2) if you showed us your code.  Yes, I know you showed us something
that you called "code," but clearly it's not "the code" that causes
an exception.  (This is clear because the code shown won't even
compile, hence it won't run, hence it will throw no exceptions,
hence it isn't "the code.")

     Post again when you're able to come up with a more complete and
more accurate problem description.

-- 
esosman@comcast-dot-net.invalid
Nine hundred fifty-three days to go.

[toc] | [prev] | [next] | [standalone]


#38363

FromEric Douglas <e.d.programmer@gmail.com>
Date2018-06-12 07:36 -0700
Message-ID<0bec52a1-055e-46bf-9f49-734331b1663c@googlegroups.com>
In reply to#38362
On Tuesday, June 12, 2018 at 8:58:08 AM UTC-4, Eric Sosman wrote:
> > I have a small test program to try and encrypt the message hello world.  It keeps causing a exception when it calls getinstance.  code
> 
>      It would help if (1) you revealed the nature of the exception and
> (2) if you showed us your code.  Yes, I know you showed us something
> that you called "code," but clearly it's not "the code" that causes
> an exception.  (This is clear because the code shown won't even
> compile, hence it won't run, hence it will throw no exceptions,
> hence it isn't "the code.")
> 
>      Post again when you're able to come up with a more complete and
> more accurate problem description.
> 

Sure it compiles, once you add the imports, fix the method definition, and add a main to make it runnable.  I ran it, didn't get any exceptions because it's catching and ignoring them.  I added a print, so here's the trace.

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
     public static boolean test() {
          try {
               Cipher desCipher;
               byte[] internalKey = { 23, 55, 34, 66, 33, 77 };
               String str = ("hellow world");
               SecretKeySpec myDesKey = new SecretKeySpec(internalKey, 0, 4, "DES");
               desCipher = Cipher.getInstance("DES");

               // exception goes off here
               desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);

               byte[] text = str.getBytes("UTF8");
               byte[] textEncrypted = desCipher.doFinal(text);

          } catch (Exception e) {
               e.printStackTrace();
               String m = e.getMessage();
               return false;
          }
          return true;
     }
     public static void main(final String[] args) {
          System.out.println(test());
     }
java.security.InvalidKeyException: Wrong key size
	at com.sun.crypto.provider.DESCrypt.init(DESCrypt.java:536)
	at com.sun.crypto.provider.ElectronicCodeBook.init(ElectronicCodeBook.java:94)
	at com.sun.crypto.provider.CipherCore.init(CipherCore.java:592)
	at com.sun.crypto.provider.CipherCore.init(CipherCore.java:468)
	at com.sun.crypto.provider.DESCipher.engineInit(DESCipher.java:186)
	at javax.crypto.Cipher.implInit(Cipher.java:801)
	at javax.crypto.Cipher.chooseProvider(Cipher.java:863)
	at javax.crypto.Cipher.init(Cipher.java:1248)
	at javax.crypto.Cipher.init(Cipher.java:1185)

[toc] | [prev] | [next] | [standalone]


#38366

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2018-06-12 11:57 -0400
Message-ID<pfoqh5$ka$1@dont-email.me>
In reply to#38363
On 6/12/2018 10:36 AM, Eric Douglas wrote:
> On Tuesday, June 12, 2018 at 8:58:08 AM UTC-4, Eric Sosman wrote:
>>> I have a small test program to try and encrypt the message hello world.  It keeps causing a exception when it calls getinstance.  code
>>
>>       It would help if (1) you revealed the nature of the exception and
>> (2) if you showed us your code.  Yes, I know you showed us something
>> that you called "code," but clearly it's not "the code" that causes
>> an exception.  (This is clear because the code shown won't even
>> compile, hence it won't run, hence it will throw no exceptions,
>> hence it isn't "the code.")
>>
>>       Post again when you're able to come up with a more complete and
>> more accurate problem description.
>>
> 
> Sure it compiles, once you add the imports, fix the method definition, and add a main to make it runnable. [...]

     ... and declare the undeclared identifier, and put the whole
thing in a class.

     My point being: Yes, it's possible to fix the broken code, run
the fixed version and observe the breakage, diagnose what happened,
and suggest a fix.  With luck and a following wind the suggested
fix to the altered code may even be helpful in repairing the unseen
original.  But there's also an excellent chance that the assorted
modifications have led to something that doesn't resemble the real
code, and that the "fix" won't help at all.

> [...] I ran it, didn't get any exceptions because it's catching and ignoring them.  I added a print, so here's the trace.
> [...]
> java.security.InvalidKeyException: Wrong key size

     Yes, yes, we can all get that far.  But did we manage to
reconstruct the shrouded-in-mystery original?  Or are we debugging
something "plausible" but unlike reality? Is the O.P. encountering
this same problem, or something rather different?

-- 
esosman@comcast-dot-net.invalid
Nine hundred fifty-three days to go.

[toc] | [prev] | [next] | [standalone]


#38367

Fromv_borchert@despammed.com (Volker Borchert)
Date2018-06-12 19:32 +0000
Message-ID<pfp746$opv$1@Gaia.teknon.de>
In reply to#38366
Eric Sosman wrote:

>      Yes, yes, we can all get that far.  But did we manage to
> reconstruct the shrouded-in-mystery original?  Or are we debugging
> something "plausible" but unlike reality? Is the O.P. encountering
> this same problem, or something rather different?

JMHO, @OP:

It's a matter of courtesy when asking others one has never met before
face to face for help, to provide as much and as accurate information
as possible.

-- 

"I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy@ncc1701.starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert  <v_borchert@despammed.com>

[toc] | [prev] | [next] | [standalone]


#38364

FromEric Douglas <e.d.programmer@gmail.com>
Date2018-06-12 07:41 -0700
Message-ID<d6b69d23-f7a5-43f4-a801-93f79804d4fc@googlegroups.com>
In reply to#38361
On Tuesday, June 12, 2018 at 7:38:49 AM UTC-4, tedpo...@gmail.com wrote:
> I have a small test program to try and encrypt the message hello world.  It keeps causing a exception when it calls getinstance.  code
> 
>        void test()     
>             {
>             try{    
>             Cipher desCipher;
>             byte[] internalKey={23,55,34,66,33,77};
>             String str=("hellow world");
>             myDesKey = new SecretKeySpec(internalKey, 0,4,"DES");
>             desCipher = Cipher.getInstance("DES");
> 
>             // exception goes off here
>             desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
>           
>   byte[] text = str.getBytes("UTF8");
>             byte[] textEncrypted = desCipher.doFinal(text);
> 
>         } catch(Exception e)
>         {
>            
>             String m=e.getMessage();
>             return false;
>         }
>         return true;
>     }

The exception is on the init, not the getInstance.  It appears your internalKey is wrong.  Did you read the Javadoc?

public SecretKeySpec(byte[] key,
                     int offset,
                     int len,
                     String algorithm)

Constructs a secret key from the given byte array, using the first len bytes of key, starting at offset inclusive. 
The bytes that constitute the secret key are those between key[offset] and key[offset+len-1] inclusive. 

This constructor does not check if the given bytes indeed specify a secret key of the specified algorithm. For example, if the algorithm is DES, this constructor does not check if key is 8 bytes long, and also does not check for weak or semi-weak keys. In order for those checks to be performed, an algorithm-specific key specification class (in this case: DESKeySpec) must be used.

[toc] | [prev] | [next] | [standalone]


#38365

FromEric Douglas <e.d.programmer@gmail.com>
Date2018-06-12 08:33 -0700
Message-ID<df520458-b03d-43ad-ba34-8ab2f2c79888@googlegroups.com>
In reply to#38364
On Tuesday, June 12, 2018 at 10:41:09 AM UTC-4, Eric Douglas wrote:
> On Tuesday, June 12, 2018 at 7:38:49 AM UTC-4, tedpo...@gmail.com wrote:
> > I have a small test program to try and encrypt the message hello world.  It keeps causing a exception when it calls getinstance.  code
> > 
> >        void test()     
> >             {
> >             try{    
> >             Cipher desCipher;
> >             byte[] internalKey={23,55,34,66,33,77};
> >             String str=("hellow world");
> >             myDesKey = new SecretKeySpec(internalKey, 0,4,"DES");
> >             desCipher = Cipher.getInstance("DES");
> > 
> >             // exception goes off here
> >             desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
> >           
> >   byte[] text = str.getBytes("UTF8");
> >             byte[] textEncrypted = desCipher.doFinal(text);
> > 
> >         } catch(Exception e)
> >         {
> >            
> >             String m=e.getMessage();
> >             return false;
> >         }
> >         return true;
> >     }
> 
> The exception is on the init, not the getInstance.  It appears your internalKey is wrong.  Did you read the Javadoc?
> 
> public SecretKeySpec(byte[] key,
>                      int offset,
>                      int len,
>                      String algorithm)
> 
> Constructs a secret key from the given byte array, using the first len bytes of key, starting at offset inclusive. 
> The bytes that constitute the secret key are those between key[offset] and key[offset+len-1] inclusive. 
> 
> This constructor does not check if the given bytes indeed specify a secret key of the specified algorithm. For example, if the algorithm is DES, this constructor does not check if key is 8 bytes long, and also does not check for weak or semi-weak keys. In order for those checks to be performed, an algorithm-specific key specification class (in this case: DESKeySpec) must be used.

If I change the value, the method returns true.  If you want a 4 byte encryption don't use DES.

               byte[] internalKey = { 23, 55, 34, 66, 33, 77, 32, 73 };
               String str = ("hellow world");
               SecretKeySpec myDesKey = new SecretKeySpec(internalKey, 0, 8, "DES");

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web