Getting a random number in bash

  • Post author:
  • Post category:Bashrand

This script generates a bounded random number: #!/bin/bash # Generate a random number. Copyright (c) Michael Still 2002 # Released under the terms of the GNU GPL # # (Is it possible to copyright a single line of code?) # To quote from the rand manpage as to why we bound the random number this way: # # In Numerical Recipes in C: The Art of Scientific Computing # (William H. Press, Brian P. Flannery, Saul A. Teukolsky, # William T. Vetterling; New York: Cambridge University # Press, 1992 (2nd ed., p. 277)), the following comments are # made: # "If you want to generate a random integer between 1 # and 10, you should always do it by using high-order # bits, as in # # j=1+(int) (10.0*rand()/(RAND_MAX+1.0)); # # and never by anything resembling # # j=1+(rand() % 10); # # (which uses lower-order bits)." # To seed the random number generator, set RANDOM to a value... We can see # that the bash code (2.05a in this case) already does some seeding for us... # # brand () # { # rseed = rseed * 1103515245 + 12345; # return ((unsigned int)((rseed >> 16) & 32767)); /*…

Continue ReadingGetting a random number in bash

Example of DES encryption code

  • Post author:
  • Post category:Des

/****************************************************************************** desex.c, an example of how to use the openssl DES implementation... Data is input on stdin, and the enrypted information is output to stdout. Prompts appear on stderr... This is an example of the Electronic Code Book mode, which is predictable for known input, and is not recommended. Copyright (c) Michael Still 2001 Released under the terms of the GNU GPL ******************************************************************************/ #include #include int main(int argc, char *argv[]){ des_cblock key, input, output; des_key_schedule sched; int c, i; fprintf(stderr, "Setting up the DES library...\n"); des_string_to_key("Mary had a little lamb, it's fleece as white as snow. Everywhere that Mary went, the lamb would surely go...", &key); fprintf(stderr, "Schedualling the key...\n"); switch(des_set_key_checked(&key, sched)){ case -1: fprintf(stderr, "Bad parity\n"); _exit(42); break; case -2: fprintf(stderr, "Key is weak\n"); _exit(42); break; } fprintf(stderr, "Start typing and hit ctrl-d to finish...\n"); i = 0; while((c = fgetc(stdin)) != EOF){ input[i] = c; i++; if(i == 8){ des_ecb_encrypt(&input, &output, sched, DES_ENCRYPT); fprintf(stdout, "%c%c%c%c%c%c%c%c", output[0], output[1], output[2], output[3], output[4], output[5], output[6], output[7]); i = 0; } } }

Continue ReadingExample of DES encryption code

Example of DES decryption code

  • Post author:
  • Post category:Des

/****************************************************************************** desex.c, an example of how to use the openssl DES implementation... Data is input on stdin, and the enrypted information is output to stdout. Prompts appear on stderr... This is an example of the Electronic Code Book mode, which is predictable for known input, and is not recommended. Copyright (c) Michael Still 2001 Released under the terms of the GNU GPL ******************************************************************************/ #include #include int main(int argc, char *argv[]){ des_cblock key, input, output; des_key_schedule sched; int c, i; fprintf(stderr, "Setting up the DES library...\n"); des_string_to_key("Mary had a little lamb, it's fleece as white as snow. Everywhere that Mary went, the lamb would surely go...", &key); fprintf(stderr, "Schedualling the key...\n"); switch(des_set_key_checked(&key, sched)){ case -1: fprintf(stderr, "Bad parity\n"); _exit(42); break; case -2: fprintf(stderr, "Key is weak\n"); _exit(42); break; } fprintf(stderr, "Start typing and hit ctrl-d to finish...\n"); i = 0; while((c = fgetc(stdin)) != EOF){ input[i] = c; i++; if(i == 8){ des_ecb_encrypt(&input, &output, sched, DES_DECRYPT); fprintf(stdout, "%c%c%c%c%c%c%c%c", output[0], output[1], output[2], output[3], output[4], output[5], output[6], output[7]); i = 0; } } }

Continue ReadingExample of DES decryption code

DES examples

  • Post author:
  • Post category:Des

This page contains some DES example code I wrote in reaction to a crypto tutorial I attended a while ago. I finally got around to putting the code online... The basic point of the code is it shows you how to use the DES calls within openssl. It also shows how the electronic code book for of DES is a bad choice for vaguely predictable data such as images. A visual demonstration of this is these two images. The one on the right is an encrypted version of the one on the left... The vertical flip of the image is because of the way libtiff reads colour images, and to be honest isn't worth fixing for this example.    

Continue ReadingDES examples

My AOSS III Talk

  • Post author:
  • Post category:Pngtools

The slides for my Australian Open Source Symposium talk are now available online. This talk covers Panda, PandaLex, PandaScript, PNGTools, and stuff like that. A historical note from November 2020: these presentations are no longer useful due to their age and are no longer online due to hosting changes.

Continue ReadingMy AOSS III Talk

Initial public release

  • Post author:
  • Post category:Pngtools

In line with the maxim "release early, release often", here is the initial public viewing of my work on pngtools. At the moment, the only command that is implemented is pnginfo, which is pretty much there (apart from the command line options, which are rarely used). I would welcome comments on the code, which can be sent to mikal@stillhq.com. The code is (c) Michael Still 2001, and is released under the GNU GPL version 2. Source A historical note from November 2020: this code is quite old, but still actively used. I have therefore converted the old subversion repository to git and it is hosted at https://github.com/mikalstill/pngtools. I will monitor there for issues and patches and try my best to remember what I was thinking 20 years ago...

Continue ReadingInitial public release

End of content

No more pages to load