Creating a Key File and Password File
With PowerShell, we can generate a 256-bit AES encryption key:
Creating the AES.key
1 2 3 4 |
$KeyFile = "C:\o365\AES.key" $Key = New-Object Byte[] 32 # You can use 16, 24, or 32 for AES [Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key) $Key | out-file $KeyFile |
Creating the password file
1 2 3 4 5 |
$PasswordFile = "Password.txt" $KeyFile = "AES.key" $Key = Get-Content $KeyFile $Password = "P@ssword1" | ConvertTo-SecureString -AsPlainText -Force $Password | ConvertFrom-SecureString -key $Key | Out-File $PasswordFile |
Using the key and password file
1 |
$User = "username"$PasswordFile = "c:\o365\Password.txt"$KeyFile = "c:\o365\AES.key"$key = Get-Content $KeyFile$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential ` -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key) |