Skip to content

Commit 4a705b2

Browse files
committed
Initial commit
0 parents  commit 4a705b2

File tree

8 files changed

+217
-0
lines changed

8 files changed

+217
-0
lines changed

Crypto.RSA/Crypto.RSA.psd1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
@{
3+
RootModule = 'Crypto.RSA.psm1'
4+
ModuleVersion = '1.0.0'
5+
GUID = '17a38e59-e885-411a-89a6-617a4efc5f21'
6+
Author = 'Alan Plocieniak'
7+
CompanyName = 'Alan Plocieniak'
8+
Copyright = '(c) 2022 Alan Plocieniak. All rights reserved.'
9+
Description = 'PowerShell module for cryptography (RSA)'
10+
PowerShellVersion = '5.0'
11+
FunctionsToExport = '*'
12+
PrivateData = @{
13+
PSData = @{
14+
}
15+
}
16+
}

Crypto.RSA/Crypto.RSA.psm1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#Get public and private function definition files.
2+
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
3+
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
4+
5+
#Dot source the files
6+
Foreach ($import in @($Public + $Private)) {
7+
try {
8+
. $import.fullname
9+
}
10+
catch {
11+
Write-Error -Message "Failed to import function $($import.fullname): $_"
12+
}
13+
}
14+
Export-ModuleMember -Function $Public.Basename

Crypto.RSA/Private/Use.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function Use {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true)]
5+
[AllowEmptyString()]
6+
[AllowEmptyCollection()]
7+
[AllowNull()]
8+
[Object]
9+
$InputObject,
10+
11+
[Parameter(Mandatory = $true)]
12+
[scriptblock]
13+
$ScriptBlock
14+
)
15+
16+
try {
17+
. $ScriptBlock
18+
}
19+
finally {
20+
if ($null -ne $InputObject -and $InputObject -is [System.IDisposable]) {
21+
$InputObject.Dispose()
22+
}
23+
}
24+
}

Crypto.RSA/Public/Get-KeyString.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function Get-KeyString {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true, Position = 0 )]
5+
[System.Security.Cryptography.RSAParameters]$publicKey
6+
)
7+
8+
begin {
9+
Write-Verbose "Cmdlet Get-KeyString - Begin"
10+
}
11+
12+
process {
13+
Write-Verbose "Cmdlet Get-KeyString - Process"
14+
$stringWriter = [System.IO.StringWriter]::new()
15+
$xmlSerializer = [System.Xml.Serialization.XmlSerializer]::new([System.Security.Cryptography.RSAParameters])
16+
$xmlSerializer.Serialize($stringWriter, $publicKey)
17+
$stringWriter.ToString()
18+
}
19+
20+
end {
21+
Write-Verbose "Cmdlet Get-KeyString - End"
22+
}
23+
}

Crypto.RSA/Public/New-KeyPair.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function New-KeyPair {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $false, Position = 0 )]
5+
[System.Security.Cryptography.RSACryptoServiceProvider]$cryptoServiceProvider
6+
)
7+
8+
begin {
9+
Write-Verbose "Cmdlet New-KeyPair - Begin"
10+
}
11+
12+
process {
13+
Write-Verbose "Cmdlet New-KeyPair - Process"
14+
if ($cryptoServiceProvider -eq $null) {
15+
$cryptoServiceProvider = [System.Security.Cryptography.RSACryptoServiceProvider]::new(2048)
16+
}
17+
$privateKey = $cryptoServiceProvider.ExportParameters($true)
18+
$publicKey = $cryptoServiceProvider.ExportParameters($false)
19+
@{
20+
'public' = Get-KeyString $publicKey
21+
'private' = Get-KeyString $privateKey
22+
}
23+
}
24+
25+
end {
26+
Write-Verbose "Cmdlet New-KeyPair - End"
27+
}
28+
}

