Last active
November 28, 2023 04:43
-
-
Save ciaoly/23e3356bcd13192b45879c3e38a72bb5 to your computer and use it in GitHub Desktop.
一个简单的凯撒解密脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[Parameter(Mandatory=$true)] | |
[string] $Ciph = "", | |
[string] $GuideWord = "flag", | |
[switch] $SkipNoneAlpha = $false | |
) | |
begin { | |
if (($Ciph.Length -le 0) -or ($GuideWord.Length -le 1)){ | |
return; | |
} | |
function Test-ArithmeticProgression($arr) { | |
if ($arr.Count -le 1) { | |
return $False; | |
} | |
# Arrays.sort($arr);//先排序 | |
$delta = $arr[1] - $arr[0]; | |
foreach ($i in 2..($arr.Count - 1)) { | |
if ($arr[$i] - $arr[$i - 1] -ne $delta) { | |
return $False; | |
} | |
} | |
return $True; | |
} | |
} | |
process { | |
$bytes = [int[]][char[]]$Ciph | |
$a = @() | |
$firstIndex = 0; | |
$flag = ""; | |
foreach($i in 0..($bytes.Count - $GuideWord.Length)) { | |
foreach($j in 0..($GuideWord.Length - 1)) { | |
$a += $bytes[$i + $j] - [int][char]$GuideWord[$j]; | |
} | |
if (Test-ArithmeticProgression $a) { | |
$firstIndex = $i; | |
break; | |
} else { | |
# echo "$($a -join ' ')" | |
$a = @(); | |
} | |
} | |
foreach($i in $firstIndex..($bytes.Count - 1)) { | |
if ($SkipNoneAlpha) { | |
if(($bytes[$i] -lt [int][char]'A') -or ($bytes[$i] -gt [int][char]'z')) { | |
$flag += [char]$bytes[$i]; | |
continue; | |
} | |
if(($bytes[$i] -gt [int][char]'Z') -and ($bytes[$i] -lt [int][char]'a')) { | |
$flag += [char]$bytes[$i]; | |
continue; | |
} | |
} | |
$f = $bytes[$i] - $a[0] - $i * ($a[1] - $a[0]); | |
if (($f -gt 0) -and ($f -lt 127)) { | |
$flag += [char]$f; | |
} | |
if ($f -eq [int][char]"}") { | |
break; | |
} | |
} | |
echo "`n$($flag -join '')" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment