Friday, January 22, 2016

Trying to understand the RSA algorithm with a Perl script

I tried to understand the RSA encryption algorithm and wrote two Perl scripts. Maybe someone else finds this useful, so I share it.

The first script (generate_keys.pl) creates the public and the private key. It takes two prime numbers as arguments, in this context commonly referred to as p and n:

U:\> generate_keys.pl 643 947 public key e = 65537 n = 608921 private key d = 412697 n = 608921

Now, I can encrypt a message with the second script (encrypt_decrypt.pl) and the public key: the first two parameters are the public key, the third parameter the message to be encrypted.

The message I want to encrypt is the number 42:

U:\> encrypt_decrypt.pl 65537 608921 42 166097

In order to decrypt 166097, I use encrypt_decrypt.pl again, this time with the private key:

U:\> encrypt_decrypt.pl 412697 608921 166097 42

Links

RSA key generation example with python was a very helpful page for me.

Modular multiplicative inverse on wikipedia.

Extended euclidean algorithm on wikipedia.

RSA (cryptosystem) on wikipedia.

The scripts on github.

Wednesday, January 20, 2016

Using birdy to tweet a message

birdy makes it easy to tweet a message:

u:\20-birdy> tweet.py "Hello World!"

Here's the script (tweet.py):

import os import sys from birdy.twitter import UserClient if len(sys.argv) < 2: print "specify text to tweet" sys.exit() tweet_text = sys.argv[1] tw = UserClient(os.environ['TWITTER_CONSUMER_KEY' ], os.environ['TWITTER_CONSUMER_SECRET' ], os.environ['TWITTER_ACCESS_TOKEN' ], os.environ['TWITTER_ACCESS_TOKEN_SECRET']) tw.api.statuses.update.post(status = tweet_text)
Experimenting with the twitter API client birdy

tweet.py

birdy

Experimenting with the twitter API client birdy

I stumbled upon the twitter API client birdy whose description reads a super awesome Twitter API client for Python.

Of course, inquiring minds want to know, so I wrote a little script (account_info.py). The script takes one argument: the name of a twitter account. In the following screenshot, I read some account data for the account twitterapi:

For example, the script reports that twitterapi has 5.3 million followers, but follows only 48 other accounts.

The script is also able to read the current status. For a reason I don't understand, the status for twitterapi seems always to be "@TheNiceBot aww thanks, you're lovely too! :-)". The status is correct, however, for other accounts.

Here's the script

import os import sys from birdy.twitter import UserClient if len(sys.argv) < 2: print "specify screen name" sys.exit() screen_name = sys.argv[1] tw = UserClient(os.environ['TWITTER_CONSUMER_KEY' ], os.environ['TWITTER_CONSUMER_SECRET' ], os.environ['TWITTER_ACCESS_TOKEN' ], os.environ['TWITTER_ACCESS_TOKEN_SECRET']) r = tw.api.users.show.get(screen_name=screen_name) profile_url=r.data['profile_background_image_url_https'] # for key, value in r.data.iteritems() : # print key status_id=str(r.data['status']['id_str']) print "" print "Current Status" print " of " + r.data['status']['created_at'] print " url=https://twiter.com/" + screen_name + "/status/" + status_id print "------------------------------------------------------" print r.data['status']['text'] print "--------------" print "" print "Name: " + r.data['name' ] print "Description: " + r.data['description' ] print "Followers: " + str(r.data['followers_count']) print "Following: " + str(r.data['friends_count' ]) print "Tweets: " + str(r.data['statuses_count' ]) print "Language: " + r.data['lang' ]
account_info on github.
Inueni's github repository birdy.

Tuesday, January 19, 2016

Oracle: Turning a select statement into an Excel file

I don't know how many times a CFO has approached me and asked I know, the data for XYZ is somewhere in the database. Could you quickly get me XYZ and send it to me as Excel.

Usually, getting the data out of the database was fairly easy with a SQL select statement, yet, bringing the data into an excel worksheet was sort of a recurring PITA: I would start SQL Developer, execute the select statement, copy the result set (ctrl-c), open Excel, paste the result set (ctrl-v) then I'd adjust the widths of the columns, and only then I'd save the resulting excel sheet.

Not that these steps are too hard, but I always felt that should be easier. So, I have written the procedure xlsx_writer.sql_to_xlsx. This procedure takes an SQL statement and the name of an Excel file to be written, executes the SQL statement and writes the Excel file.

In SQL*Plus, that would look like:

Of course, this can be written in one line, I have used four lines because of the width limit in this blog.

Source code on github

xlsx_writer on my homepage

Writing a blob into a file with PL/SQL with a single line of code.

I have updated my blob_wrapper so that it can write a blob with a single line:

blob_wrapper.to_file('c:\temp\abc.txt', my_blob);

This creates the file c:\temp\abc.txt and fills it with the content of my_blob

A varchar2 can be converted into a blob and then written into the file like so

begin blob_wrapper.to_file( 'c:\temp\two_params.txt', utl_raw.cast_to_raw('foo bar baz') ); end; /

Programming PL/SQL almost makes fun again.

Wednesday, January 13, 2016

Creating Excel (.xlsx) files with Oracle PL/SQL

The XLSX Excel file format is actually a zipped archive of some xml files with the suffix .xlsx rather than .zip This makes it possible to create XLSX files with (almost) any programming language that can create files and zip them. Since I am working with PL/SQL every now and then and I needed to create xlsx files for a reporting solution, I crated a small library: XLSX writer for Oracle. The source code for this library is on this github repository, some examples are on my homepage.

Friday, July 17, 2015

Using Apophysis to create background images

With Apophysis, it is possible to create wallpapers or background images such as the following image:

I have produced this image with a variation of this script. Unfortunatly, I lost the script that created the picture above, but this script sort of comes close.