我在玩加密。我被困在用BouncyCastle加载密钥上。密钥是由PuTTYgen(SSH-2 RSA 4096位)生成的。也许有其他方法可以加载它吗?
PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: rsa-key-20160508
Public-Lines: 12
我得到:
“System.IO”类型的未处理异常。BouncyCastle.Crypto中出现“IOException”。dll<br>其他信息:意外的内容结束标记“”
const string pubkey = @"AAAAB3NzaC1yc2EAAAABJQAAAgEAlX+AsmDF7FoR7D2jWYJTKLD6ZH6FAY9v2vPa
5+ICSQCydphiUbiJwsb3sl4yJIB6uOw4fM+22F0U/P1pxdGqUabqixNOC5X9xBzI
KbMt2xcYof/JCaNskHE2teguxdCeLlQ30QIxTRD76y6TqISS+anTtpMiGFs/LNOX
yDp7H3S5OuvRjoTRLoocEwIFNbpnkq8D+RVSdV1kkqVSMwzwRgBRRCkBfZAYW9VY
qlWNwbLCiGw3qi5mu2qorxqqF0tO2l0gLFNYdbnf9+bKavqiV7wMn3mVbcFtRhe/
29klqxos6V8Of71Q7IShFz9IXq5KdRdJoGyNJrNXKoF87UCPppa4lmOlfY40E/c5
w/LMjf63c/xx8ji7v6hY0sHt7XdWUB3V1XBPScypk7PxNS13/RtdzjeOn/8W9tcV
tqX6b7XFU/AwIrkBDa47JGnuOJnmzmMzHDPKeZwaEQT4BZRzUa6yKq3EZCLymkBB
W9lRQkKY3qg8OJX+FhAQ7c5pCOl3PFmFjuDMbRWTaF/nJj/qPfvND+w7a86fxUUC
HPokcYGLVZUBWTfnx3q/StbjaQBQDPMxogLh/yCZywHFqO0xGaA8SQAM2Vyd7r7l
waPQhFIJhVrXuaz0Ok+TE5jqe5yTB0gZ8pcVDq/GWRXBAUwXu50UeZBurcGHEXk5
HhFdFlk=";
const string prikey = @"...";
var publickey = Convert.FromBase64String(pubkey);
//var privatekey = Convert.FromBase64String(prikey);
Org.BouncyCastle.Crypto.AsymmetricKeyParameter publicAsymmetricKey = Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(publickey);
//Org.BouncyCastle.Crypto.AsymmetricKeyParameter privateAsymmetricKey = Org.BouncyCastle.Security.PrivateKeyFactory.CreateKey(privatekey);
已添加:我使用“ssh-keygen -t rsa -b 4096”生成了一个新密钥,并尝试使用pemreader加载它:
const string pubkey = @"AAAAB3NzaC1yc2EAAAADAQABAAACAQDGzR88DIxKRUvEJDAa0zcRq0O39M5i9vwkb9r+mPi56eYPEZJfMcZZmrtMoBoCJM3g0CmdHHktanRnjwXI+Gw6zRIbe3Lnz8rq+IyCDQnJQzHMlFJkwt7tuFzLWwNoWL5bWN0+Y6h8pB6KudPxsutQfNVTAyI00JhhhMk1T0FSF82BHP+HKI+lsGrTaBhDHnbZryk5Bf00sAJLVvS5kE7LOthVAWHGKZbV/PJnDcXQN8Hxto9mWy0iELTsUYzwf1VfIvB1Ugyv/rs3Vn5A98p2BaXidwYrgc3NhKliESQyl9KYggF3ody99zlMzWQfdmpfJEBzjxjNIMaD/NFBgTLHSbEzyXDVLcvCocmNaJeEQ4uGcqqw2Oe7d9RDZikllttLCtPQSQ49N/1JI5CRHEn9bz7RNX4aZWPbzw1l5y6fcsTXcPsVHb+bGmhcobSbWZvAnLMrHA0FI+47yNrBlp+qXBXfxO3/sSK3yU1lJEUosmfb3nPvvt0bt0TlwLYC12gMLk1Z0ZAoHxR7qw7la/HoZXhQqqi57RdDWbHiX6zxFPDVMd48fc8m9Wnwv2+AIcBRsErnL4YWT/4Pb57+ehg+HbKy5cCgo6wsHYB9IjoXIfNQAz2dAbXHr2C4zo0BKlabZSqv+ZoTBP5NwSZItMeRv1eCreg7ejJ5HR8NIJaAZQ==";
var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(new StringReader(pubkey));
// KeyParameter is still null
var KeyParameter = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pemReader.ReadObject();
添加:
我可以生成一个公钥,也可以加载一个未加密的私钥(https://rietta.com/blog/2012/01/27/openssl-generating-rsa-key-from-command/)
openssl genrsa-des3-out是私有的。pem 4096
openssl rsa-私有。pem-输出pem-发布-输出public.pem
OpenSSL RSA-in private . PEM-out private _ encrypted . PEM-out form PEM
var publickey = Convert.FromBase64String(pubkey);
var privatekey = Convert.FromBase64String(prikey);
var asnprivate = Org.BouncyCastle.Asn1.Asn1Object.FromStream(new MemoryStream(privatekey));
var privStruct = new Org.BouncyCastle.Asn1.Pkcs.RsaPrivateKeyStructure((Org.BouncyCastle.Asn1.Asn1Sequence)asnprivate);
AsymmetricKeyParameter publicAsymmetricKey = PublicKeyFactory.CreateKey(publickey);
但是我无法加载加密的私钥...
有了openssl PEM,我能够让它工作。我继续寻找如何加载受保护的私钥。
感谢您的所有输入。
using System;
using System.IO;
using System.Text;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1;
namespace EncryptTest1
{
class Program
{
static void Main(string[] args)
{
const string pubkey = @"MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAuQhcZKNnIC2bvJhI5Yzq
oH9uGAHuiAt9bOXdIrsaeEwsIUMuyNfXkLozwlcJb0YLgqhpP3gNLb58WCb1AvkV
ZHgzLQbay93HwmBiEPW6LZIKGsHwh1awBmJpeWhU8h3oNP+KpNq44H+jAeM9G9En
W0y7+AUbe3pnisOWVZHmM72A2mLq1piCOjmvjegzgB8/rU7ZkRrNadeFO7DBP0Ew
6eElS3iXfFHJmbK+L5lL0pFxvUMRfGVC33NQkuSoWiQJZsYyUgIk6WSOQm3t5fw6
VESMTfd1xKlaBGp7IrWksthEF+XAu5ziAcSdjBwJLEFzJ2830cQ4GhFS3JtU/Iao
6MbJkUuaHoI3Xx9C7d6RiKbK1uZrOHjyVsUhqpYwyAUdrViraj0pybxbE2GwbRC4
/NyHuPKG3gBULiGYbl8wwlhl71nRVdkPQtGw36kmGWHRk3ERM6JgLWsDZjED4ak2
Na6VzI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QJVNxHeo7
ZlRul6vFgS3uaaEyaPXrG5pFRPeLlqllJxQ5Z1mQtqaK9U0dSCykxW6zaz3d8YXE
BIe/0Vc1/0d80lkJk/3SnLGIAiC2C9dpmA6RmgkGa0cyD5QPmXmYXHFT+tA7S/lV
W+5C6ZO0aaOmO+F+W82ZJ92FRd8CITasoPO9zxalJBEGD/s5ehnRhhizLqjY";
var publickey = Convert.FromBase64String(pubkey);
var privatekey = Convert.FromBase64String(prikey);
var asnprivate = Asn1Object.FromStream(new MemoryStream(privatekey));
var privStruct = new RsaPrivateKeyStructure((Asn1Sequence)asnprivate);
RsaKeyParameters privateAsymmetricKey = new RsaKeyParameters(true, privStruct.Modulus, privStruct.PrivateExponent);
RsaKeyParameters publicAsymmetricKey = (RsaKeyParameters)PublicKeyFactory.CreateKey(publickey);
var inputBytes = Encoding.UTF8.GetBytes("the message");
Console.WriteLine("--- Message: ----");
Console.WriteLine(Encoding.UTF8.GetString(inputBytes));
IAsymmetricBlockCipher cipher = new RsaEngine();
cipher.Init(true, publicAsymmetricKey);
var cipheredBytes = cipher.ProcessBlock(inputBytes, 0, inputBytes.Length);
Console.WriteLine("--- Enc utf8: ----");
Console.WriteLine(Encoding.UTF8.GetString(cipheredBytes));
Console.WriteLine("--- Enc Base64: ----");
Console.WriteLine(Convert.ToBase64String(cipheredBytes));
cipher.Init(false, privateAsymmetricKey);
var deciphered = cipher.ProcessBlock(cipheredBytes, 0, cipheredBytes.Length);
Console.WriteLine("--- Dec: ----");
Console.WriteLine(Encoding.UTF8.GetString(deciphered));
Console.ReadLine();
}
}
}
油灰。ppk密钥格式是PuTTY专有的。
我很确定Bouncy castle中不支持.ppk文件。
您需要将您的密钥转换为OpenSSH格式。为此,请参阅如何将使用PuttyGen(Windows)生成的SSH密钥对转换为ssh-agent和KeyChain(Linux)使用的密钥对。
有人能解释一下为什么这段代码在解密密钥时会在最后一行抛出吗? 以下是来自https://stackoverflow.com/a/27886397/66722对于使用OAEP的RSA也是如此? “RSA/ECB/PKCS1Padding”实际上没有实现ECB模式加密。它应该被称为“RSA/None/PKCS1Padding”,因为它只能用于加密单个明文块(或者实际上是一个密钥)。这只是Sun/Ora
我需要用Java中的Bouncy Castle创建一个自签名X509证书,但我尝试包含的每个类都不推荐使用。我该怎么解决这个问题?还有其他课程吗?谢谢
我正在调试bouncy castle 1.47的一个问题。我可以找到“bcprov”的调试jar,但无法找到{org.bouncycastle:bcpkix-jdk15on:1.47:jar}的调试jar。 还有其他地方可以下载bcpkix-jdk15on-1.47吗。包含调试信息的jar? 或 是否有一种工具可以从没有行号的jar(包含. class文件)生成行号,并且还可以为相同生成的jar生
我的项目正在对来自某些第三方软件的某些数据集进行签名验证。使用的签名算法是 。当我使用SDK附带的标准SUN加密提供程序时,一切都很顺利。最近我切换到了Bouncy Castle 1.50,之后,一些以前(即SUN提供者)进行验证的数据集开始失败,而其余的仍然被验证正常。 我探索了两个提供程序的源代码,结果发现SDK的默认提供程序对格式错误的签名有某种保护(同时能够恢复),而Bouncy Cast
我正在开发一个功能来对某些内容进行数字签名。我有一个带有私钥的有效证书。如何使用私钥和充气城堡进行数字签名? 我尝试了以下方法,但想要一些正确的方法来实现同样的使用充气城堡: 谢谢!
问题内容: 任何人都可以向我展示(或提供指向)如何使用充气城堡在Java中加密文件的示例吗?我查看了bouncycastle.org,但找不到其API的任何文档。即使只是知道要使用哪些类,也对我入门很有帮助! 问题答案: 您要执行哪种类型的加密?基于密码(PBE),对称,不对称?这就是您配置Cipher的全部方法。 您不必使用任何BouncyCastle特定的API,只需使用它提供的算法即可。这是