Thank you for the quick response.
This seems better, but I’m still not getting the desired answer.
I’m using ruby-1.8.1 on Redhat 7.3 system.
Same system used for both Ruby and C versions …
Here are two short example
programs - first in C, giving the correct answer
The C compilation line is
gcc sample1.c -lssl -o sample1
— sample1.c —
#include <openssl/des.h>
void dmp_blk(int l, char *b)
{
int i;
for (i=0; i<l; i++) printf (“%02x “,(b[i] & 0xFF));
printf (”\n”);
}
main(int argc, char *argv)
{
char magic=“KGS!@#$%”;
char key1=“R¢Qk%*Qa”;
des_key_schedule sked;
unsigned char res[9];
/* encrypt magic w/DES Key 1 */
des_set_key_checked((const_des_cblock *)key1, sked);
des_ecb_encrypt((const_des_cblock *) magic,
(const_des_cblock *)res, sked, 1);
printf (“Results of DES encryption\n”);
printf (“Key: %s Plaintext: %s\n”, key1, magic);
dmp_blk(8,res);
}
— end of sample1.c —
Then in ruby - giving a different answer
— sample1.rb —
#! /usr/bin/env ruby
require ‘openssl’
class String
def dmp_blk
self.each_byte {|c| printf(“%02x “,c)}
printf (”\n”)
end
end
magic = ‘KGS!@#$%’
key1 = ‘R¢Qk%*Qa’
des = OpenSSL::Cipher::Cipher.new(“DES-ECB”)
des.key = key1
des.encrypt(magic)
res = des.final
puts “Results of DES encryption”
puts “Key: #{key1} Plaintext: #{magic}”
res.dmp_blk
— end of sample1.rb —
— results from C version —
$ sample1
Results of DES encryption
Key: R¢Qk%*Qa Plaintext: KGS!@#$%
ff 37 50 bc c2 b2 24 12
$
— results from ruby version
$ sample1.rb
Results of DES encryption
Key: R¢Qk%*Qa Plaintext: KGS!@#$%
ff c4 20 c7 c2 f9 74 e3
$
···
On Thu, 2004-04-15 at 02:49, ts wrote:
Here’s the ruby code segment I’m using…
des = OpenSSL::Cipher::Cipher.new(“DES”)
des.encrypt( key1 )
res1 = des.update( magic )
res1 << des.final
Well, probably I’ve not understood but you don’t want this ?
des = OpenSSL::Cipher::Cipher.new(“DES-ECB”)
des.key = key1
des.encrypt(magic)
p des.final
Guy Decoux