2012-04-16 140 views
3

我正在使用gnupg通過powershell腳本加密和解密數據,問題是,我在密碼中的代碼。這可能不是最好的解決方案。哪種方法可以爲腳本提供密碼?謝謝。gpg與powershell - 密碼安全

C:\"Program Files"\GNU\GnuPG\gpg2.exe --passphrase mypassphrase --batch --output C:\Z\$decrypted_file.xls --decrypt C:\_Zo\$encrypted_file 

回答

4

您可以在磁盤上加密密碼或密碼。

以下解決方案使用計算機作爲用戶

以下兩個腳本是securot框架.NET程序集。

對於服務器計算機,可以通過計算機身份保護祕密。此代碼使用的事實是任何可以在計算機上運行代碼的人都可以訪問該密碼。所以passwword文件可以在網絡上共享,只能由服務器本身解碼。您可以將ACL添加到密碼文件中,以使其僅由某組用戶讀取。

Crypting(必須在服務器計算機上完成):

# Mandatory Framework .NET Assembly 
Add-Type -assembly System.Security 

# String to Crypt 
$passwordASCII = Read-Host -Prompt "Entrer le mot de passe" 

# String to INT Array 
$enc = [system.text.encoding]::Unicode 
$clearPWD_ByteArray = $enc.GetBytes($passwordASCII.tochararray()) 

# Crypting 
$secLevel = [System.Security.Cryptography.DataProtectionScope]::LocalMachine 
$bakCryptedPWD_ByteArray = [System.Security.Cryptography.ProtectedData]::Protect($clearPWD_ByteArray, $null, $secLevel) 

# Store in Base 64 form 
$B64PWD_ByteArray = [Convert]::ToBase64String($bakCryptedPWD_ByteArray) 
Set-Content -LiteralPath c:\Temp\pass.txt -Value $B64PWD_ByteArray 

解碼:

# Mandatory Framework .NET Assembly 
Add-Type -assembly System.Security 

# Getting from Base 64 storage 
$resCryptedPWD_ByteArray = [Convert]::FromBase64String((Get-Content -LiteralPath c:\Temp\pass.txt)) 

# Decoding 
$secLevel = [System.Security.Cryptography.DataProtectionScope]::LocalMachine 
$clearPWD_ByteArray = [System.Security.Cryptography.ProtectedData]::Unprotect($resCryptedPWD_ByteArray, $null, $secLevel) 

# Dtring from int Array 
$enc = [system.text.encoding]::Unicode 
$enc.GetString($clearPWD_ByteArray) 
+0

謝謝你,JPBlanc。如果我理解正確,我將密碼短語加密一次,解碼部分包含在我的腳本中。我已經測試過它,它工作正常,但我無法弄清楚哪個變量保存解碼密碼在我的gpg2.exe參數中使用它。 – culter 2012-04-16 12:13:54

+0

好的,我已經解決了。我已將解碼部分包含到腳本中,並使用$ end.GetString($ clearPWD_ByteArray)作爲密碼。請確認此方法是否正確(安全)。謝謝。 – culter 2012-04-16 12:57:22

+0

該口令在'$ enc.GetString($ clearPWD_ByteArray)' – JPBlanc 2012-04-16 13:04:04