VigenereCipher.Psm1

Function Lock-Vigenere ($PlainText, $key)
    {
     <#
.SYNOPSIS
'Encrypts' data using Vigenere Cipher
.DESCRIPTION
 Simple Vigenere cipher (https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) encryption.
 
.PARAMETER Plaintext
    The plaintext to encrypt. All spaces are elimated and then the plaintext is converted to uppercase
 
.PARAMETER Key
    The key to encrypt the plaintext
 
.Example
    New-Vigenere -PlainText "This is a test" -Key "abc"
 
.INPUTS
No inputs required
 
.OUTPUTS
Returns ciphertext
 
.NOTES
Author: Tim Jardim
Date: 21.03.18
 
Version:1.001.002
 
#>
######################################################################
        
    # Create our arrays to hold bytes for Ciphertext and Key
      [Byte[]]$PlainTextBytes=@();[Byte[]]$CipherTextBytes=@();[Byte[]]$KeyBytes=@()
    # Get data
    # Remove any spaces in PlainText
      $PlainText=$PlainText.Replace(" ","")
    # Convert PlainText to upper
      $PlainText=$PlainText.ToUpper()
    # Remove any spaces from key
      $Key=$Key.Replace(" ","")
    # Convert key to upper
      $Key=$Key.ToUpper()
    # Convert PlainText to bytes
      $ConvertToASCII = [System.Text.Encoding]::ASCII
      $ConvertToBytes = [system.Text.Encoding]::UTF8
 
      $PlainTextBytes = $ConvertToBytes.GetBytes($Plaintext)
      $KeyBytes = $ConvertToBytes.GetBytes($Key)
 
     $KeyCount=0
    # Cycle through plaintext to convert to ciphertext
     For ($I=0;$I -lt ($PlainTextBytes.Count);$I++)
        {
    # Strip out non alphabetic charcters allow only ASCII 65-90 (A-Z)
      
         If ($PlainTextBytes[$I] -gt 65 -or $PlainTextBytes -lt 90)
            {
            # Take byte value of plaintext and add with byte value of key. Last step is to take result and mod 26. The number 65 is used to offset ASCII value. A is ASCII value 65
              $CipherTextBytes += ((($PlainTextBytes[$I] -65) + ($KeyBytes[$KeyCount] -65)) % 26) + 65
            # Get next character in key
              $KeyCount ++
            # Check whether or not key needs to be reset
              If ($KeyCount -gt ($Key.Length)-1) {$KeyCount=0}
        }
     }
    # Convert CiperTextBytes to ASCII
      $CipherText=$ConvertToASCII.GetString($CipherTextBytes)
    # Display reults
      Return $CipherText
    }
 
 
Function Unlock-Vigenere ($Ciphertext,$Key)
    {
   <#
.SYNOPSIS
'Decrypts' data using Vigenere Cipher
.DESCRIPTION
 Simple Vigenere cipher (https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) encryption.
 
.PARAMETER Ciphertext
    The ciphertext to decrypt. All spaces are elimated and then the ciphertext is converted to uppercase
 
.PARAMETER Key
    The key to decrypt the plaintext
 
.Example
    New-Vigenere -CipherText "tiksjubvvaugsu" -Key "abc"
 
.INPUTS
No inputs required
 
.OUTPUTS
returns Plaintext
 
.NOTES
Author: Tim Jardim
Date: 21.03.18
 
Version:1.001.002
 
#>
######################################################################
        
    # Check to make sure that Ciphertext and key length is not 0
      If (($CipherText.Length) -eq 0 -or ($Key.Length) -eq 0)
        {
         Return $false
        }
    # Create our arrays to hold bytes for Ciphertext and Key
      [Byte[]]$PlainTextBytes=@();[Byte[]]$CipherTextBytes=@();[Byte[]]$KeyBytes=@()
    # Remove any spaces from ciphertext
      $CipherText=$CipherText.Replace(" ","")
    # Convert ciphertext to upper
      $CipherText=$CipherText.ToUpper()
    # Remove any spaces from key
      $Key=$Key.Replace(" ","")
    # Convert key to upper
      $Key=$Key.ToUpper()
    # Convert PlainText to bytes
      $ConvertToASCII = [System.Text.Encoding]::ASCII
      $ConvertToBytes = [system.Text.Encoding]::UTF8
 
      $CipherTextBytes = $ConvertToBytes.GetBytes($Ciphertext)
      $KeyBytes = $ConvertToBytes.GetBytes($Key)
 
      $KeyCount=0
 
    # Cycle through plaintext to convert to ciphertext
      For ($I=0;$I -lt ($CipherTextBytes.Count);$I++)
        {
    # Strip out non alphabetic charcters allow only ASCII 65-90 (A-Z)
         If ($CipherTextBytes[$I] -gt 65 -or $CipherTextBytes -lt 90)
            {
            # Pause
            # Take byte value of plaintext and add with byte value of key. Last step is to take result (add 26 incase result is less than 65) and mod 26. The number 65 is used to offset ASCII value. A is ASCII value 65
              $PlainTextBytes += (((($CipherTextBytes[$I] -65) - ($KeyBytes[$KeyCount] -65))+26) % 26) + 65
            # Get next character in key
              $KeyCount ++
            # Check whether or not key needs to be reset
              If ($KeyCount -gt ($Key.Length)-1) {$KeyCount=0}
        }
     }
 
    # Convert CiperTextBytes to ASCII
      $PlainText=$ConvertToASCII.GetString($PlainTextBytes)
    # Display reults
      Return $PlainText
    }