With DES encryption, we use a 64-bit key and a 64-bit IV value. The two modes supported are ECB (without salt) and CBC (with salt). In this case we will use PKCS7 padding, and which fills the input data with a value that is equal to the number of padding bytes.
DES CBC/ECB Encryption with PowerShell |
Coding
The following is the coding:
$plaintext="Hello" $key="0001020304050607" $iv="0001020304050607" $plaintext=$Args[0] $key=$Args[1] $iv=$Args[2] $mode=$Args[3] $des=[System.Security.Cryptography.Des]::Create() if ($mode -eq "CBC") { $des.Mode = [System.Security.Cryptography.CipherMode]::CBC } elseif ($mode -eq "ECB") {$des.Mode = [System.Security.Cryptography.CipherMode]::ECB} $des.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7 $des.BlockSize = 64 $des.IV=[System.Convert]::FromHexString($iv) $des.Key = [System.Convert]::FromHexString($key) $bytes = [System.Text.Encoding]::UTF8.GetBytes($plaintext) $encryptor = $des.CreateEncryptor() $encryptedData = $encryptor.TransformFinalBlock($bytes, 0, $bytes.Length); $decryptor = $des.CreateDecryptor() $pl = $decryptor.TransformFinalBlock($encryptedData, 0, $encryptedData.Length); "== DES Encryption == " "Message: " + $plaintext "Key: " + $key "IV: " + $iv "Mode: " + $mode "Encrypted Data: " + [System.Convert]::ToHexString($encryptedData) "Decrypted Data: " + [System.Text.Encoding]::ASCII.GetString($pl)
A sample run shows:
== DES Encryption == Message: qwerty Key: 0001020304050607 IV: 0001020304050607 Mode: ECB Encrypted Data: 2744EF778F3AE100 Decrypted Data: qwerty