-
Notifications
You must be signed in to change notification settings - Fork 17
/
convert-string.ps1
88 lines (73 loc) · 3.69 KB
/
convert-string.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
param(
[Parameter(Mandatory=$true)]
[object]$inputString,
[switch]$stringToBase64,
[switch]$base64ToString,
[switch]$urlEncodeToString,
[switch]$stringToUrlEncode,
[switch]$stringToRegexEscaped,
[switch]$regexToStringUnescaped,
[switch]$stringToCharArray,
[switch]$charArrayToString,
[switch]$rsaDecrypt,
[switch]$rsaEncrypt,
$certThumbprint
)
if($stringToBase64){
write-host "stringToBase64: [convert]::ToBase64String([text.encoding]::UTF8.GetBytes(`$inputString))" -ForegroundColor Green
$global:output = [convert]::ToBase64String([text.encoding]::UTF8.GetBytes($inputString))
write-host "stringToBase64: $($global:output)"
}
if($base64ToString){
write-host "base64ToString: [text.encoding]::UTF8.GetString([convert]::FromBase64String(`$inputString))" -ForegroundColor Green
$global:output = [text.encoding]::UTF8.GetString([convert]::FromBase64String($inputString))
write-host "base64ToString: $($global:output)"
}
if($urlEncodeToString) {
Write-Host "urlEncodeToString: [web.httpUtility]::UrlDecode(`$inputString)" -ForegroundColor Green
$global:output = [web.httpUtility]::UrlDecode($inputString)
Write-Host "urlEncodeToString: $($global:output)"
}
if($stringToUrlEncode) {
Write-Host "stringToUrlEncode: [web.httpUtility]::UrlEncode(`$inputString)" -ForegroundColor Green
$global:output = [web.httpUtility]::UrlEncode($inputString)
Write-Host "stringToUrlEncode: $($global:output)"
}
if($stringToRegexEscaped) {
Write-Host "stringToRegexEscaped: [regex]::Escape(`$inputString)" -ForegroundColor Green
$global:output = [regex]::Escape($inputString)
Write-Host "stringToRegexEscaped: $($global:output)"
}
if($regexToStringUnescaped) {
Write-Host "regexToStringUnescaped: [regex]::Unescape(`$inputString)" -ForegroundColor Green
$global:output = [regex]::Unescape($inputString)
Write-Host "regexToStringUnescaped: $($global:output)"
}
if($stringToCharArray) {
write-host "stringToCharArray: `$inputString.ToCharArray() | foreach-object {[int][char]`$_}" -ForegroundColor Green
$global:output = $inputString.ToCharArray() | foreach-object {[int][char]$_}
write-host "stringToCharArray: $($global:output)"
}
if($charArrayToString) {
write-host "charArrayToString: [string]::new(`$inputString)" -ForegroundColor Green
$global:output = [string]::new($inputString)
write-host "charArrayToString: $($global:output)"
}
if($certThumbprint) {
write-host "certThumbprint: $certThumbprint" -ForegroundColor Green
$cert = Get-ChildItem -Path cert:\ -Recurse | where-object thumbprint -eq $certThumbprint
if(!$cert) {
write-host "certThumbprint: $certThumbprint not found" -ForegroundColor Red
}
}
if($rsaDecrypt -and $cert) {
write-host "rsaDecrypt: [text.encoding]::UTF8.GetString(`$cert.PrivateKey.Decrypt([convert]::FromBase64String($inputString),[security.cryptography.RSAEncryptionPadding]::OaepSHA256))" -ForegroundColor Green
$global:output = [text.encoding]::UTF8.GetString($cert.PrivateKey.Decrypt([convert]::FromBase64String($inputString),[security.cryptography.RSAEncryptionPadding]::OaepSHA256))
write-host "rsaDecrypt: $($global:output)"
}
if($rsaEncrypt -and $cert) {
write-host "rsaEncrypt: [convert]::ToBase64String(`$cert.PublicKey.Key.Encrypt([text.encoding]::UTF8.GetBytes($inputString),[security.cryptography.RSAEncryptionPadding]::OaepSHA256))" -ForegroundColor Green
$global:output = [convert]::ToBase64String($cert.PublicKey.Key.Encrypt([text.encoding]::UTF8.GetBytes($inputString),[security.cryptography.RSAEncryptionPadding]::OaepSHA256))
write-host "rsaEncrypt: $($global:output)"
}
write-host "output stored in `$global:output" -ForegroundColor Cyan