11 October 2012

encrypt/decrypt with openssl API -2

The symmetric cipher commands allow data to be encrypted or decrypted using various block and stream ciphers using keys based on passwords.

int AES_set_encrypt_key(const uchar *userKey, const int bits, 
    AES_KEY *key);

void AES_cbc_encrypt(const uchar *in, uchar *out, const ulong length, 
    const AES_KEY *key, uchar *ivec, const int enc);
# ./encrypt password1
cb3d7f690989f7237fd4485a582ac5b2fe98be8ebd6e37c50bb4bdd734a95631
# ./decrypt cb3d7f690989f7237fd4485a582ac5b2fe98be8ebd6e37c50bb4bdd734a95631
password1
download encrypt.c from here
download decrypt.c from here
# echo -n "password1" > plain.txt
# openssl enc -aes-256-cbc -nosalt -k "1234567890abcdef" -iv 0 -in plain.txt -out enc.txt
# hexdump -C enc.txt 
00000000  aa 3b 81 85 64 bf 6d b9  35 de 59 c3 36 41 0e f2  |.;..d.m.5.Y.6A..|
# openssl enc -d -aes-256-cbc -nosalt -k "1234567890abcdef" -iv 0 -in enc.txt
password1


Reference:
http://saju.net.in/blog/?p=36
http://stackoverflow.com/questions/9889492/how-to-do-encryption-using-aes-in-openssl
http://www.128bitstudios.com/2010/05/31/file-encryption-with-openssl/

07 October 2012

Single precision floating point variable

Single-precision floating-point format is a computer number format that occupies 4 bytes (32 bits) in computer memory, known as float in C.

IEEE 754 is a technical standard for floating-point computation.


Converting from decimal to single-precision binary:
26.25

.25 x 2 = 0.50 -> 0
.50 x 2 = 1.00 -> 1

26.25 = 1A.25 = 0001 1010 . 01 = 1.101001 x 2^4

0 (127+4) 101001 = 0 (131) 101001
0 10000011 101001 = 0100 0001 1101 0010 = 0x41D20000

Converting from single-precision binary to decimal:
471BCF90 = 0100 0111 0001 1011 1100 1111 1001 0000

0 10001110 00110111100111110010000 = 0 (142-127) XYZ

1 . 00110111100111110010000 x 2^15 = 1001 1011 1100 1111 . 1001

1x2^-1 + 1x2^-4 =  .5 + .0625 = .5625

9BCF . 1001 = 39887 . 5625 = 39887.5625

Reference:
http://en.wikipedia.org/wiki/Single_precision
http://sandbox.mc.edu/~bennet/cs110/flt/dtof.html
http://www.exploringbinary.com/pi-and-e-in-binary/

05 October 2012

A script to check PNR status

# cat << EOF > pnrdata
lccp_pnrno1=MYPNRNO
EOF

# cat pnrdata
lccp_pnrno1=MYPNRNO

# url=http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi
# lynx -post_data $url < ./pnrdata | 
  grep -A32 PNR | grep "Passenger [0-9]"
   Passenger 1 W/L 114,GNWL W/L 21

wget --user-agent="Myscript 1.0;" \
--header="Content-Type: application/x-www-form-urlencoded" \
--post-data="lccp_pnrno1=MYPNRNO" \
"http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi" -O- 2>/dev/null \
| grep -A2 "Passenger 1"
<TD class="table_border_both"><B>Passenger 1</B></TD>
<TD class="table_border_both"><B>W/L  114,GNWL  </B></TD>
<TD class="table_border_both"><B>W/L   21</B></TD>


Reference:
http://wiki.linux-delhi.org/cgi-bin/twiki/view/Main/LinuxTricks
http://akhileshss.blogspot.com/2008/09/script-to-periodically-check-pnr-status.html