Hello
I find the ruby crypto very confusing. It looks like the methods are
dynamically generated, and carry no rdoc documentation that would
appear on ruby-doc.
I tried to look at the openssl samples supplied with ruby.
But they only show how to use a high level method which
- does not allow specifying ivec
- makes the encrypted string longer than plain string (even for
lenghts like 4K I would consider very well rounded)
So the question is: how to make the equivalent of the following in ruby?
I am not sure how the length is supposed to be rounded and how long
the ivec should be - there is no documentation for aes in the openssl
man pages.
The following program is supposed to read a file from stdin, encrypt
each BUFLEN block with key specified in file testkey and ivec
specified in testiv, and output file of the same length (rounded to
KLEN). Decrypt if an argument is specified.
In ruby I could not specify the ivec, and the blocks would inflate slightly.
#include <stdio.h>
#include <string.h>
#include <openssl/aes.h>
#include <assert.h>
#define BUFLEN 256
#define KLEN 32
#define KBITS 256
void main(int argc, char** argv) {
AES_KEY ks;
FILE * keyf = fopen("testkey","rb");
FILE * ivf = fopen("testiv","rb");
assert(keyf && ivf);
char buf[BUFLEN];
char key [KLEN];
char iv [KLEN];
char civ [KLEN];
int res;
int len;
int way;
res=fread(key, KLEN, 1, keyf); assert(res);
res=fread(iv, KLEN, 1, ivf); assert(res);
memcpy(civ, iv, KLEN);
if(argc>1){
fprintf(stderr, "DECRYPT");
AES_set_decrypt_key(key, KBITS, &ks);
way=AES_DECRYPT;
}else{
fprintf(stderr, "ENCRYPT");
AES_set_encrypt_key(key, KBITS, &ks);
way=AES_ENCRYPT;
}
while(len = fread(buf, 1, BUFLEN, stdin)){
if(len<BUFLEN && (len%KLEN)) {
res=((len / KLEN) +1)*KLEN;
fprintf(stderr, "%i, %i\n",len,res);
memset(buf+len,0,res-len);
len=res;
}
memcpy(civ, iv, KLEN);
AES_cbc_encrypt(buf,buf,len,&ks,civ,way);
res=fwrite(buf, 1, len, stdout);
assert(res==len);
}
}
Thanks
Michal Suchanek
···
--
Support the freedom of music! Maybe it's a weird genre you would not like
if you heared it .. but weird is *not* illegal.
Maybe next time they will send a special forces commando to your picnic ..
.. because they think you are weird.
http://www.music-versus-guns.org http://en.policejnistat.cz