Skip to content

Instantly share code, notes, and snippets.

@nzbart
Last active September 19, 2018 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzbart/a90d3080f595924e649b6d166228cb33 to your computer and use it in GitHub Desktop.
Save nzbart/a90d3080f595924e649b6d166228cb33 to your computer and use it in GitHub Desktop.
Encryption and decryption using public TLS certificate. This is useful if you want someone to encrypt data to send to you, and you have access to the private key for a public website.
$certPassword = Read-Host "Certificate password"
$fullCertBytes = <certificate>
$fullCert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$fullCert.Import($fullCertBytes, $certPassword, 0)
$encrypted = [Convert]::FromBase64String((Read-Host "Encrypted password"))
[System.Text.Encoding]::UTF8.GetString(($fullCert.PrivateKey.Decrypt($encrypted, $true)))
$r = [System.Net.HttpWebRequest]::Create(<public website HTTPS url>)
$r.GetResponse().Dispose()
$cert = $r.ServicePoint.Certificate
$password = [System.Text.Encoding]::UTF8.GetBytes((Read-Host "Please enter the text to encrypt"))
$out = [Convert]::ToBase64String((New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $cert).PublicKey.Key.Encrypt($password, $true))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment