From: tad.bochan@... Date: 2004-09-10T23:37:33+09:00 Subject: Réf. : ASCII, RSA AND BIGNUM Hi Barry, Being aruby-nuby myself, and having next to no experience of C/C++, Perl,Python or Unix or Windows programming, I found it difficult to grasp the strategy of your code. Please don't take this as criticism! If I were doing this, I would start with some way of taking an ascii message, splitting it into a series of bitstrings, and use each bitstring as a number. The size of each bitstring would be a function of the size of the primepair product used in the RSA algorithm. Each number would be encrypted using the RSA algorithm, and then the array of encrypted numbers would be re-assembled as a string. I'm sure your code follows this approach. The difficult bit is getting Ruby to cast a String into a Bignum and back again. That is, given the string "ABCDEFGH" , return 0x4142434445464648 and given 0x4142434445464648, return "ABCDEFGH". It's a shame that there is no standard method for doing that, and I'm sure Matz could add the methods quicker than you could blink. Anyway, Here's my version of this process, (hope it's useful, and if I'm on the wrong track, any comments would be appreciated) # CONVERT A NUMBER INTO ITS EQUIVALENT BIT-STRING def n2s(num,width) s = "x" * width (width-1).downto(0) { |k| s[k] = (num & 0xff); num=num>>8} return s end # CONVERT AN ARRAY OF NUMBERS INTO THE EQUIVALENT STRING OF BITS def a2s(a,width) (a.collect {|x| n2s(x,width)}).join end # SPLIT STRG INTO SEGMENTS OF N-BYTES EACH, AND CONVERT EACH TO A NUMBER def s2a(strg,width) a=strg.unpack("a#{width}" * ((strg.size + width - 1)/width)) a.collect do |s| big=0 s.each_byte {|k| big = ((big << 8) | k)} big = big << ((width-s.length)*8) end end def encrypt(n,primepair) # the clever stuff n end # Example :- prime1,prime2=104729,104743 primepair=prime1 * prime2 blksize=primepair.size # bytes per msg block puts msgoriginal="RSA encryption in Ruby" msgarray=s2a(msgoriginal,blksize) puts msgarray.inspect msgarray.collect! {|m| encrypt(m,primepair)} msgoriginal=a2s(msgarray,blksize) puts msgoriginal This message and any attachments (the "message") is intended solely for the addressees and is confidential. If you receive this message in error, please delete it and immediately notify the sender. Any use not in accord with its purpose, any dissemination or disclosure, either whole or partial, is prohibited except formal approval. The internet can not guarantee the integrity of this message. BNP PARIBAS (and its subsidiaries) shall (will) not therefore be liable for the message if modified. --------------------------------------------- Ce message et toutes les pieces jointes (ci-apres le "message") sont etablis a l'intention exclusive de ses destinataires et sont confidentiels. Si vous recevez ce message par erreur, merci de le detruire et d'en avertir immediatement l'expediteur. Toute utilisation de ce message non conforme a sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. L'internet ne permettant pas d'assurer l'integrite de ce message, BNP PARIBAS (et ses filiales) decline(nt) toute responsabilite au titre de ce message, dans l'hypothese ou il aurait ete modifie.