Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Security / December 2003

Tip: Looking for answers? Try searching our database.

Help Needed!!

Thread view: 
Yuval Mishory - 22 Dec 2003 11:55 GMT
hello,
as part of my current task, I need to follow these instructions:
"encode a file using AES, with a 128 bit key, in CBC mode. 128 bit
initialization vector will prefix the ciphertext. padding according to
RFC 2630. decode the result into a new file in a seperate process."
I must admit that this is the first time I've had to do this, and I
don't have a clue what CBC mode or padding mean, or what an
initialization vector is and its function. in fact, I'm not really
sure how the AES algorithm works.
searching this newsgroup and the net, I found pretty good examples for
encryption and decryption. I have no idea which are relevant to my
task and which are not.
I can't afford to learn the whole theory of cryptology just to
complete this assignment, so I need some help...
anyone?

thanks,
Yuval
Karl Scheibelhofer - 22 Dec 2003 14:24 GMT
first, you should read some introduction to symmetric cryptography.
otherwise it is likely that we make severe mistakes.
in Java, you can do the encryption using the javax.crypto.Cipher class. e.g

byte[] iv = new byte[16]; // iv...initialization vector
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
IvParameterSpec ivSpec = new IvParameterSpec(iv);

byte[] keyBytes = ... // 16 key bytes, either you "know" it or you generate
e.g. using randomly like the IV

SecretKey secretKey = SecretKeySpec(keyBytes, "AES");

Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
aes.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);

byte[] ciphertext = aes.doFinal(plaintextBytes);

then write to file the "iv" (initialization vector) followed by
"ciphertext". however, you should implement this using streams rather then
just one big byte array as shown above.
to do the reverse, simply read the first 16 bytes and take them as IV. the
rest can be decrypted in a similar manner; e.g.

Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
aes.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);

where the key must be the same as at the encryption side (the IV either, but
the IV need not be kept secure).
see also
http://java.sun.com/j2se/1.4.2/docs/guide/security/jce/JCERefGuide.html#SimpleEncrEx.

i hope this helps for starting.
note that old Java version do not support AES per default. thus you may use
SUN Java 1.4.2, which comes with AES. if you have to support older versions
of Java, you may use a JCE provider; e.g. IAIK-JCE
(http://jce.iaik.tugraz.at/products/01_jce/index.php), which is very good
;-).

regards

 Karl

> hello,
> as part of my current task, I need to follow these instructions:
[quoted text clipped - 14 lines]
> thanks,
> Yuval
Yuval Mishory - 24 Dec 2003 10:17 GMT
Thank you kindly, Karl!!! you've really helped me! :-))
I've succeeded in encrypting a text file and decrypting it again
perfectly, creating the key from a 16 character string password.
to test my encryption, I've tried running the OpenSSL command-line
program to try and get the same decryption (using the same password,
obviously). unfortunately it doesn't work... :-(
are you familiar with OpenSSL? is anyone else on the newsgroup? the
OpenSSL documentation is not complete yet, and their newsgroup is not
very helpful either. the command I gave to try and decrypt my file is:

<command_prompt>\openssl\bin\openssl enc -d -aes-128-cbc -in enc.txt
-out openssl_dec.txt -pass pass:<mypassword>

and the error I get is "bad magic number", which raises the question
of 'can I decrypt using openSSL at all?'.

are you sure that PKCS5Padding is RFC2630 compliant? where can I read
more about paddings?

thanks again!
Yuval

> first, you should read some introduction to symmetric cryptography.
> otherwise it is likely that we make severe mistakes.
[quoted text clipped - 58 lines]
> > thanks,
> > Yuval
Michael Amling - 24 Dec 2003 13:05 GMT
> Thank you kindly, Karl!!! you've really helped me! :-))
> I've succeeded in encrypting a text file and decrypting it again
[quoted text clipped - 11 lines]
> and the error I get is "bad magic number", which raises the question
> of 'can I decrypt using openSSL at all?'.

  Are you sure that you converted the password to an AES key by the
same method that OpenSSL uses?

--Mike Amling
Yuval Mishory - 25 Dec 2003 11:07 GMT
actually, I have no idea how openSSL builds the key from the
password... I did it this way (in Java, obviously):

SecretKey key = new SecretKeySpec(password.getBytes(), "AES");

the problem that's bothering me the most right now is whether I'm
using the right encryption... I absolutely MUST use padding according
to RFC 2630, and I'm not sure if PKCS5 or PKCS7 is appropriate (or
something else entirely).

Yuval.

> > Thank you kindly, Karl!!! you've really helped me! :-))
> > I've succeeded in encrypting a text file and decrypting it again
[quoted text clipped - 16 lines]
>
> --Mike Amling
Yuval Mishory - 25 Dec 2003 13:07 GMT
I'm sorry to have bothered you guys...
I searched and I searched, and eventually I started reading the RFCs
for PKCS#5 and PKCS#7 as well as RFC 2630 and found that they all use
the same padding scheme :-)
so I have my answer. but I appreciate the help!
merry christmas to those of you who celebrate it.
Yuval.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.