using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Security.Cryptography;
namespace
SHA1Crypt
{
[System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get,
"Encrypted"
)]
public
class
Encrypt : System.Management.Automation.PSCmdlet
{
[System.Management.Automation.Parameter(Position = 0, Mandatory =
true
, HelpMessage =
"String to be encrypted"
)]
public
string
PlainString;
[System.Management.Automation.Parameter(Position = 1, Mandatory =
true
, HelpMessage =
"Pass Key should be the same as used to encrypt if decrypting"
)]
public
string
passKey;
protected
override
void
ProcessRecord()
{
byte
[] Results;
System.Text.UTF8Encoding UTF8 =
new
System.Text.UTF8Encoding();
TripleDESCryptoServiceProvider TDESAlgorithm =
new
TripleDESCryptoServiceProvider();
SHA1CryptoServiceProvider HashProvider =
new
SHA1CryptoServiceProvider();
byte
[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passKey));
byte
[] key =
new
byte
[24];
TDESKey.CopyTo(key, 0);
TDESAlgorithm.Key = key;
TDESAlgorithm.Mode = CipherMode.ECB;
TDESAlgorithm.Padding = PaddingMode.PKCS7;
byte
[] DataToEncrypt = UTF8.GetBytes(PlainString);
try
{
ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
}
finally
{
TDESAlgorithm.Clear();
HashProvider.Clear();
}
this
.WriteObject(Convert.ToBase64String(Results),
true
);
}
}
[System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get,
"Decrypted"
)]
public
class
Decrypt : System.Management.Automation.PSCmdlet
{
[System.Management.Automation.Parameter(Position = 0, Mandatory =
true
, HelpMessage =
"Encrypted string to be decrypted"
)]
public
string
EncryptedString;
[System.Management.Automation.Parameter(Position = 1, Mandatory =
true
, HelpMessage =
"Pass Key should be the same as used to encrypt if decrypting"
)]
public
string
passKey;
protected
override
void
ProcessRecord()
{
byte
[] Results;
System.Text.UTF8Encoding UTF8 =
new
System.Text.UTF8Encoding();
TripleDESCryptoServiceProvider TDESAlgorithm =
new
TripleDESCryptoServiceProvider();
SHA1CryptoServiceProvider HashProvider =
new
SHA1CryptoServiceProvider();
byte
[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passKey));
byte
[] key =
new
byte
[24];
TDESKey.CopyTo(key, 0);
TDESAlgorithm.Key = key;
TDESAlgorithm.Mode = CipherMode.ECB;
TDESAlgorithm.Padding = PaddingMode.PKCS7;
byte
[] DataToDecrypt = Convert.FromBase64String(EncryptedString);
try
{
ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
}
finally
{
TDESAlgorithm.Clear();
HashProvider.Clear();
}
this
.WriteObject(UTF8.GetString(Results),
true
);
}
}
}