1+ [CmdletBinding ()]
2+ param (
3+ [switch ]$Encrypt
4+ )
5+
16# ################
27# Powershell Allows The Loading of .NET Assemblies
3- # Load the Security assembly to use with this script
8+ # Load the Security assembly to use with this script
49# ################
5- [Reflection.Assembly ]::LoadWithPartialName(" System.Security" )
10+ [Reflection.Assembly ]::LoadWithPartialName(" System.Security" ) | Out-Null
611
712# ################
813# This function is to Encrypt A String.
914# $string is the string to encrypt, $passphrase is a second security "password" that has to be passed to decrypt.
1015# $salt is used during the generation of the crypto password to prevent password guessing.
1116# $init is used to compute the crypto hash -- a checksum of the encryption
1217# ################
13- function Encrypt-File ( $path , $Passphrase , $salt = " SaltCrypto " , $init = " IV_Password " , $outputPath )
18+ Function Encrypt-File
1419{
20+ param (
21+ [string ] $path ,
22+ [string ] $Passphrase ,
23+ [string ] $outputPath ,
24+ [string ] $salt = " SaltCrypto" ,
25+ [string ] $init = " IV_Password"
26+ )
27+
1528 # Create a COM Object for RijndaelManaged Cryptography
1629 $r = new-Object System.Security.Cryptography.RijndaelManaged
1730 # Convert the Passphrase to UTF8 Bytes
@@ -23,8 +36,8 @@ function Encrypt-File($path, $Passphrase, $salt="SaltCrypto", $init="IV_Password
2336 $r.Key = (new-Object Security.Cryptography.PasswordDeriveBytes $pass , $salt , " SHA1" , 5 ).GetBytes(32 ) # 256/8
2437 # Create the Intersecting Vector Cryptology Hash with the init
2538 $r.IV = (new-Object Security.Cryptography.SHA1Managed).ComputeHash( [Text.Encoding ]::UTF8.GetBytes($init ) )[0 .. 15 ]
26-
27- # Starts the New Encryption using the Key and IV
39+
40+ # Starts the New Encryption using the Key and IV
2841 $c = $r.CreateEncryptor ()
2942 # Creates a MemoryStream to do the encryption in
3043 $ms = new-Object IO.MemoryStream
@@ -48,8 +61,16 @@ function Encrypt-File($path, $Passphrase, $salt="SaltCrypto", $init="IV_Password
4861 $os.Close ();
4962}
5063
51- function Decrypt-File ( $path , $Passphrase , $salt = " SaltCrypto " , $init = " IV_Password " , $OutputPath )
64+ Function Decrypt-File
5265{
66+ param (
67+ [string ] $path ,
68+ [string ] $Passphrase ,
69+ [string ] $OutputPath ,
70+ [string ] $salt = " SaltCrypto" ,
71+ [string ] $init = " IV_Password"
72+ )
73+
5374 # Create a COM Object for RijndaelManaged Cryptography
5475 $r = new-Object System.Security.Cryptography.RijndaelManaged
5576 # Convert the Passphrase to UTF8 Bytes
@@ -63,33 +84,50 @@ function Decrypt-File($path, $Passphrase, $salt="SaltCrypto", $init="IV_Password
6384 $r.IV = (new-Object Security.Cryptography.SHA1Managed).ComputeHash( [Text.Encoding ]::UTF8.GetBytes($init ) )[0 .. 15 ]
6485
6586 $fs = [IO.File ]::OpenRead($path )
66-
87+
6788 # Create a new Decryptor
6889 $d = $r.CreateDecryptor ()
6990 # Create a New memory stream with the encrypted value.
70-
91+
7192 # Read the new memory stream and read it in the cryptology stream
7293 $cs = new-Object Security.Cryptography.CryptoStream $fs , $d , " Read"
7394 # Read the new decrypted stream
7495
7596 # Return from the function the stream
76-
97+
7798 $os = [IO.File ]::Open($outputPath , [IO.FileMode ]::Truncate, [IO.FileAccess ]::Write);
7899 $cs.CopyTo ($os );
79100
80101 $os.Close ();
81102 # Stops the crypology stream
82103 $cs.Close ()
83-
104+
84105 # Stops the memory stream
85106 $fs.Close ()
86107 # Clears the RijndaelManaged Cryptology IV and Key
87108 $r.Clear ()
88109}
89110
90- Decrypt- File `
91- - path " C:\projects\jmespath-net\src\jmespath.net.snk.crypted" `
92- - Passphrase $env: SNK_PASSPHRASE `
93- - salt $env: SNK_SALT `
94- - init $env: SNK_INIT `
95- - outputPath " C:\projects\jmespath-net\src\jmespath.net.snk"
111+ $SRC_DIR = (Resolve-Path - Path (
112+ Join-Path - Path $PSScriptRoot - ChildPath " .." )).Path
113+
114+ $STRONG_NAME_PLAINTEXT_KEY_PATH = " $ ( $SRC_DIR ) /src/jmespath.net.snk"
115+ $STRONG_NAME_ENCRYPTED_KEY_PATH = " $ ( $SRC_DIR ) /src/jmespath.net.snk.crypted"
116+
117+ if ($Encrypt.IsPresent ) {
118+ Encrypt- File `
119+ - path $STRONG_NAME_PLAINTEXT_KEY_PATH `
120+ - Passphrase $env: SNK_PASSPHRASE `
121+ - salt $env: SNK_SALT `
122+ - init $env: SNK_INIT `
123+ - outputPath $STRONG_NAME_ENCRYPTED_KEY_PATH
124+ }
125+
126+ else {
127+ Decrypt- File `
128+ - path $STRONG_NAME_ENCRYPTED_KEY_PATH `
129+ - Passphrase $env: SNK_PASSPHRASE `
130+ - salt $env: SNK_SALT `
131+ - init $env: SNK_INIT `
132+ - outputPath $STRONG_NAME_PLAINTEXT_KEY_PATH
133+ }
0 commit comments