SymmetricAlgorithm.CreateEncryptor Method
Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)
If the current Key property is null, the GenerateKey method is called to create a new random Key. If the current IV property is null, the GenerateIV method is called to create a new random IV.
Use the CreateDecryptor overload with the same signature to decrypt the result of this method.
The following example encrypts a string using the transform object returned from the CreateEncryptor method.
using System; using System.Security.Cryptography; using System.Text; class EncryptorExample { private static string quote = "Things may come to those who wait, but only the " + "things left by those who hustle. -- Abraham Lincoln"; public static void Main() { AesCryptoServiceProvider aesCSP = new AesCryptoServiceProvider(); aesCSP.GenerateKey(); aesCSP.GenerateIV(); byte[] encQuote = EncryptString(aesCSP, quote); Console.WriteLine("Encrypted Quote:\n"); Console.WriteLine(Convert.ToBase64String(encQuote)); Console.WriteLine("\nDecrypted Quote:\n"); Console.WriteLine(DecryptBytes(aesCSP, encQuote)); } public static byte[] EncryptString(SymmetricAlgorithm symAlg, string inString) { byte[] inBlock = UnicodeEncoding.Unicode.GetBytes(inString); ICryptoTransform xfrm = symAlg.CreateEncryptor(); byte[] outBlock = xfrm.TransformFinalBlock(inBlock, 0, inBlock.Length); return outBlock; } public static string DecryptBytes(SymmetricAlgorithm symAlg, byte[] inBytes) { ICryptoTransform xfrm = symAlg.CreateDecryptor(); byte[] outBlock = xfrm.TransformFinalBlock(inBytes, 0, inBytes.Length); return UnicodeEncoding.Unicode.GetString(outBlock); } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
You cannot create an instance of an abstract class. Application code will create a new instance of a derived class.
This algorithm supports key lengths from 40 bits to 1024 bits in increments of 8 bits, but the RC2CryptoServiceProvider implementation only supports key lengths from 40 bits to 128 bits in increments of 8 bits.
This method initializes the protected fields of SymmetricAlgorithm to the default values listed in the following table.
- 9/13/2010
- MarkDeibert
<#
.SYNOPSIS
This script enctypts then decrpyts a string using AES
.DESCRIPTION
This script re-implements an MSDN sample that first
enctypts a string then decrypts it. The crypto is done
using AES. Running this script multiple times will result
in differently enctypted quotes since a new key/IV is
generated each time you run the script.
.NOTES
File Name : Get-AESEnctyptedString.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/09d0kyb3.aspx
.EXAMPLE
PSH [C:\foo]: .\Get-AesEncryptedString.ps1
Quote:
Things may come to those who wait, but only the things left by those who hustle. -- Abraham Lincoln
Encrypted Quote:
zp4gD/Mmu01Msr3V8d94Kvwut4ClaW+f1HyohT+QjpFN5FVNhyLcQIgia4iD2TcsK/SpLcm5cOi1/KM+d9ENeU2Lkn8fOZHpMnklUrABGDoM1BRQDfWNcVbMIOA4IUY
zLoJ5huHKhnyPA2fobGouW33HOpONI0zR3KS4RdtD3M3QQsqHW2+QOkE4/Ls/5gCSbcTuP2FDkk3J9b+1XgqzJZx0jZIBl+moUcqA33xQdu5bGhwg1E8sk2oEU8AQyy
XcRrS/h8vTLve8c0Tpj4f2Vg==
Decrypted Quote:
Things may come to those who wait, but only the things left by those who hustle. -- Abraham Lincoln
#>
# Best Practice!
Set-StrictMode -version 2
# Two helper functions
function EncryptString{
param( $SymAlg,
[string] $inString
)
$inBlock = [System.Text.UnicodeEncoding]::Unicode.getbytes($instring)
$xfrm = $symAlg.CreateEncryptor()
$outBlock = $xfrm.TransformFinalBlock($inBlock, 0, $inBlock.Length);
return $outBlock;
}
function DecryptBytes {
param ($SymAlg,
$inBytes
)
$xfrm = $symAlg.CreateDecryptor();
$outBlock = $xfrm.TransformFinalBlock($inBytes, 0, $inBytes.Length)
return [System.Text.UnicodeEncoding]::Unicode.GetString($outBlock)
}
# main script
$Quote = "Things may come to those who wait, but only the " +
"things left by those who hustle. -- Abraham Lincoln"
"Quote:";$Quote;""
# Generate CSP, Key and IV
$AesCSP = New-Object System.Security.Cryptography.AesCryptoServiceProvider
$AesCSP.GenerateKey()
$AesCSP.GenerateIV()
# Encrypt quote
$EncQuote = EncryptString $aesCSP $quote
"Encrypted Quote:"
[System.Convert]::ToBase64String($encQuote)
""
# Now Decrypt
"Decrypted Quote:"
DecryptBytes $AesCSP $EncQuote
- 8/11/2010
- Thomas Lee
- 8/11/2010
- Thomas Lee