Sunday, 10. January 2010 21:47
Hello again. This time I want to share a function to convert a plain-text string to an encoded string, ConvertTo-BlurredString, and a function to decode an encrypted string, ConvertFrom-BlurredString. The function names contain the wording “blurred” because my encryption method should be considered very, very basic. It is a real low-tech security solution. Please keep that in mind!
I wrote the algorithm a couple of years ago in order to save sensitive data like email addresses in HTML files or passwords in INI files in a more secure manner than in plain-text. At least the blurred string protects the data from reading out accidentally if a curious user is watching an admin at work.
Enough talk – here comes function #1, ConvertTo-BlurredString:
function ConvertTo-BlurredString {
<#
.SYNOPSIS
Converts a plain-text string to a blurred string.
.DESCRIPTION
The ConvertTo-BlurredString function converts a plain-text string into
a blurred string. It is used with ConvertFrom-BlurredString. The blurred
string can be used in configuration files (.ini or .xml for example) in
order to prevent unwanted readout by curious people.
ConvertTo-BlurredString is a very basic security solution. Do not use it
in high security scenarios.
.EXAMPLE
PS C:\> ConvertTo-BlurredString "Hello, World!"
81:648:909:972:972:999:396:288:783:999:1026:972:900:297
PS C:\>
.EXAMPLE
PS C:\> $blurred = "Hello, World!" | ConvertTo-BlurredString
PS C:\> $blurred
55696:16992:23836:25488:25488:26196:10384:7552:20532:26196:26904:25488:23600:7788
PS C:\>
#>
param(
[Parameter(ValueFromPipeline=$true)]
[string]
$String
)
begin {
function BlurString ($String) {
$Random = (New-Object System.Random).Next(2,254)
$Key = [string]($Random * $Random)
[string]$ReturnValue = $Key
$String.ToCharArray() | ForEach-Object {
$ReturnValue += ":" + ([Byte][Char]$_) * $Random
}
$ReturnValue
}
}
process {
if ($_) {
$String = $_
} elseif (!$String) {
$String = $(Read-Host "Supply a value for the String parameter")
}
BlurString $String
}
}
And now the according ConvertFrom function, ConvertFrom-BlurredString:
function ConvertFrom-BlurredString {
<#
.SYNOPSIS
Converts a blurred string to a plain-text string.
.DESCRIPTION
The ConvertFrom-BlurredString function converts an blurred string into
a plain-text string. It is used with ConvertTo-BlurredString.
.EXAMPLE
PS C:\> "81:648:909:972:972:999:396:288:783:999:1026:972:900:297" | ConvertFrom-BlurredString
Hello, World!
PS C:\>
#>
param(
[Parameter(ValueFromPipeline=$true)]
[string]
$BlurredString
)
begin {
function UnravelString ($String) {
$Chars = $String.Split(':')
if ($Chars.Count -lt 2) {
throw "'$String' is not blurred with ConvertTo-BlurredString"
}
[string]$ReturnValue = ""
$Key = $Chars[0]
foreach ($Char in $Chars[1..$Chars.Count]) {
$ReturnValue += [Char][Byte]($Char/[math]::Sqrt($Key))
}
$ReturnValue
}
}
process
{
if ($_) {
$BlurredString = $_
} elseif (!$BlurredString) {
$BlurredString = $(Read-Host "Supply a value for the BlurredString parameter")
}
UnravelString $BlurredString
}
}
How about using this functions as a module? Ok, just follow this steps:
Create a new directory in your Modules directory, for example “BlurStrings”.
Save the two functions in this folder.
Type the following commands in a text editor and save it as psm1 file, for example BlurStrings.psm1
. $psScriptRoot\ConvertTo-BlurredString.ps1
. $psScriptRoot\ConvertFrom-BlurredString.ps1
That’s all. The module is ready for use. If you enter
Get-Module –ListAvailable
you’ll see a new script module called BlurStrings (for example). If you need to use the module’s functions type
Import-Module BlurStrings
Now, the functions are ready to use. Hope you like it.