Crypto.RSA/Public/Protect-String.ps1

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function Protect-String {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true, Position = 0 )]
5+
[string]$textToEncrypt,
6+
7+
[Parameter(Mandatory = $true, Position = 1 )]
8+
[string]$publicKeyString
9+
)
10+
11+
begin {
12+
Write-Verbose "Cmdlet Protect-String - Begin"
13+
}
14+
15+
process {
16+
Write-Verbose "Cmdlet Protect-String - Process"
17+
18+
$bytesToEncrypt = [System.Text.Encoding]::UTF8.GetBytes($textToEncrypt)
19+
Use ($rsa = [System.Security.Cryptography.RSACryptoServiceProvider]::new(2048)) {
20+
try {
21+
$rsa.FromXmlString($publicKeyString.ToString());
22+
$encryptedData = $rsa.Encrypt($bytesToEncrypt, $true);
23+
$base64Encrypted = [System.Convert]::ToBase64String($encryptedData);
24+
$base64Encrypted;
25+
}
26+
finally {
27+
$rsa.PersistKeyInCsp = $false;
28+
}
29+
}
30+
}
31+
32+
end {
33+
Write-Verbose "Cmdlet Protect-String - End"
34+
}
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function Unprotect-String {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true, Position = 0 )]
5+
[string]$textToDecrypt,
6+
7+
[Parameter(Mandatory = $true, Position = 1 )]
8+
[string]$privateKeyString
9+
)
10+
11+
begin {
12+
Write-Verbose "Cmdlet Unprotect-String - Begin"
13+
}
14+
15+
process {
16+
Write-Verbose "Cmdlet Unprotect-String - Process"
17+
18+
Use ($rsa = [System.Security.Cryptography.RSACryptoServiceProvider]::new(2048)) {
19+
try {
20+
21+
$rsa.FromXmlString($privateKeyString);
22+
23+
$resultBytes = [System.Convert]::FromBase64String($textToDecrypt);
24+
$decryptedBytes = $rsa.Decrypt($resultBytes, $true);
25+
$decryptedData = [System.Text.Encoding]::UTF8.GetString($decryptedBytes);
26+
return $decryptedData.ToString()
27+
}
28+
finally {
29+
$rsa.PersistKeyInCsp = $false;
30+
}
31+
}
32+
}
33+
34+
end {
35+
Write-Verbose "Cmdlet Unprotect-String - End"
36+
}
37+
}

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Crypto.RSA
2+
3+
This is a PowerShell module for doing slack history backups.
4+
5+
# Installation
6+
Module is available on [Powershell Gallery][gallery]
7+
8+
### Install
9+
```powershell
10+
PS> Install-Module -Name Crypto.RSA
11+
```
12+
13+
### Import
14+
```powershell
15+
PS> Import-Module Crypto.RSA
16+
```
17+
# Usage
18+
19+
```powershell
20+
$keys = New-KeyPair
21+
$plain = "Crypto.RSA"
22+
$encryptedText = Protect-String $plain $keys.public
23+
$decryptedText = Unprotect-String $encryptedText $keys.private
24+
25+
Write-Host "Encryption:" -ForegroundColor Green
26+
Write-Host "[$plain] `t=> `t[$encryptedText]"
27+
Write-Host "Decryption:" -ForegroundColor Green
28+
Write-Host "[$decryptedText] `t<= `t[$encryptedText]"
29+
```
30+
31+
**Output:**
32+
33+
```plain
34+
Encryption:
35+
[Crypto.RSA] => [e7BM+iNpec8Uc90me8U8edKWKT7cpxSaBoBu62ReATypqxmq/oagPke3jUX955RMP7sL/mvXcpV75j2/i9QL5EmcPvKpvGpByQ2EIaxIAtV73ukp7v0jXSCsp965mOX6WcvuyWH6sn8y9jZSYauWo0eFoKzddf0GQJBHYDcNqgpUya9GwhzzP4wOuNW9D1kKqgIbnVqt92xuES1o3qe1ecd1n/8eu1I9szjFWu58C9XzHiXYC9lJD+y0be6Ft1gf4n/ASGC7mJCiiXqnzTlNcEP8Pg8UyqealCn/EdbpKLGfwpl5RgSLpk7jZq3SEo+6wrymp/fhDr2ALlv7gACtYw==]
36+
Decryption:
37+
[Crypto.RSA] <= [e7BM+iNpec8Uc90me8U8edKWKT7cpxSaBoBu62ReATypqxmq/oagPke3jUX955RMP7sL/mvXcpV75j2/i9QL5EmcPvKpvGpByQ2EIaxIAtV73ukp7v0jXSCsp965mOX6WcvuyWH6sn8y9jZSYauWo0eFoKzddf0GQJBHYDcNqgpUya9GwhzzP4wOuNW9D1kKqgIbnVqt92xuES1o3qe1ecd1n/8eu1I9szjFWu58C9XzHiXYC9lJD+y0be6Ft1gf4n/ASGC7mJCiiXqnzTlNcEP8Pg8UyqealCn/EdbpKLGfwpl5RgSLpk7jZq3SEo+6wrymp/fhDr2ALlv7gACtYw==]
38+
```
39+
40+
[gallery]: https://www.powershellgallery.com/packages/Crypto.RSA

0 commit comments

Comments
 (0)