A module to encrypt and decrypt a using public & public keys respectively.
This PowerShell contains two functions, one to encrypt a string using a public key that is provided, and another which decrypts the string using a certificate containing the the private key installed in the keystore.
I am using openssl to do this in wsl (for convenience).
openssl req -x509 -newkey rsa:4096 -sha256 -keyout private.key -out public.crt -subj "/CN=SubjectName" -days 1095
openssl pkcs12 -export -name "Certificate Purpose" -out Private.pfx -inkey private.key -in public.crt
Run each command in turn (you cannot copy/paste both lines together as the first one requests input).
- You must specify a PEM pass phrase when creating the key and cert in step 1.
- You must use that same PEM pass phrase for the private key in step 2.
- It is not necessary, but highly recommended to specify an export password in step 2, this prevents the private certificate from being imported into a certificate store without knowing the password.
mark@WSL:/cert$ openssl req -x509 -newkey rsa:4096 -sha256 -keyout private.key -out public.crt -subj "/CN=SubjectName" -days 1095
Generating a RSA private key
.......................................................................................++++
......................................................................................................................................++++
writing new private key to 'private.key'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
mark@WSL:/cert$ openssl pkcs12 -export -name "Certificate Purpose" -out Private.pfx -inkey private.key -in public.crt
Enter pass phrase for private.key:
Enter Export Password:
Verifying - Enter Export Password:
mark@WSL:/cert$
In Windows, open the 'Private.pfx' file. Follow the prompts to import the certificate. By default, it will import into the personal certificate store.
You can check that the certificate is installed in PowerShell:
Get-ChildItem -Path Cert:\CurrentUser\My
-
Read the private key:
$PublicKey = (Get-Content -Path ".\public.crt") -join ""
or:
$PublicKey = @" -----BEGIN CERTIFICATE----- MIIFDTCCAvWgAwIBAgIUOedp0TYx26zAByIpfukrhz3Hy+swDQYJKoZIhvcNAQEL BQAwFjEUMBIGA1UEAwwLU3ViamVjdE5hbWUwHhcNMjEwMjI3MjAzMjE4WhcNMjQw MjI3MjAzMjE4WjAWMRQwEgYDVQQDDAtTdWJqZWN0TmFtZTCCAiIwDQYJKoZIhvcN AQEBBQADggIPADCCAgoCggIBAKVfQAy04KT7M1MyCJGTtbD7UL7AwEpoRPDcgTdD vqr+iSr64VBclB1dUU1XtYlSNBFk0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TOe1McGHII92rPPFpB7f2KRhXb06P13FHVg H6UY6++JzMIg/Ugm0QCf1fLOYTyUl8JvLK30PFum5SBKB0ilruuxpEQUvZx9sGft TKjNiefcvwxUovPA80Xb4FLQb5WXzzuya5G7J95bmnICS5nz29Zkw/+iw8+xz0qS 0j3P6p/hZzgoPGi9heSjpjWPQJo+twTZ1iN2kAbLtWK2mKyF/CFErhI+/RJmxg6Z 7BLvavYZFUDYPxSME22bnUWgFScJToJp+rNkUKCSzQKcZI2f3z1nQ71lhTEy4STz tim6MyZD/JKnGe7keg6+k8JgVKAgXKPLadjf7Vnq49DHDixTDBTV/QCvxO0BTEXY vw== -----END CERTIFICATE----- "@
-
Use the public key to encrypt a string:
"This is a test string." | ConvertTo-EncryptedString -PublicKey $PublicKey
-
Ensure that the certificate with the private key is imported into the certificate store. On Windows this can be the personal or the computer store.
"MIIGBgYJKoZIhvcNAQcDoIIF9zCCBfMCAQAxggJKMIICRgIBADAuMBYxFDASBgNVBAMMC1N1YmplY3ROYW1lAhQ552nRNjHbrMAHIil+6SuHPcfL6zANBgkqhkiG9w0BAQEFAASCAgAC03RtKDtfazazJLfi2vWRGgrYT1GC8r7O0Qv8SUUIbsdU7dq9FPR20OnRsVsA7KaEV62rY8prQO1XyMUBjjsW+mtBQB3JBuX9Srf3ijaGqHc2xpJ/Pg5dlvrcxOekAd33LqON2TSBaLyCNBipcvbKMe7T05yjG+FXyiOHWqCPGQlPBJBto7mrNxhRjqd0BEhMIIRp9IAjUKLYMusOBl6G3aOTLgAIVutz66+lbslMGFkOAarhm6bSjgjfrPVLSIBjy8yIYYCqoGicQpY3J1PWRKIMECQfpKdYiXmdLqlbtLEMtDfAFiDGl0nRjpqS6IDEbdOzuSXG0vEWHSJMqZm9KZOWp28BTHyvRvA8wtbwVWzICz9I9uzGG/0SkjHg3qJWF9gblhVR/s3a7ZPUOpmE0sQg4cm+4kFp2fBq8srKn/uwLQDaVX0O0c+HP5aIXWZJOc6WL9s3IsMPaMzh1IUX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y+6S2ySj9vbTIOcsDT8ew+JDu3tFpyQdZhOaNt/w8zUcMwHHdJBEVis37temuDPMjRaoIDNRme+KSVWKLLS370jWATGNpijH3q+So/7qWO2kYNWyfosoiVkWsmel+wO18AlSLe11qwU1RozEOLy9jOwLyh45eLwVPe65CuUrkmalJxSrjqZMkcRoJlN2gAZdsBF/lhQO8J8de/ZYgbgFHVEznO/rNhcM20cz9NU9MgNrYUoeCSiBFqBi+6jEum692tQeArxl/MfFEKg24KtMEtXvIm4exv/OuRsZZOvZ8PV+7IrFlfc9BDK82fjOh7cThpxrkSaLFJLjG/VZViLJlYCzZmsqvzQlox6tjx/WzC8bzV/e9tGg==" | ConvertFrom-EncryptedString
The 'examples' folder contains a private certificate, public certificate and the key generated using the methodology in Generate a Public & Private Key. The password to the private key and the certificate in the examples folder is 'test'.
The script examples.ps1 encrypts some example text (Laurem Ipsom) using the public key and decrypts it using the private key (assuming it has been imported).
Please see module help documentation or use the built in help.