Fighting with computers

Computers are not always friendly.

Wednesday, September 28, 2011

A bit more RSA

I really don't like when I do not understand anyone's code, so it takes me a bit of effort till I do my own homework to create my own version of things I can understand. Being a teacher, I always find troubling not to be able to explain things to my students, so I used some time to create my very own RSA implementation. I've found a very good reference text here.

My perl program takes three parameters data, encryption (or decryption) exponent and module and it returns the encrypted (or decrypted) version. As it uses BigInt library it should be happy dealing with any of your big numbers. You can find a Java implementation in the above mentioned text too.

#!/usr/bin/perl 
use Math::BigInt;
if($#ARGV<2) 
     {print "Usage: exp, module\n"; exit 2;}
$a = Math::BigInt->new($ARGV[0]);
$b = Math::BigInt->new($ARGV[1]);
$c = Math::BigInt->new($ARGV[2]);
print "out=".($dec = $a->bmodpow($b,$c))."\n";
Contrary to what may look, bmodpow has been implemented to be computationally efficient using repeated squaring technique.
Example:
I have created an RSA keypair before choosing public exponent to be 3. The numbers seem to come out of thin air but I created them with a software tool mentioned in the previous RSA post. The plaintext 1234567890 (as a number and not as string) was selected as it was easy to type.
$ perl myRSA.pl 1234567890 3 0x102421060aad49ec2521203b28
ffdccc73b2b55b7e4f6e73b5ad31dcc240cdc8883fd220fff0a218d16
ec6cbd8a713aa907bd9ac46fd4f622b639de83aa5f4f752f6953960a
363e7c18d3bbdb7b93ff5c9ee07a2de7ba1fe46545da35d1cb92f926
c691e7415b253ad9047547285bc79e0ad915ac2b2b83722ad3d8fc
001aab7
cyphertext = 1881676371789154860897069000

$ perl myRSA.pl 1881676371789154860897069000 0xac2c0aeb1
c8dbf2c36b6ad21b553ddda27723925434f44d23c8cbe8818089305
ad536c0aaa06c108b9f2f32906f6271b5a7e672d9fe34ec1ced13f02
7194df9e19960c0e9b793efd65c4a68f9cabfa1dbe904d5ade76bf2d
83091c17d687b6d04323abd935676379e5ada2cf6892846586360d9
80c9a2cec1bd7cff55566a6b 0x102421060aad49ec2521203b28ffdc
cc73b2b55b7e4f6e73b5ad31dcc240cdc8883fd220fff0a218d16ec6c
bd8a713aa907bd9ac46fd4f622b639de83aa5f4f752f6953960a363e7
c18d3bbdb7b93ff5c9ee07a2de7ba1fe46545da35d1cb92f926c691e
7415b253ad9047547285bc79e0ad915ac2b2b83722ad3d8fc001aab7
cyphertext = 1234567890

Update: I've found that Crypt::OpenSSL::Bignum is more than a thousand times faster doing the modular exponentiation of RSA. So avoid the BigInt approach if speed matters to you.

Wednesday, September 14, 2011

Playing with RSA

A recent assignment required me to dust off my knowledge of RSA (public-key encryption algorithm). While it is something I do teach every year, I usually do not have to fiddle with the code. So when I was needing a pair of keys (public+private) for some test I needed to look around for some  key-generation code.

Once I've got my pair of keys I was surprised with this implementation of RSA encryption in perl:

#!/bin/perl -sp0777i<X+d*lMLa^*lN%0]dsXx++lMlN/dsM0<j]dsj
$/=unpack('H*',$_);$_=`echo 16dio\U$k"SK$/SM$n\EsN0p[lN*1
lK[d2%Sa2/d0$^Ixp"|dc`;s/\W//g;$_=pack('H*',/((..)*)$/)
However, the fastest way to get you working is using OpenSSL's genrsa command.

Tuesday, September 06, 2011

Remote Desktop is gone

Last year I bought a small notebook to keep me updated of what was available in Windows 7. Being a small computer, using it for a long session and, in particular the mouse pad, is not very pleasant. So while I am not on the go, I rather would prefer to access this computer remotely to use my desktop's bigger screen and more comfortable mouse.

I was shocked when I learned that Microsoft had "streamlined" some versions of Windows 7 so some features were missing for some of them. I later learned that Windows 7 Home Premium does not include the Remote Desktop server that was common in Windows XP.

I occasionally use remote control tools like TeamViewer with great success (or other variants of VNC) but the main problem is they do not work as smoothly as Remote Desktop Protocol (RDP) nor they allow you to resize the desktop area at log-on time.

One colleague of mine mentioned, while I was complaining about the missing Remote Desktop on my notebook, that there was a hack to get back Remote Desktop. I tried it, and it worked like a charm. Not only you can get Remote Desktop service back, but it is possible to get RDP concurrent sessions too.