如何在Bouncy城堡中使用RSA私鑰解開密鑰?我收到已使用RSA公鑰打包的已包裝密鑰。我有RSA密鑰對。我無法在C#Bouncy Castle中找到可用於解開它的api。如何在Bouncy城堡c#中使用RSA打開密鑰?
此代碼在C#源代碼(https://github.com/bcgit/bc-csharp)中目前已被註釋掉。對於RSA註釋掉線正是我所需要的,但是當我嘗試使用它們似乎它已刪除或沒有實施
Key key = cipher.unwrap(wrappedKey, "RSA", IBufferedCipher.PRIVATE_KEY);
上面的線是正是我需要的。它爲什麼被評論了?在WrapTest.cs完整的功能如下:
public ITestResult Perform()
{
try
{
// IBufferedCipher cipher = CipherUtilities.GetCipher("DES/ECB/PKCS5Padding");
IWrapper cipher = WrapperUtilities.GetWrapper("DES/ECB/PKCS5Padding");
IAsymmetricCipherKeyPairGenerator fact = GeneratorUtilities.GetKeyPairGenerator("RSA");
fact.Init(
new RsaKeyGenerationParameters(
BigInteger.ValueOf(0x10001),
new SecureRandom(),
512,
25));
AsymmetricCipherKeyPair keyPair = fact.GenerateKeyPair();
AsymmetricKeyParameter priKey = keyPair.Private;
AsymmetricKeyParameter pubKey = keyPair.Public;
byte[] priKeyBytes = PrivateKeyInfoFactory.CreatePrivateKeyInfo(priKey).GetDerEncoded();
CipherKeyGenerator keyGen = GeneratorUtilities.GetKeyGenerator("DES");
// Key wrapKey = keyGen.generateKey();
byte[] wrapKeyBytes = keyGen.GenerateKey();
KeyParameter wrapKey = new DesParameters(wrapKeyBytes);
// cipher.Init(IBufferedCipher.WRAP_MODE, wrapKey);
cipher.Init(true, wrapKey);
// byte[] wrappedKey = cipher.Wrap(priKey);
byte[] wrappedKey = cipher.Wrap(priKeyBytes, 0, priKeyBytes.Length);
// cipher.Init(IBufferedCipher.UNWRAP_MODE, wrapKey);
cipher.Init(false, wrapKey);
// Key key = cipher.unwrap(wrappedKey, "RSA", IBufferedCipher.PRIVATE_KEY);
byte[] unwrapped = cipher.Unwrap(wrappedKey, 0, wrappedKey.Length);
//if (!Arrays.AreEqual(priKey.getEncoded(), key.getEncoded()))
if (!Arrays.AreEqual(priKeyBytes, unwrapped))
{
return new SimpleTestResult(false, "Unwrapped key does not match");
}
return new SimpleTestResult(true, Name + ": Okay");
}
catch (Exception e)
{
return new SimpleTestResult(false, Name + ": exception - " + e.ToString());
}
}
你在說Java,對吧?這段代碼究竟在哪裏註釋掉了,你爲什麼依賴它?你可以在你的代碼中使用它。 –
您能否請包含更多代碼?目前我們甚至不知道「cipher」變量的類型。 @ArtjomB。好吧,那裏有'IBufferedCipher',感覺有點C#-ish。 –
@MaartenBodewes我已經用更好的解釋和更多的代碼更新了這個問題。 – minime