Skip to content

Instantly share code, notes, and snippets.

@lennybacon
Last active April 29, 2024 11:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lennybacon/9fcce97f84d1b760e8da0e9d0738536a to your computer and use it in GitHub Desktop.
Save lennybacon/9fcce97f84d1b760e8da0e9d0738536a to your computer and use it in GitHub Desktop.
Generate a new AES 256 Key with PowerShell
$random = [System.Security.Cryptography.RandomNumberGenerator]::Create();
$buffer = New-Object byte[] 32;
$random.GetBytes($buffer);
[BitConverter]::ToString($buffer).Replace("-", [string]::Empty);
@gajjardarshithasmukhbhai

@lennybacon I added the some steps to run the above scripts for various OS:

Windows:

  • Open a PowerShell terminal (Command Prompt or PowerShell).
  • Copy and paste the script into the terminal.
  • Press Enter to run the script.

MacOS:

  • Open a terminal (you can find it in Applications > Utilities > Terminal).
  • Use the pwsh command (PowerShell Core) or powershell (if installed) to run the script. For example:
pwsh -File path/to/your/script.ps1

Linux:

  • Open a terminal.
  • Use the pwsh command (PowerShell Core) to run the script. For example:
pwsh -File path/to/your/script.ps1

@MONTANA241
Copy link

What should I do if I need several AES 256 keys at once? Thks!

@lennybacon
Copy link
Author

What should I do if I need several AES 256 keys at once? Thks!

Put it in a for-Loop?

$random = [System.Security.Cryptography.RandomNumberGenerator]::Create();

for($i=0; $i -lt 10; $i++){
  $buffer = New-Object byte[] 32;
  $random.GetBytes($buffer);
  $key = [BitConverter]::ToString($buffer).Replace("-", [string]::Empty);
   Write-Host "Key $($i): $($key)"
 }

@MONTANA241
Copy link

What should I do if I need several AES 256 keys at once? Thks!

Put it in a for-Loop?

$random = [System.Security.Cryptography.RandomNumberGenerator]::Create();

for($i=0; $i -lt 10; $i++){
  $buffer = New-Object byte[] 32;
  $random.GetBytes($buffer);
  $key = [BitConverter]::ToString($buffer).Replace("-", [string]::Empty);
   Write-Host "Key $($i): $($key)"
 }

Thanks a lot!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment