From 7b2c03f39b651410566b881707e7be7ee4bbb64f Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 2 Aug 1992 09:00:06 +0000 Subject: New rotor methods: setkey(), encryptmore() and decryptmore(). --- Modules/rotormodule.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 89 insertions(+), 8 deletions(-) diff --git a/Modules/rotormodule.c b/Modules/rotormodule.c index e40615f..e8fec07 100644 --- a/Modules/rotormodule.c +++ b/Modules/rotormodule.c @@ -1,6 +1,4 @@ /*********************************************************** -Copyright 1992 by Lance Ellinghouse (lance@markv.com). - Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. @@ -23,6 +21,19 @@ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ******************************************************************/ +/****************************************************************** +Copyright 1992 by Lance Ellinghouse (lance@markv.com) + +All Rights Reserved + +Permission to use, copy, distribute for any purpose and without fee +is hereby granted, provided that this copyright notice appear in +all copies and that both this copyright notice and this permission +notice appear in supporting documentation. +Permission to make any changes is granted on the basis that all +changes and improvements are also forwarded to Lance Ellinghouse +before distribution. +******************************************************************/ /* This creates an encryption and decryption engine I am calling a rotor due to the original design was a harware rotor with @@ -35,9 +46,19 @@ Rotor Module: Rotor Objects: +- ro.setkey('string') -> None (resets the key as defined in newrotor(). - ro.encrypt('string') -> encrypted string - ro.decrypt('encrypted string') -> unencrypted string +- ro.encryptmore('string') -> encrypted string +- ro.decryptmore('encrypted string') -> unencrypted string + +NOTE: the {en,de}cryptmore() methods use the setup that was + established via the {en,de}crypt calls. They will NOT + re-initalize the rotors unless: 1) They have not been + initalized with {en,de}crypt since the last setkey() call; + 2) {en,de}crypt has not been called for this rotor yet. + NOTE: you MUST use the SAME key in rotor.newrotor() if you wish to decrypt an encrypted string. Also, the encrypted string is NOT 0-127 ASCII. @@ -51,11 +72,14 @@ NOTE: you MUST use the SAME key in rotor.newrotor() #include "modsupport.h" #include #include +#define TRUE 1 +#define FALSE 0 typedef struct { OB_HEAD int seed[3]; short key[5]; + int isinited; int size; int size_mask; int rotors; @@ -79,6 +103,7 @@ rotorobject *r; r->seed[0] = r->key[0]; r->seed[1] = r->key[1]; r->seed[2] = r->key[2]; + r->isinited = FALSE; } /* Return the next random number in the range [0.0 .. 1.0) */ @@ -120,6 +145,7 @@ static short r_rand(r,s) rotorobject *r; short s; { + /*short tmp = (short)((int)(r_random(r) * (float)32768.0) % 32768);*/ short tmp = (short)((short)(r_random(r) * (float)s) % s); return tmp; } @@ -360,6 +386,7 @@ static void RTR_init(r) r->advances[i] = (1+(2*(r_rand(r,r->size/2)))); RTR_permute_rotor(r,&(r->e_rotor[(i*r->size)]),&(r->d_rotor[(i*r->size)])); } + r->isinited = TRUE; } /*(defun RTR-advance () @@ -497,13 +524,15 @@ static unsigned char RTR_d_char(r, c) (let ((fc (following-char))) (insert-char (RTR-e-char fc) 1) (delete-char 1))))))*/ -static void RTR_e_region(r, beg, len) +static void RTR_e_region(r, beg, len, doinit) rotorobject *r; unsigned char *beg; int len; + int doinit; { register int i; - RTR_init(r); + if (doinit || r->isinited == FALSE) + RTR_init(r); for (i=0;iisinited == FALSE) + RTR_init(r); for (i=0;i