include xpllib; \For StrLen, StrCopy, Print func StrChar(Str, C); char Str, C; [loop [if Str(0) = 0 then return 0; if Str(0) = C then return Str; Str:= Str+1; ]; ]; def \CMode\ ENCRYPT, DECRYPT; char L_alphabet, R_alphabet; proc Chao(In, Out, Mode, Show_steps); char In, Out, Mode, Show_steps; int Len, I, J, Index; char Store, Left(27), Right(27), Temp(27); [Len:= StrLen(In); StrCopy(Left, L_alphabet); StrCopy(Right, R_alphabet); Temp(26):= 0; for I:= 0 to Len-1 do [if Show_steps then Print("%s %s\n", Left, Right); if Mode = ENCRYPT then [Index:= StrChar(Right, In(I)) - Right; Out(I):= Left(Index); ] else [Index:= StrChar(Left, In(I)) - Left; Out(I):= Right(Index); ]; if I = Len-1 then return; \Permute Left for J:= Index to 26-1 do Temp(J-Index):= Left(J); for J:= 0 to Index-1 do Temp(26-Index+J):= Left(J); Store:= Temp(1); for J:= 2 to 14-1 do Temp(J-1):= Temp(J); Temp(13):= Store; StrCopy(Left, Temp); \Permute Right for J:= Index to 26-1 do Temp(J-Index):= Right(J); for J:= 0 to Index-1 do Temp(26-Index+J):= Right(J); Store:= Temp(0); for J:= 1 to 26-1 do Temp(J-1):= Temp(J); Temp(25):= Store; Store:= Temp(2); for J:= 3 to 14-1 do Temp(J-1):= Temp(J); Temp(13):= Store; StrCopy(Right, Temp); ]; ]; char Plain_text, Cipher_text, Plain_text2; [L_alphabet:= "HXUCZVAMDSLKPEFJRIGTWOBNYQ"; R_alphabet:= "PTLNBQDEOYSFAVZKGJRIHWXUMC"; Plain_text:= "WELLDONEISBETTERTHANWELLSAID"; Cipher_text:= MAlloc(StrLen(Plain_text) + 1); Plain_text2:= MAlloc(StrLen(Plain_text) + 1); Print("The original plaintext is : %s\n", Plain_text); Print("\nThe left and right alphabets after each permutation during encryption are :\n\n"); Chao(Plain_text, Cipher_text, ENCRYPT, true); Print("\nThe ciphertext is : %s\n", Cipher_text); Chao(Cipher_text, Plain_text2, DECRYPT, false); Print("\nThe recovered plaintext is : %s\n", Plain_text2); Release(Cipher_text); Release(Plain_text2); ]