PowerShell
PowerShell
RSA Sign with PKCS8 Encrypted Key
See more RSA Examples
Demonstrates how to load a private key from an encrypted PKCS8 file and create an RSA digital signature (and then verify it).Chilkat PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $false
# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
$privKey = New-Object Chilkat.PrivateKey
# Load the private key from an RSA PEM file:
$success = $privKey.LoadAnyFormatFile("raul_privateKey.key","a0123456789")
if ($success -eq $false) {
$($privKey.LastErrorText)
exit
}
$rsa = New-Object Chilkat.Rsa
# Import the private key into the RSA component:
$success = $rsa.UsePrivateKey($privKey)
if ($success -eq $false) {
$($rsa.LastErrorText)
exit
}
# This example will sign a string, and receive the signature
# in a hex-encoded string. Therefore, set the encoding mode
# to "hex":
$rsa.EncodingMode = "hex"
$strData = "This is the string to be signed."
# Sign the string using the sha256 hash algorithm.
# Other valid choices are sha1, sha384, sha512 and others.
$hexSig = $rsa.SignStringENC($strData,"sha256")
if ($rsa.LastMethodSuccess -eq $false) {
$($rsa.LastErrorText)
exit
}
$($hexSig)
# Now verify with the public key.
# This example shows how to use the public key from
# a digital certificate (.cer file)
$cert = New-Object Chilkat.Cert
$success = $cert.LoadFromFile("raul_publicKey.cer")
if ($success -eq $false) {
$($cert.LastErrorText)
exit
}
$pubKey = New-Object Chilkat.PublicKey
$cert.GetPublicKey($pubKey)
$rsa2 = New-Object Chilkat.Rsa
$success = $rsa2.UsePublicKey($pubKey)
if ($success -eq $false) {
$($rsa2.LastErrorText)
exit
}
# Verify the signature against the original data:
$rsa2.EncodingMode = "hex"
$success = $rsa2.VerifyStringENC($strData,"sha256",$hexSig)
if ($success -eq $false) {
$($rsa2.LastErrorText)
exit
}
$("Signature verified!")
# Verify with incorrect data:
$success = $rsa2.VerifyStringENC("something else","sha256",$hexSig)
if ($success -ne $true) {
$("Signature not verified! (which was expected in this case)")
}
else {
$("Hmmm... that's not right...")
}