• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java encryption...

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm really new to any of the java encryption stuff and tried to write a real simple program to test it out:



When I run it I get the following output:


It appearently doesn't encrypt the string at all and throws an exception when trying to decrypt.

-Tad
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this thread. The code contained in one of the responses works fine.
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the code to
KeyGenerator kg = KeyGenerator.getInstance("DESede");

But getting the java.security.InvalidKeyException: Invalid key length: 24 bytes

Any solurions?

Rgds,

Seetesh
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Triple DES (aka DESede) needs a 192 bit key.
 
chris up
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry that was the most redundant post ever (24 bytes = 192 bits), i was definately not thinking.

Try this to fix your original problem...

c.init(Cipher.DECRYPT_MODE, skeySpec, c.getParameters());

When initializing a cipher with a generated key in encrypt mode there are some parameters which are created underneath the covers which need to be given to the decrypter (related to the mode and padding which you specified in getting the instance of the cipher).
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are three obvious problems with yoour code.

1) Using
c = Cipher.getInstance("DES/CBC/PKCS5Padding");
indicates that you wish to use CBC mode so you will need an initialisation vector (IV). For DES this will be 8 bytes used as
byte[] ivBytes = your IV bytes
IvParameterSpec iv = new IvParameterSpec(ivBytes);
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key, iv);
You will need to supply the same IV when you decrypt.

2) Using
String y = new String(c.doFinal(x.getBytes()));
has two problems -

a) x.getBytes() uses the default platform encoding to turn characters into bytes so you 'may' get a different result for different platforms. It is normally better to specify the encoding i.e. x.getBytes("utf-8") then the same bytes will be generated for ALL platforms.
b) String y = new String(c.doFinal(...))
tries to turn the arbitrary bytes generated by the encryption process into a String using the default platform encoding. As well as the problems outlined in a), this has the additional problem that not all bytes and byte sequences can represent a valid character so the transformation is frequently not reversible. If you MUST have a String result from the encryption then use Base64 or Hex to armor the bytes.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic