Powershell用于加密解密方法

################# 
# Powershell Allows The Loading of .NET Assemblies 
# Load the Security assembly to use with this script  
################# 
[Reflection.Assembly]::LoadWithPartialName("System.Security") 
 ################# 
# This function is to Encrypt A String. 
# $string is the string to encrypt, $passphrase is a second security "password" that has to be passed to decrypt. 
# $salt is used during the generation of the crypto password to prevent password guessing. 
# $init is used to compute the crypto hash -- a checksum of the encryption 
################# 
function Encrypt-String($String, $Passphrase, $salt="SaltCrypto", $init="IV_Password", [switch]$arrayOutput) 

    # Create a COM Object for RijndaelManaged Cryptography
    $r = new-Object System.Security.Cryptography.RijndaelManaged 
    # Convert the Passphrase to UTF8 Bytes
    $pass = [Text.Encoding]::UTF8.GetBytes($Passphrase)
    # Convert the Salt to UTF Bytes
    $salt = [Text.Encoding]::UTF8.GetBytes($salt)
  
    # Create the Encryption Key using the passphrase, salt and SHA1 algorithm at 256 bits
    $r.Key = (new-Object Security.Cryptography.PasswordDeriveBytes $pass, $salt, "SHA1", 5).GetBytes(32) #256/8
    # Create the Intersecting Vector Cryptology Hash with the init
    $r.IV = (new-Object Security.Cryptography.SHA1Managed).ComputeHash( [Text.Encoding]::UTF8.GetBytes($init) )[0..15] 
      
    # Starts the New Encryption using the Key and IV
    $c = $r.CreateEncryptor() 
    # Creates a MemoryStream to do the encryption in
    $ms = new-Object IO.MemoryStream 
    # Creates the new Cryptology Stream --> Outputs to $MS or Memory Stream
    $cs = new-Object Security.Cryptography.CryptoStream $ms,$c,"Write"
    # Starts the new Cryptology Stream
    $sw = new-Object IO.StreamWriter $cs 
    # Writes the string in the Cryptology Stream
    $sw.Write($String) 
    # Stops the stream writer
    $sw.Close() 
    # Stops the Cryptology Stream
    $cs.Close() 
    # Stops writing to Memory
    $ms.Close() 
    # Clears the IV and HASH from memory to prevent memory read attacks
    $r.Clear() 
    # Takes the MemoryStream and puts it to an array
    [byte[]]$result = $ms.ToArray() 
    # Converts the array from Base 64 to a string and returns
    return [Convert]::ToBase64String($result) 
}


 function Decrypt-String($Encrypted, $Passphrase, $salt="SaltCrypto", $init="IV_Password") 

    # If the value in the Encrypted is a string, convert it to Base64
    if($Encrypted -is [string]){ 
        $Encrypted = [Convert]::FromBase64String($Encrypted) 
    } 
  
    # Create a COM Object for RijndaelManaged Cryptography
    $r = new-Object System.Security.Cryptography.RijndaelManaged 
    # Convert the Passphrase to UTF8 Bytes
    $pass = [Text.Encoding]::UTF8.GetBytes($Passphrase) 
    # Convert the Salt to UTF Bytes
    $salt = [Text.Encoding]::UTF8.GetBytes($salt) 
  
    # Create the Encryption Key using the passphrase, salt and SHA1 algorithm at 256 bits     
    $r.Key = (new-Object Security.Cryptography.PasswordDeriveBytes $pass, $salt, "SHA1", 5).GetBytes(32) #256/8     
    # Create the Intersecting Vector Cryptology Hash with the init     
    $r.IV = (new-Object Security.Cryptography.SHA1Managed).ComputeHash( [Text.Encoding]::UTF8.GetBytes($init) )[0..15] 
  
    # Create a new Decryptor
    $d = $r.CreateDecryptor() 
    # Create a New memory stream with the encrypted value.
    $ms = new-Object IO.MemoryStream @(,$Encrypted) 
    # Read the new memory stream and read it in the cryptology stream
    $cs = new-Object Security.Cryptography.CryptoStream $ms,$d,"Read"
    # Read the new decrypted stream
    $sr = new-Object IO.StreamReader $cs 
    # Return from the function the stream
    Write-Output $sr.ReadToEnd() 
    # Stops the stream
    $sr.Close() 
    # Stops the crypology stream
    $cs.Close() 
    # Stops the memory stream
    $ms.Close() 
    # Clears the RijndaelManaged Cryptology IV and Key
    $r.Clear() 

 # This clears the screen of the output from the loading of the assembly. cls 
 # $me will never = 1, so It will run indefinately $me = 0 
    write-host "To End This Application, Close the Window"  
    Write-host ""
do

    # Prompt the user for the password
    $string = read-host "Please Enter User Password"
    # Encrypt the string and store it into the $encrypted variable
    $encrypted = Encrypt-String $string "MyStrongPassword"
    # Write result to the screen
    write-host "Encrypted Password is: $encrypted"
    write-host ""
  
    write-host "Testing Decryption of Password..."
      
    # Decrypts the string and stores the decrypted value in $decrypted
    $decrypted = Decrypt-String $encrypted "MyStrongPassword"
    # Writes the decrpted value to the screen
    write-host "Decrypted Password is: $decrypted"
    write-host ""

 while ($me -ne 1) 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值