How to resolve the algorithm Vigenère cipher step by step in the Icon and Unicon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Vigenère cipher step by step in the Icon and Unicon programming language

Table of Contents

Problem Statement

Implement a   Vigenère cypher,   both encryption and decryption. The program should handle keys and text of unequal length, and should capitalize everything and discard non-alphabetic characters. (If your program handles non-alphabetic characters in another way, make a note of it.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Vigenère cipher step by step in the Icon and Unicon programming language

Source code in the icon programming language

procedure main()
   ptext := "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
   write("Key        = ",ekey := "VIGENERECIPHER")
   write("Plain Text = ",ptext)
   write("Normalized = ",GFormat(ptext := NormalizeText(ptext)))
   write("Enciphered = ",GFormat(ctext := Vignere("e",ekey,ptext)))
   write("Deciphered = ",GFormat(ptext := Vignere("d",ekey,ctext)))
end

procedure Vignere(mode,ekey,ptext,alpha)   #: Vignere cipher
   /alpha := &ucase                                              # default 
   if *alpha ~= *cset(alpha) then runerr(205,alpha)              # no dups
   alpha ||:= alpha                                              # unobstructed

   every ctext:="" & p:=ptext[i := 1 to *ptext] & k:=ekey[(i-1)%*ekey+1] do 
      case mode of {
         "e"|"encrypt": 
            ctext||:=map(p,alpha[1+:*alpha/2],alpha[find(k,alpha)+:(*alpha/2)])
         "d"|"decrypt": 
            ctext||:=map(p,alpha[find(k,alpha)+:(*alpha/2)],alpha[1+:*alpha/2])
         default: runerr(205,mode)
      }
return ctext
end


link strings 

procedure NormalizeText(ptext,alpha)       #: text/case classical crypto helper 
   /alpha := &ucase                                              # default 
   if &lcase === (alpha := cset(alpha)) then ptext := map(ptext) # lower
   if &ucase === alpha  then ptext := map(ptext,&lcase,&ucase)   # upper
   return deletec(ptext,&cset--alpha)                            # only alphas
end

procedure GFormat(text)                    #: 5 letter group formatting helper
   text ?  (s := "", until pos(0) do s ||:= " " || move(5)|tab(0))
   return s[2:0]
end


  

You may also check:How to resolve the algorithm Langton's ant step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Empty program step by step in the PostScript programming language
You may also check:How to resolve the algorithm Literals/String step by step in the Oz programming language
You may also check:How to resolve the algorithm Cullen and Woodall numbers step by step in the Perl programming language
You may also check:How to resolve the algorithm Kernighans large earthquake problem step by step in the Arturo programming language