Tuesday, October 13, 2009

Secure-Phrase

"When in the Course of human events it becomes necessary for one people to..." completely secure everything they have, but are running out of passwords, a tool must be created to convert existing phrases known by the user to a secure passphrase. That time is now, and the tool is below:

SCRIPT

param (

  [string]$convert, #phrase to convert to passphrase
  [switch]$poss #ask for total phrases possible
)

$ErrorActionPreference = "SilentlyContinue"

function GenRand ($char) { # returns a random number between 0 and the input string length
  $random = $rand.Next(0,($char.Count))
  Start-Sleep -m 1
  $char[$random]
}

function GenPoss ($text) { # generates the number of possible outputs for a specific character or phonetic string
  $possible = 1
  $chars = (0..($text.Length - 1)) | % { $text[$_] }
  foreach ($char in $chars) {
    foreach ($count in (0..($alpha.Count - 1))) {
      $alphaMatch = $alpha[$count][0]
      if ($char -match $alphaMatch) { $match = $true ; break }
    }
    if ($match) { $possible *= $alpha[$count].Count ; $match = $false }
    else {
      foreach ($spec in $special) {
        if ($char -match $spec) { $match = $true ; break }
      }
      if ($match) { $possible *= $special.Count ; $match = $false }
    }
  }
  return $possible
}

$rand = New-Object System.Random

$a = "a","A","4","`@"
$b = "b","B","8"
$c = "c","C","`("
$d = "d","D"
$e = "e","E","3"
$f = "f","F","ph"
$g = "g","G","6"
$h = "h","H"
$i = "i","I","`!","1"
$j = "j","J"
$k = "k","K"
$l = "l","L","`|"
$m = "m","M"
$n = "n","N"
$o = "o","O","0"
$p = "p","P","`?"
$q = "q","Q","9"
$r = "r","R"
$s = "s","S","`$","5"
$t = "t","T","7","`+"
$u = "u","U"
$v = "v","V"
$w = "w","W"
$x = "x","X","`*"
$y = "y","Y"
$z = "z","Z"
$space = " ","_","-"

$ate = "ate","8"
$eat = "eat","8"
$any = "any","ne"
$one = "one","1"
$won = "won","1"
$ph = "ph","f"
$oo = "oo","u"
$ck = "ck","k"
$ew = "ew","oo"
$two = "two","too","to"

$to = "to","two","too"
$too = "too","to","two"
$0 = "0","O","o"

$1 = "1","one","won"
$2 = "2","two","to","too"
$3 = "3","three"
$4 = "4","four","fore","for"
$5 = "5","five"
$6 = "6","six"
$7 = "7","seven"
$8 = "8","eight","ate"
$9 = "9","nine"
$special = "`~","`!","`@","`#","`$","`%","`^","`&","`*","`(","`)","`_","`+","`\{","`}","`|","`:","`"","`<","`>","`?","``","`-","`=","`[","`]","`\","`;","`'","`,","`.","`/"

$alpha = @($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$t,$u,$v,$w,$x,$y,$z,$space)
$phone = @($ate,$eat,$any,$one,$won,$ph,$oo,$ck,$ew,$two,$to,$too,$0,$1,$2,$3,$4,$5,$6,$7,$8,$9)

$text = $convert

foreach ($count in (0..($phone.Count-1))) {
  $old = $phone[$count][0]
  $new = GenRand ($phone[$count])
  $text = $text.Replace($old,$new)
}

$chars = (0..($text.Length - 1)) | % { $text[$_] }
$newChars = ""

foreach ($char in $chars) {
  foreach ($count in (0..($alpha.Count - 1))) {
    $alphaMatch = $alpha[$count][0]
    if ($char -match $alphaMatch) { $match = $true ; break }
  }
  if ($match) { $newChars += GenRand $alpha[$count] ; $match = $false }
  else {
    foreach ($spec in $special) {
      if ($char -match $spec) { $match = $true ; break }
    }
    if ($match) { $newChars += GenRand $special ; $match = $false }
    else { $newChars += $char }
  }
}

if ($poss) {
  $possible = 1
  foreach ($count in (0..($phone.Count-1))) {
    if ($convert -match $phone[$count][0]) { $possible *= GenPoss ($phone[$count][0]) }
  }
  $possible *= GenPoss $convert
}

if ($poss) { Write-Host "Password:" $newChars ; Write-Host "Possible:" $possible }
else { $newChars }
 code: copy : expand : collapse

USAGE
.\Secure-Phrase.ps1 "text_to_convert" [-p]
This will output a the text given using random replacements of the characters with the given set within $alpha.  The "-p" optional switch shows the total number of possibilites for the input text.

EXAMPLES
PS > (0..9) | % { .\Secure-Phrase.ps1 "convertme" }
C0nveR7M3

c0nvertMe
(ONV3r+ME
CONVeRTM3
cOnver+m3
conV3RtM3
(ONveR7Me
COnvErtMe
cONvER7mE
coNver7Me
Create 10 randomized passphrases from "convertme"

PS >.\Secure-Phrase.ps1 "convertme" -p
Password: CoNveRTme
Possible: 5184
Create a single passphrase from "convertme" and show the total number of possible passphrases

PS >.\Secure-Phrase.ps1 "!@#$%^&"
@/|&!.$
Create a single passphrase from !@#$%^

NOTE
The script will accept special characters and numbers.

0 comments: