Private/NewCryptographyKey.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<#
    .SYNOPSIS
    Generates a random cryptography key.
 
    .DESCRIPTION
    Generates a random cryptography key based on the desired key size.
 
    .PARAMETER Algorithm
    Algorithm to generate key for.
 
    .PARAMETER KeySize
    Number of bits the generated key will have.
 
    .PARAMETER AsPlainText
    Returns a String instead of SecureString.
 
    .OUTPUTS
    System.Security.SecureString. New-CryptographyKey return the key as a SecureString by default.
    System.String. New-CryptographyKey will return the key in plain text as a string if the -AsPlainText parameter is specified.
 
    .EXAMPLE
    $key = New-CryptographyKey
    This example generates a random 256-bit AES key and stores it in the variable $key.
 
    .NOTES
    Author: Tyler Siegrist
    Date: 9/22/2017
#>

function NewCryptographyKey() {
    [CmdletBinding()]
    [OutputType([System.Security.SecureString])]
    [OutputType([String], ParameterSetName='PlainText')]
    Param(
        [Parameter(Mandatory=$false, Position=1)]
        [ValidateSet('AES','DES','RC2','Rijndael','TripleDES')]
        [String]$Algorithm='AES',
        [Parameter(Mandatory=$false, Position=2)]
        [Int]$KeySize,
        [Parameter(ParameterSetName='PlainText')]
        [Switch]$AsPlainText
    )
    Process
    {
        try
        {
            $Crypto = [System.Security.Cryptography.SymmetricAlgorithm]::Create($Algorithm)
            if($PSBoundParameters.ContainsKey('KeySize')){
                $Crypto.KeySize = $KeySize
            }
            $Crypto.GenerateKey()
            if($AsPlainText)
            {
                return [System.Convert]::ToBase64String($Crypto.Key)
            }
            else
            {
                return [System.Convert]::ToBase64String($Crypto.Key) | ConvertTo-SecureString -AsPlainText -Force
            }
        }
        catch
        {
            Write-Error $_
        }
    }
}