blob: 2556a947f64599cf9b11d70279a4117f7ad8c31c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
using System;
using System.IO;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.IO;
namespace Org.BouncyCastle.Pkcs
{
public class Pkcs8EncryptedPrivateKeyInfoBuilder
{
private PrivateKeyInfo privateKeyInfo;
public Pkcs8EncryptedPrivateKeyInfoBuilder(byte[] privateKeyInfo): this(PrivateKeyInfo.GetInstance(privateKeyInfo))
{
}
public Pkcs8EncryptedPrivateKeyInfoBuilder(PrivateKeyInfo privateKeyInfo)
{
this.privateKeyInfo = privateKeyInfo;
}
/// <summary>
/// Create the encrypted private key info using the passed in encryptor.
/// </summary>
/// <param name="encryptor">The encryptor to use.</param>
/// <returns>An encrypted private key info containing the original private key info.</returns>
public Pkcs8EncryptedPrivateKeyInfo Build(
ICipherBuilder encryptor)
{
try
{
MemoryStream bOut = new MemoryOutputStream();
ICipher cOut = encryptor.BuildCipher(bOut);
byte[] keyData = privateKeyInfo.GetEncoded();
using (var str = cOut.Stream)
{
str.Write(keyData, 0, keyData.Length);
}
return new Pkcs8EncryptedPrivateKeyInfo(
new EncryptedPrivateKeyInfo((AlgorithmIdentifier)encryptor.AlgorithmDetails, bOut.ToArray()));
}
catch (IOException)
{
throw new InvalidOperationException("cannot encode privateKeyInfo");
}
}
}
}
|