@@ -3,131 +3,139 @@ param(
33 [switch ]$Encrypt
44)
55
6- # ################
7- # Powershell Allows The Loading of .NET Assemblies
8- # Load the Security assembly to use with this script
9- # ################
10- [Reflection.Assembly ]::LoadWithPartialName(" System.Security" ) | Out-Null
11-
12- # ################
13- # This function is to Encrypt A String.
14- # $string is the string to encrypt, $passphrase is a second security "password" that has to be passed to decrypt.
15- # $salt is used during the generation of the crypto password to prevent password guessing.
16- # $init is used to compute the crypto hash -- a checksum of the encryption
17- # ################
18- Function Encrypt-File
19- {
20- param (
21- [string ] $path ,
22- [string ] $Passphrase ,
23- [string ] $outputPath ,
24- [string ] $salt = " SaltCrypto" ,
25- [string ] $init = " IV_Password"
26- )
27-
28- # Create a COM Object for RijndaelManaged Cryptography
29- $r = new-Object System.Security.Cryptography.RijndaelManaged
30- # Convert the Passphrase to UTF8 Bytes
31- $pass = [Text.Encoding ]::UTF8.GetBytes($Passphrase )
32- # Convert the Salt to UTF Bytes
33- $salt = [Text.Encoding ]::UTF8.GetBytes($salt )
34-
35- # Create the Encryption Key using the passphrase, salt and SHA1 algorithm at 256 bits
36- $r.Key = (new-Object Security.Cryptography.PasswordDeriveBytes $pass , $salt , " SHA1" , 5 ).GetBytes(32 ) # 256/8
37- # Create the Intersecting Vector Cryptology Hash with the init
38- $r.IV = (new-Object Security.Cryptography.SHA1Managed).ComputeHash( [Text.Encoding ]::UTF8.GetBytes($init ) )[0 .. 15 ]
39-
40- # Starts the New Encryption using the Key and IV
41- $c = $r.CreateEncryptor ()
42- # Creates a MemoryStream to do the encryption in
43- $ms = new-Object IO.MemoryStream
44- # Creates the new Cryptology Stream --> Outputs to $MS or Memory Stream
45- $cs = new-Object Security.Cryptography.CryptoStream $ms , $c , " Write"
46- # Starts the new Cryptology Stream
47- $fs = [IO.File ]::OpenRead($path )
6+ BEGIN {
7+
8+ # ################
9+ # Powershell Allows The Loading of .NET Assemblies
10+ # Load the Security assembly to use with this script
11+ # ################
12+ [Reflection.Assembly ]::LoadWithPartialName(" System.Security" ) | Out-Null
13+
14+ Function Get-Crypto (){
15+ param (
16+ [string ]$Passphrase ,
17+ [string ]$salt ,
18+ [string ]$init
19+ )
20+ # Create a COM Object for RijndaelManaged Cryptography
21+ $r = New-Object System.Security.Cryptography.RijndaelManaged
22+ # Convert the Passphrase to UTF8 Bytes
23+ $pass_ = [Text.Encoding ]::UTF8.GetBytes($Passphrase )
24+ # Convert the Salt to UTF Bytes
25+ $salt = [Text.Encoding ]::UTF8.GetBytes($salt )
26+
27+ # Create the Encryption Key using the passphrase, salt and SHA1 algorithm at 256 bits
28+
29+ $r.Key = (
30+ New-Object Security.Cryptography.PasswordDeriveBytes `
31+ - Arg @ ($pass_ , $salt_ , " SHA1" , 5 )
32+ ).GetBytes(32 ) # 256/8
33+
34+ # Create the Intersecting Vector Cryptology Hash with the init
35+ $r.IV = (
36+ New-Object Security.Cryptography.SHA1Managed).ComputeHash( [Text.Encoding ]::UTF8.GetBytes($init )
37+ )[0 .. 15 ]
38+
39+ Write-Output $r
40+ }
41+
42+ # ################
43+ # This function is to Encrypt A String.
44+ # $string is the string to encrypt, $passphrase is a second security "password" that has to be passed to decrypt.
45+ # $salt is used during the generation of the crypto password to prevent password guessing.
46+ # $init is used to compute the crypto hash -- a checksum of the encryption
47+ # ################
48+ Function Encrypt-File {
49+ param (
50+ [string ] $path ,
51+ [string ] $Passphrase ,
52+ [string ] $outputPath ,
53+ [string ] $salt = " SaltCrypto" ,
54+ [string ] $init = " IV_Password"
55+ )
56+
57+ $r = Get-Crypto - Passphrase $Passphrase - Salt $salt - Init $init
58+ $c = $r.CreateEncryptor ()
59+
60+ # Creates a MemoryStream to do the encryption in
61+ # Creates the new Cryptology Stream --> Outputs to $MS or Memory Stream
62+
63+ $ms = New-Object IO.MemoryStream
64+ $cs = New-Object Security.Cryptography.CryptoStream - Arg @ ($ms , $c , " Write" )
65+ $fs = [IO.File ]::OpenRead($path )
66+
4867 $fs.CopyTo ($cs );
68+
4969 $fs.Close ();
50- # Stops the Cryptology Stream
51- $cs .Close ()
52- # Stops writing to Memory
53- $ms .Close ()
54- # Clears the IV and HASH from memory to prevent memory read attacks
55- $r .Clear ()
56- # Takes the MemoryStream and puts it to an array
57- [byte []]$result = $ms.ToArray ()
70+ $cs .Close ()
71+ $ms .Close ()
72+
73+ # Clears the IV and HASH from memory to prevent memory read attacks
74+ $r .Clear ()
75+
76+ # Takes the MemoryStream and puts it to an array
77+ [byte []]$result = $ms.ToArray ()
5878
5979 $os = [IO.File ]::OpenWrite($outputPath );
6080 $os.Write ($result , 0 , $result.Length );
6181 $os.Close ();
62- }
82+ }
6383
64- Function Decrypt-File
65- {
66- param (
67- [string ] $path ,
68- [string ] $Passphrase ,
69- [string ] $OutputPath ,
70- [string ] $salt = " SaltCrypto" ,
71- [string ] $init = " IV_Password"
72- )
73-
74- # Create a COM Object for RijndaelManaged Cryptography
75- $r = new-Object System.Security.Cryptography.RijndaelManaged
76- # Convert the Passphrase to UTF8 Bytes
77- $pass = [Text.Encoding ]::UTF8.GetBytes($Passphrase )
78- # Convert the Salt to UTF Bytes
79- $salt = [Text.Encoding ]::UTF8.GetBytes($salt )
80-
81- # Create the Encryption Key using the passphrase, salt and SHA1 algorithm at 256 bits
82- $r.Key = (new-Object Security.Cryptography.PasswordDeriveBytes $pass , $salt , " SHA1" , 5 ).GetBytes(32 ) # 256/8
83- # Create the Intersecting Vector Cryptology Hash with the init
84- $r.IV = (new-Object Security.Cryptography.SHA1Managed).ComputeHash( [Text.Encoding ]::UTF8.GetBytes($init ) )[0 .. 15 ]
84+ Function Decrypt-File {
85+ param (
86+ [string ] $path ,
87+ [string ] $Passphrase ,
88+ [string ] $OutputPath ,
89+ [string ] $salt = " SaltCrypto" ,
90+ [string ] $init = " IV_Password"
91+ )
8592
86- $fs = [ IO.File ]::OpenRead( $path )
93+ $r = Get-Crypto - Passphrase $Passphrase - Salt $salt - Init $init
8794
88- # Create a new Decryptor
89- $d = $r.CreateDecryptor ()
90- # Create a New memory stream with the encrypted value.
95+ # Create a new Decryptor
96+ $d = $r.CreateDecryptor ()
9197
92- # Read the new memory stream and read it in the cryptology stream
93- $cs = new-Object Security.Cryptography.CryptoStream $fs , $d , " Read"
94- # Read the new decrypted stream
98+ # Create a New memory stream with the encrypted value.
99+ # Read the new memory stream and read it in the cryptology stream
95100
96- # Return from the function the stream
101+ $fs = [IO.File ]::OpenRead($path )
102+ $cs = New-Object Security.Cryptography.CryptoStream - Arg @ ($fs , $d , " Read" )
103+ $os = [IO.File ]::Open($outputPath , [IO.FileMode ]::Truncate, [IO.FileAccess ]::Write)
97104
98- $os = [IO.File ]::Open($outputPath , [IO.FileMode ]::Truncate, [IO.FileAccess ]::Write);
99105 $cs.CopyTo ($os );
100106
101107 $os.Close ();
102- # Stops the crypology stream
103- $cs.Close ()
104-
105- # Stops the memory stream
106- $fs.Close ()
107- # Clears the RijndaelManaged Cryptology IV and Key
108- $r.Clear ()
109- }
110-
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"
108+ $cs.Close ()
109+ $fs.Close ()
116110
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
111+ # Clears the RijndaelManaged Cryptology IV and Key
112+ $r.Clear ()
113+ }
124114}
125115
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
116+ PROCESS {
117+
118+ $SRC_DIR = (Resolve-Path - Path (
119+ Join-Path - Path $PSScriptRoot - ChildPath " .." )).Path
120+
121+ $STRONG_NAME_PLAINTEXT_KEY_PATH = " $ ( $SRC_DIR ) /src/jmespath.net.snk"
122+ $STRONG_NAME_ENCRYPTED_KEY_PATH = " $ ( $SRC_DIR ) /src/jmespath.net.snk.crypted"
123+
124+ if ($Encrypt.IsPresent ) {
125+ Encrypt- File `
126+ - path $STRONG_NAME_PLAINTEXT_KEY_PATH `
127+ - Passphrase $env: SNK_PASSPHRASE `
128+ - salt $env: SNK_SALT `
129+ - init $env: SNK_INIT `
130+ - outputPath $STRONG_NAME_ENCRYPTED_KEY_PATH
131+ }
132+
133+ else {
134+ Decrypt- File `
135+ - path $STRONG_NAME_ENCRYPTED_KEY_PATH `
136+ - Passphrase $env: SNK_PASSPHRASE `
137+ - salt $env: SNK_SALT `
138+ - init $env: SNK_INIT `
139+ - outputPath $STRONG_NAME_PLAINTEXT_KEY_PATH
140+ }
133141}
0 commit comments