Wednesday, October 31, 2007

PowerShell Script for AES Key Generation

I have to constantly generate AES keys for the numerous SSO requests that we receive from our clients.  The keys are used for message level security, and they're really the biggest headache we have when it comes to setting up SSO for a new client.  Everything after that is a breeze (a simple database entry).

I used to use one of the unit tests that exercises our cryptography code for this task.  I would set a break point where the AES algorithm was instantiated and then inspect the value of the Key property.  However, a short PowerShell function has now made this much easier.

   1:  function GenerateAesKey() {
   2:     $algorithm = [System.Security.Cryptography.SymmetricAlgorithm]::Create("Rijndael")
   3:     $keybytes = $algorithm.get_Key()
   4:     $key = [System.Convert]::ToBase64String($keybytes)
   5:     Write-Output $key
   6:  }

No comments: