バッチファイルとパワーシェルでテキストやテキストファイルを暗号化して復号化する

2024年6月25日

バッチファイル

powershell Set-Clipboard -Value "This"
powershell  Set-Content -Path .\DateTime.txt -Value (Get-Date)
timeout /t 5

powershell -Command "$Secure = ConvertTo-SecureString "P@ssW0rD!" -AsPlainText -Force;$Encrypted =ConvertFrom-SecureString -SecureString $Secure;$Encrypted | Set-Content Encrypted.txt  "
powershell -Command "$PlainText =Get-Clipboard -Format Text;$Secure = ConvertTo-SecureString $PlainText -AsPlainText -Force;$Encrypted =ConvertFrom-SecureString -SecureString $Secure;$Encrypted | Set-Content Encrypted.txt  "
powershell -Command "$PlainText =Get-Content -Path .\DateTime.txt;$Secure = ConvertTo-SecureString $PlainText -AsPlainText -Force;$Encrypted =ConvertFrom-SecureString -SecureString $Secure;$Encrypted | Set-Content Encrypted.txt  "
::powershell -Command "$Secure =(Read-Host -AsSecureString);$Encrypted =ConvertFrom-SecureString -SecureString $Secure;$Encrypted | Set-Content Encrypted.txt  "

powershell -Command "$Secure2 = Get-Content Encrypted.txt | ConvertTo-SecureString;$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Secure2);$str  = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr);Write-Host $str  "
timeout /t 50

別なパソコンに移しても使いたい時

URL を暗号化

クリップボードから暗号化

powershell -Command "$PlainText =Get-Clipboard -Format Text;$Secure = ConvertTo-SecureString $PlainText -AsPlainText -Force;$Encrypted =ConvertFrom-SecureString -SecureString $Secure;$Encrypted | Set-Content Encrypted.txt  "

ファイルから戻す

powershell -Command "$Secure2 = Get-Content Encrypted.txt | ConvertTo-SecureString;$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Secure2);$str  = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr);Write-Host $str  ">unEncrypted.txt

Chrome で開く

FOR /F "usebackq" %%a IN (`powershell -command " $contents = (Get-Content unEncrypted.txt ); return $contents "`) DO @SET contents=%%a
echo %contents%
timeout /t 2
"C:\Program Files\Google\Chrome\Application\chrome.exe" %contents%
del unEncrypted.txt

一括処理

ファイルから戻す

set aa=C:\app\Portable\*.txt
for %%A in (%aa%) do (

powershell -Command "$Secure2 = Get-Content %%A | ConvertTo-SecureString;$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Secure2);$str  = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr);Write-Host $str  ">"%%A.dat"
)
copy %aa%.dat y:\urls.txt
del %aa%.dat 

timeout /t 5
"C:\Program Files\Google\Chrome\Application\chrome.exe" y:\output.html

bat,PowerShell ,windows

Posted by eightban