Powershell Simple Substitution Cipher

Powershell Simple Substitution Cipher

Another assignment from my Cryptography course in my undergrad was to develop our own Powershell simple substitution cipher programmatically. This code is merely a framework to provide any type of substitution key you want. I wanted to do a custom cipher to allow a bit more security (though any security expert knows that substitution ciphers are highly crackable)… though, it is still better then clear-text at least…

An example ofย  a very popular substitution cipher is shown below:

Powershell Simple Substitution Cipher - Ceaser

This cipher is called a Ceaser Cipher (ROT13) because it will rotate each letter by 13 characters of the alphabet. It was used by Julius Ceaser to send highly sensitive information during roman times. The basic concept is still the same today and the whole idea behind this type of cipher, is that each letter in the alphabet has a direct substitution of another letter from the alphabet. The problem is that the same key is used to both encrypt, and decrypt. This makes this method of encryption very weak. I chose to NOT use the most popular substitution cipher and to create my own custom cipher. Some letters were substituted with another, and others were not, further throwing off a malicious user attempting to decipher your ciphertext.

Take a look at my PowerShell code below.

[sourcecode language=”powershell” wraplines=”false” collapse=”false”]
Function Encrypt-String {
param (
[Parameter(Position=0,Mandatory=$True)]
[string]$Message)

# Declaring the encryption and decryption key (A=A,B=Z,C=Q …etc)
$CipherTextAlpha = “AZQSWXDECFRVGTBHYNJMUKILOP0123456789”
$PlainTextAlpha = “ABCDEFGHIJKLMNOPQRSTUVWXYZ5438279016″
$length = $CipherTextAlpha.Length

# Adding letters to array
$Hash = @{}
for($i=0; $i -lt $length; $i+=1) {
$Hash.add($PlainTextAlpha[$i],$CipherTextAlpha[$i])
}

# Converting to Upper
$Message = $Message.ToUpper()
$CTlength = $Message.Length
$CipherText=””

for($i=0; $i -lt $CTlength; $i+=1) {
$char = $Message[$i]
$CipherText+=$Hash[$char]
}
Write-host -ForegroundColor Yellow “`n$CipherText”
}

Function Decrypt-String {
param (
[Parameter(Position=0,Mandatory=$True)]
[string]$CipherText)

# Declaring the encryption and decryption key (A=A,B=Z,C=Q …etc)
$CipherTextAlpha = “AZQSWXDECFRVGTBHYNJMUKILOP0123456789”
$PlainTextAlpha = “ABCDEFGHIJKLMNOPQRSTUVWXYZ5438279016″
$length = $CipherTextAlpha.Length

$Hash = @{}
for($i=0; $i -lt $length; $i+=1) {
$Hash.add($CipherTextAlpha[$i],$PlainTextAlpha[$i])
}

$CipherText = $CipherText.ToUpper()
$CTlength = $CipherText.Length
$PlainText=””

for($i=0; $i -lt $CTlength; $i+=1) {
$char = $CipherText[$i]
$PlainText+=$Hash[$char]
}
Write-host -ForegroundColor Green “`n$PlainText”
}

}
[/sourcecode]

There is much that can be done with this simple script, future revisions could include additional abilities like.

  1. Allow the encryption key to be parameter based, rather than statically coded
  2. Randomization (shuffling) of the alphabet to create a new encryption key every time.
  3. Encoding the source code in base64 to further obfuscate the source

Either way, just a quick script I wrote for the assignment and I got an A. Took about 10 minutes to write.. ๐Ÿ™‚

Enjoy

Tags// ,