Jas*_*ngh 6 windows powershell command-line rsa certificate
我想在不使用外部软件的情况下在 powershell 中生成一个 RSA 公钥对,我想测试它。它应该能够加密/解密任何在线公钥/私钥验证服务上的数据。
目的-严格教育。我非常清楚您不应该出于安全目的在线导出您的私钥。
到目前为止,我已经尝试过 ssh-keygen和
$RSA = New-Object System.Security.Cryptography.RSACryptoServiceProvider(2048)
[System.Convert]::ToBase64String($rsa.ExportCspBlob(1))
[System.Convert]::ToBase64String($rsa.ExportCspBlob(0))
Run Code Online (Sandbox Code Playgroud)
System.Security.Cryptography.RSACryptoServiceProvider 创建 P、Q 等所有用于计算公钥/私钥的原材料,但我不想要原材料。
ExportCspBlob(x) 提供了一个密钥,但是当我尝试在线验证时,密钥对验证失败。
那么,有没有什么方法可以在不使用任何外部程序的情况下在powershell中创建RSA公钥私钥对,可以直接复制粘贴到证书格式中(带有-----BEGIN PRIVATE KEY----内容的那种) ?
如果您只想使用 powershell 实现公钥加密/解密,可以使用内置工具。要生成密钥对,只需使用 New-SelfSignedCertificate cmdlet,然后您可以使用生成的证书使用 Protect/Unprotect-CmsMessage 来加密/解密数据(这是类似 PGP 的 cmdlet,这意味着您不必自己处理对称密钥部分) 。然后,要将密钥共享或移动到其他计算机,您可以使用导入/导出证书 cmdlet。请参阅下面的示例
$store = "cert:\CurrentUser\My"
$params = @{
CertStoreLocation = $store
Subject = "CN=Test1"
KeyLength = 2048
KeyAlgorithm = "RSA"
KeyUsage = "DataEncipherment"
Type = "DocumentEncryptionCert"
}
# generate new certificate and add it to certificate store
$cert = New-SelfSignedCertificate @params
# list all certs
# Get-ChildItem -path $store
# Encryption / Decryption
$message = "My secret message"
$cipher = $message | Protect-CmsMessage -To "CN=Test1"
Write-Host "Cipher:" -ForegroundColor Green
$cipher
Write-Host "Decrypted message:" -ForegroundColor Green
$cipher | Unprotect-CmsMessage
# Exporting/Importing certificate
$pwd = ("P@ssword" | ConvertTo-SecureString -AsPlainText -Force)
$privateKey = "$home\Documents\Test1.pfx"
$publicKey = "$home\Documents\Test1.cer"
# Export private key as PFX certificate, to use those Keys on different machine/user
Export-PfxCertificate -FilePath $privateKey -Cert $cert -Password $pwd
# Export Public key, to share with other users
Export-Certificate -FilePath $publicKey -Cert $cert
#Remove certificate from store
$cert | Remove-Item
# Add them back:
# Add private key on your machine
Import-PfxCertificate -FilePath $privateKey -CertStoreLocation $store -Password $pwd
# This is for other users (so they can send you encrypted messages)
Import-Certificate -FilePath $publicKey -CertStoreLocation $store
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9813 次 |
| 最近记录: |