File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Security and the fly likes AES Encryption Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "AES Encryption" Watch "AES Encryption" New topic
Author

AES Encryption

Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
Hello everyone,
I got a small question on encryption, actually I am trying to learn the various techniques to encrypt a message. Now I am trying AES, using the built in security package in Java. The problem is that I am having a problem with this encryption algorithm. Actually I've got a sample of the AES on the net and trying to understand the logic but I am stuck. Here's the code:



Questions:
What is the key in this program? How can I change the key?


Finally when I try the program, I've got the following results

encrypted string: 91369a3b74c6feede0eff48fcf004f67d26cdd0b4907b44fd2147a4077ded0d1
Original string: AES still rocks!! 414553207374696c6c20726f636b732121


When the message has been decrypted, what does this "414553207374696c6c20726f636b732121" means?

Thanks a lot in advance guys. I would really appreciate if you could help me on this. I just want to understand the logic behind it in order to use it later on. Until now, I have not find a more easier code than this one. If you have one which is easier, please let me now. Again, thanks

Regards,
Mike
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42930
    
  68
What is the key in this program? How can I change the key?

You can pass anything you want as key into the "byte[] raw = skey.getEncoded()" line. Keys are binary, so that's why byte[] is used instead of a String. Any String (containing the password/passphrase) can be turned into a byte[] by calling its "getBytes" method. (You should actually use the "getBytes(String)" method and specify the encoding explicitly, just in case this code runs on different JVMs (or machines) that may have a different default encoding.)

When the message has been decrypted, what does this "414553207374696c6c20726f636b732121" means?

It's a hex representation of the cleartext. Note that the println statement outputs that in addition to the cleartext.
Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
Thanks a lot for the answer Ulf.

I am still having a problem for the key. I've replaced this




by



and I've got

at com.sun.crypto.provider.SunJCE_c.a(DashoA13*..)
at com.sun.crypto.provider.SunJCE_g.a(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.a(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.a(DashoA13*..)
at com.sun.crypto.provider.AESCipher.engineInit(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at AES.main(AES.java:47)

That's why I am confused, I've tried using a key of the same size, like "MAN" but I am still getting the same problem. By the way, is this program ready doing the AES encryption, I am having doubt on it as if it is doing something very simple. I am new to the built in crypto package in Java, that's why I am asking stupid questions.

Many thanks in advance.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42930
    
  68
"AES" is not the key, it's the algorithm.

As I said, the key is passed in the "raw" array.

And yes, the code does AES encryption - that's what the article that contains it talks about. (Just you try to retrieve the original message from "91369a3b74c6feede0eff48fcf004f67d26cdd0b4907b44fd2147a4077ded0d1" without the key, and you'll find that it's not at all easy :-)
Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
Hello dude,
Thanks again for the answer. But I've tried to change




But it's not working.

Thanks.
Regards,
Mike
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42930
    
  68
What does the getBytes method do? What does "it's not working" mean?
Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
Hello,
I am having the following error:

java:37: cannot find symbol
symbol : method getBytes(java.lang.String)
location: class AES


Thanks a lot for your time.
Regards,
Mike
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Mikael,

as Ulf said you have to call the getBytes() method on a String (which represents your key). So you don't call

but instead


getBytes() is simply a method of class String, so you have to call it on a String object! This is why you get the "cannott find symbol" error from the compiler

Marco
Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
Hello Marco,
Thanks a lot for the reply, it works, ;). But I am having a different problem now. When I run the code, I am getting this error:

Exception in thread "main" java.security.InvalidKeyException: Invalid AES key length: 6 bytes
at com.sun.crypto.provider.AESCipher.engineGetKeySize(DashoA13*..)
at javax.crypto.Cipher.b(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at AES.main(AES.java:48)

The problem is surely with the length of the key, I have tried different length but I am getting the same, could you please tell me the length of the key please, I am new to cryptography, I have made some research but still can't figure out the problem. Thanks a lot for your answers.

Regards,
Mike.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42930
    
  68
AES supports 128, 192 and 256 bit keys, so the number of bytes needs to be 16, 24, or 32. Note that the latter two may not be available in all circumstances (as the comment in the "kgen.init(128)" line mentions).
Mikael Fox
Greenhorn

Joined: Mar 22, 2009
Posts: 23
Thanks for the reply but how can I solve this problem. I want to try something but don't know what to change, please just give me a hint.

Thanks a lot in advance guys.

Regards,
Mike
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Mikael,

I don't know what's the problem here. There are technical reasons why some encryption algorithms (not only AES) only support specific length(s) for keys. So if you want to use AES you will have to provide keys with the right size. That's no big change, you just have to make sure that a given key has the correct number of bits/bytes. Where do you have problems with that restriction?

Marco
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42930
    
  68
Whatever you pass into the "raw" array needs to be exactly 16 bytes long for using AES-128. Something like this (but less predictable :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AES Encryption