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.