Modules/MessageEncoder/messageencoder.psm1
<# .SYNOPSIS Generates public and private key for encryption. .DESCRIPTION Generates a 4096-bit RSA key pair and places them in the appropriate certificate stores. The keys are also saved in the ``$HOME\.keys`` location, and the private key is password protected. The public key is used to encrypt the message using the ``Protect-Message`` cmdlet and the corresponding private key is used to decrypt it using the ``Unprotect-Message`` cemdlet. .PARAMETER Name Specifies the name of the certificate. This name will be used by users who encrypt the message with the public key. .EXAMPLE New-EncryptionKey "CertificateName" #> function New-EncryptionKey{ [CmdletBinding(SupportsShouldProcess, HelpUri="https://github.com/akotu235/APS/blob/master/Docs/Modules/MessageEncoder/New-EncryptionKey.md")] Param( [Parameter(Mandatory=$true)] [System.String]$Name ) $store = "cert:\CurrentUser\My" $params = @{ CertStoreLocation = $store Subject = "CN=APS_$Name" KeyLength = 4096 KeyAlgorithm = "RSA" KeyUsage = "DataEncipherment" Type = "DocumentEncryptionCert" NotAfter = (Get-Date).AddYears(10) } $cert = New-SelfSignedCertificate @params $keysDir = "$HOME\.keys" if(-not (Test-Path "$keysDir\My")){ New-Item -ItemType Directory -Path "$keysDir\My" -Force >> $null } $privateKey = "$keysDir\My\$Name.pfx" $publicKey = "$keysDir\My\$Name.pub.cer" $keyPassword = Read-Password -Prompt "Create a key password" Export-PfxCertificate -FilePath $privateKey -Cert $cert -Password $keyPassword -Force Export-Certificate -FilePath $publicKey -Cert $cert -Force explorer.exe "$keysDir\My" $cert | Remove-Item try{ Import-PfxCertificate -FilePath $privateKey -CertStoreLocation $store -Password $keyPassword -ProtectPrivateKey VSM } catch{ Import-PfxCertificate -FilePath $privateKey -CertStoreLocation $store -Password $keyPassword } Get-ChildItem Cert:\CurrentUser\CA | Where-Object Subject -Like "CN=APS_$Name" | Remove-Item } <# .SYNOPSIS Encrypts the message. .DESCRIPTION Encrypts the message with the indicated rsa public key and copies it to the clipboard. .PARAMETER Message Specifies the message to encrypt. .EXAMPLE Protect-Message "<secret>" #> function Protect-Message{ [OutputType([System.String])] [CmdletBinding(HelpUri="https://github.com/akotu235/APS/blob/master/Docs/Modules/MessageEncoder/Protect-Message.md")] Param( [Parameter(Mandatory=$true)] [System.String]$Message ) $storageCA = Get-ChildItem Cert:\CurrentUser\CA -DocumentEncryptionCert | Where-Object Subject -Like "CN=APS_*" if($storageCA){ $storageCA | ForEach-Object { Move-Item -Path Cert:\CurrentUser\CA\$($_.Thumbprint) -Destination Cert:\CurrentUser\My } } $encryptionCerts = Get-ChildItem Cert:\CurrentUser\My -DocumentEncryptionCert if($encryptionCerts.Count -eq 0){ Write-Host "No encryption certificates!" -ForegroundColor Red } else{ Write-Output "Installed certificates:" $encryptionCerts | ForEach-Object {$_.Subject.TrimStart("CN=APS_")} $cipher = $Message | Protect-CmsMessage -To $(Read-Key) Write-Host "Cipher:" -ForegroundColor Green $cipher = "`"$($cipher.Replace("`r`n",";").Replace("`n",";").TrimStart("-----BEGIN CMS-----").TrimEnd("-----END CMS-----"))`"" Set-Clipboard -Value $cipher return $cipher } } function Read-Key{ $CN = Read-Host -Prompt "Enter the name of the encryption certificate" if($CN -notlike "CN=APS_*"){ $CN = "CN=APS_$CN" } return $CN } <# .SYNOPSIS Decrypts the message. .DESCRIPTION Decrypts the message if the appropriate private key is installed. .PARAMETER Name Specify an encrypted message. .EXAMPLE Unprotect-Message "<encrypted message>" #> function Unprotect-Message{ [CmdletBinding(HelpUri="https://github.com/akotu235/APS/blob/master/Docs/Modules/MessageEncoder/Unprotect-Message.md")] Param( [Parameter(Mandatory=$true)] [System.String]$Message ) $Message = "-----BEGIN CMS-----$($Message.Trim('"'))-----END CMS-----".Replace(";","`r`n") try{ $decryptedMessage = ($Message | Unprotect-CmsMessage) Write-Host "Decrypted message:" -ForegroundColor Green $decryptedMessage } catch{ Write-Host "No proper key to read this message" -ForegroundColor Red } } # SIG # Begin signature block # MIIIWAYJKoZIhvcNAQcCoIIISTCCCEUCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4w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