设为首页 收藏本站
查看: 892|回复: 0

Powershell 之加解密

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-11-29 08:46:43 | 显示全部楼层 |阅读模式
#################
# 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-306937-1-1.html 上篇帖子: Powershell 之管理Linux服务器 下篇帖子: powershell 之数据库操作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表