PowerShell 综合练习
完成一个程序,具有以下功能
- 登录(输入用户名和密码,密码文件格式为 name:password:rwx )
- 文件加密(输入需要加密的文本文件路径,不加后缀;产生加密后的文件和密钥)
- 文件解密(输入加密后的encry文件,与密钥在同一文件夹下,不加后缀)
- 文件删除(输入需要删除的任意文件路径)
- 文件编辑(输入文本文件路径,输入内容覆盖文件)
- 文件显示(显示路径下文本或文本内容)
cls$path_c = Read-Host \"输入密码文件路径\"$path0 = $path_c + \".txt\"[string[]]$password = Get-Content $path0$password = $password.Split(\":\")$password = $password.Split()# 拆分密码、用户、权限[string[]]$name = @()[string[]]$pass = @()[string[]]$rwx = @()for($i = 0; $i -lt $password.Length; $i++){if($i % 3 -eq 0){$name += $password[$i]$pass += $password[$i + 1]$rwx += $password[$i + 2]}}# 登录$token = 1$count = 0$power = 0$in0 = Read-Host \"输入用户名\"while($token){if($name -contains $in0){$in1 = Read-Host \"输入密码\"while($token){$i = [array]::IndexOf($name,$in0)if($pass[$i].Equals($in1)){$token = 0$power = $ibreak;}else{if($count -eq 2){exit}$in1 = Read-Host \"密码错误,请重新输入\"$count++}}}else{$in0 = Read-Host \"用户名错误,请重新输入\"}}cls# 菜单Write-Output(\"主菜单\")Write-Output(\"1.文件加密\")Write-Output(\"2.文件解密\")Write-Output(\"3.删除文件\")Write-Output(\"4.编写文件\")Write-Output(\"5.显示文件\")Write-Output(\"输入 exit 退出程序\")Write-Output(\" \")# 接收菜单命令$num = \"1\",\"2\",\"3\",\"4\",\"5\",\"exit\"while(1){while($num -contains $in2){$in2 = Read-Host \"选择您要执行的功能\"if($in2.Equals(\"exit\")){exit}clsWrite-Output(\"主菜单\")Write-Output(\"1.文件加密\")Write-Output(\"2.文件解密\")Write-Output(\"3.删除文件\")Write-Output(\"4.编写文件\")Write-Output(\"5.显示文件\")Write-Output(\"输入 exit 退出程序\")Write-Output(\" \")# 文件加密if($in2 -eq 1){$path_a = Read-Host \"输入文件路径\"$path = $path_a + \".txt\"$path_0 = $path.Substring(0, $path.LastIndexOf(\".\"))[string]$file_content = Get-Content $path$file_content = $file_content.Split()[int[]][char[]]$file = $file_contentif($rwx[$power].Contains(\"x\")){if($rwx[$power].Contains(\"r\")){Write-Output(\"文件内容\")Write-Host($file_content)-NoNewlineWrite-Output(\"\")}# 生成密钥、加密[string[]]$key = @()for($i = 0; $i -lt $file.Length; $i++){$rand = 0..100$key_word = Get-Random -InputObject $rand$file[$i] = $file[$i] -bxor $key_word$key += $key_word}$path5 = $path_0 + \"_key.txt\"$path6 = $path_0 + \"_encry.txt\"$key | Out-File $path5$file | Out-File $path6if($rwx[$power].Contains(\"r\")){Write-Output(\"文件密文\")Write-Host($file) -NoNewlineWrite-Output(\"\")Write-Output(\"文件密钥\")Write-Host($key) -NoNewlineWrite-Output(\"\")}Write-Output(\"文件加密已完成\")}elseif($rwx[$power].Contains(\"r\")){Write-Host($file_content) -NoNewlineWrite-Output(\"\")Write-Output(\"你没有文件的加密权限\")}else{Write-Output(\"你没有足够的权限\")}}# 文件解密if($in2 -eq 2){$path_b = Read-Host \"输入文件路径\"$path1 = $path_b + \".txt\"$path_1 = $path1.Substring(0, $path1.LastIndexOf(\"_\"))$path2 = $path_1 + \"_key.txt\"$path_2 = $path_1 + \".txt\"if($rwx[$power].Contains(\"x\")){$file1 = Get-Content $path1$file2 = Get-Content $path2for($i = 0; $i -lt $file1.Length; $i++){$file1[$i] = $file1[$i] -bxor $file2[$i]}[char[]][int[]]$file1 = $file1if($rwx[$power].Contains(\"r\")){Write-Host($file1) -NoNewlineWrite-Host(\"\")$file1 | Out-File $path_2}}else{Write-Output(\"你没有足够的权限\")}}# 删除文件if($in2 -eq 3){if($rwx[$power].Contains(\"x\")){$path3 = Read-Host \"输入文件路径\"$path_d = $path3 + \".txt\"del $path3, $path_dWrite-Output(\"文件已删除\")}else{Write-Output(\"你没有足够的权限\")}}# 编写文件if($in2 -eq 4){if($rwx[$power].Contains(\"w\")){$path_e = Read-Host \"输入路径\"$path4 = $path_e + \".txt\"$content = Read-Host \"输入新的信息\"$content | Out-File $path4Write-Output(\"文件已修改\")}}# 显示文件if($in2 -eq 5){$path6 = Read-Host \"输入路径\"if($path6.Contains(\".txt\")){if($rwx[$power].Contains(\"r\")){$content0 = Get-Content $path6Write-Output(\"文件内容为:\")Write-Output($content0)}else{Write-Output(\"你没有读取权限\")}}else{dir $path6}}}Write-Output(\"输入错误\")$in2 = Read-Host \"请重新输入\"if($in2.Equals(\"exit\")){exit}}