With RC2 encryption, we use a 128-bit key and a 64-bit IV value. The two modes supported are ECB (without salt) and CBC (with salt). The block size is 64 bits (eight bytes), and thus the output cipher will be in multiples of eight bytes. 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.
RC2 CBC/ECB Encryption with PowerShell |
Coding
The following is the coding:
$plaintext="Hello" $key="00010203040506070001020304050607" $iv="0001020304050607" $plaintext=$Args[0] $key=$Args[1] $iv=$Args[2] $mode=$Args[3] $rc2=[System.Security.Cryptography.RC2]::Create() if ($mode -eq "CBC") { $rc2.Mode = [System.Security.Cryptography.CipherMode]::CBC } elseif ($mode -eq "ECB") {$rc2.Mode = [System.Security.Cryptography.CipherMode]::ECB} $rc2.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7 $rc2.BlockSize = 64 $rc2.IV=[System.Convert]::FromHexString($iv) $rc2.Key = [System.Convert]::FromHexString($key) $bytes = [System.Text.Encoding]::UTF8.GetBytes($plaintext) $encryptor = $rc2.CreateEncryptor() $encryptedData = $encryptor.TransformFinalBlock($bytes, 0, $bytes.Length); $decryptor = $rc2.CreateDecryptor() $pl = $decryptor.TransformFinalBlock($encryptedData, 0, $encryptedData.Length); "== RC2 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:
== RC2 Encryption == Message: qwerty Key: 11210203040506070001020304050607 IV: 0001020304050607 Mode: CBC Encrypted Data: 4FD12F64A64AB15B Decrypted Data: qwerty