X-Received: by 2002:ac8:44d3:: with SMTP id b19-v6mr319950qto.51.1528814175910; Tue, 12 Jun 2018 07:36:15 -0700 (PDT) X-Received: by 2002:a81:3487:: with SMTP id b129-v6mr27520ywa.13.1528814175620; Tue, 12 Jun 2018 07:36:15 -0700 (PDT) Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!h2-v6no161834qtp.0!news-out.google.com!u13-v6ni802qtg.0!nntp.google.com!h2-v6no161831qtp.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Tue, 12 Jun 2018 07:36:15 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=50.78.95.177; posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H NNTP-Posting-Host: 50.78.95.177 References: <20305580-c3c7-4309-83ba-e3cbe594ec21@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <0bec52a1-055e-46bf-9f49-734331b1663c@googlegroups.com> Subject: Re: Cipher.getInstance causing exception saying "length=4 index=7" From: Eric Douglas Injection-Date: Tue, 12 Jun 2018 14:36:15 +0000 Content-Type: text/plain; charset="UTF-8" Lines: 51 Xref: csiph.com comp.lang.java.programmer:38363 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)