From: David x Callaway Date: 2007-03-26T20:44:02+09:00 Subject: Re: Ruby and Cryptography thanks, that was very illuminating, see comments below. ... snip ... >> 1) is pkcs5_keyivgen() the way to go rather than hashing (password >> string + salt) and then using that as the key with >> OpenSSL::Cipher::Cipher as EzCrypto does? > > Maybe. In most cases the pkcs5_keyivgen method is not pkcs5 compliant. I'm running ruby 1.8.5 and comments pertain to that source. pkcs5_keyivgen eventually calls down to EVP_BytesToKey, passing the passphrase, salt (must be exactly 8 bytes if given), iteration count (defaults to 2048) and digest; it returns the key and IV. the man page for EVP_BytesToKey says: If the total key and IV length is less than the digest length and MD5 is used then the derivation algorithm is compatible with PKCS#5 v1.5 otherwise a non standard extension is used to derive the extra data. Newer applications should use more standard algorithms such as PKCS#5 v2.0 for key derivation. however there is no suggestion about how to get v2.0 output. > You must be using MD2, MD5, or SHA1 with RC2 or DES. Using any other > cipher (like AES) will generate your key/iv in an OpenSSL specific > format. > > You probably don't want to use your own key generation method since you > are fairly likely to make design mistakes. I definitely *don't* want to do this, hence I was trying to figure out how to match "enc". > > Ideally you would use PKCS5 v2. Unfortunately the ruby OpenSSL module > doesn't have a hook into any of the PKCS5 v2 password generating > functions. right, see comment above. > Below is a pure ruby PKCS5 v2 password -> key method. thanks, but I don't want to get ahead of the ruby openssl library. of course this *does* raise the question of how it will eventually be incorporated. it wouldn't be a nice thing if the addition caused all my encrypted files to be inaccessible. ... snip ... > > If you are designing your own encrypted storage format there are a > number of things you should do (this is not an exhaustive list of > everything you should do, just a few pointers). right, but I don't want to do this at all ;-). ... snip ... > > Test, test, test, test and test. > > There's nothing worse than finding out that you used the wrong > variable for the key and now everything is encrypted with the same > key. Or finding out later than you didn't store the key properly > and can't retrieve anything. right, that's precisely why I was attempting to test against openssl enc. ... snip ... > > Here's a quick and dirty bit of code to encrypt/decrypt the openssl > program output. the key in the code you provided, besides the obvious biz with the header, is the iteration count is 1, something I wouldn't have guessed. inspecting the openssl src for "enc" shows the same (I should have gone there first). the other thing I didn't figure out by messing around with "enc" is if you don't provide it a salt, and if you don't specify -nosalt, then it will do as you did, provide a random salt string when encrypting. I understand the idea of salt, but specifying it on the command line each time didn't make sense to me, and now I see that's because it doesn't make sense ;-). ... snip ... -- Posted via http://www.ruby-forum.com/.