init commit

This commit is contained in:
2025-10-09 09:57:24 +09:00
commit 4d551bd74f
6636 changed files with 1218703 additions and 0 deletions

View File

@@ -0,0 +1,108 @@
using System.Collections;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.EdEC;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Agreement;
using Org.BouncyCastle.Crypto.Agreement.Kdf;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Security;
public sealed class AgreementUtilities
{
private static readonly IDictionary algorithms;
private AgreementUtilities()
{
}
static AgreementUtilities()
{
algorithms = Platform.CreateHashtable();
algorithms[X9ObjectIdentifiers.DHSinglePassCofactorDHSha1KdfScheme.Id] = "ECCDHWITHSHA1KDF";
algorithms[X9ObjectIdentifiers.DHSinglePassStdDHSha1KdfScheme.Id] = "ECDHWITHSHA1KDF";
algorithms[X9ObjectIdentifiers.MqvSinglePassSha1KdfScheme.Id] = "ECMQVWITHSHA1KDF";
algorithms[EdECObjectIdentifiers.id_X25519.Id] = "X25519";
algorithms[EdECObjectIdentifiers.id_X448.Id] = "X448";
}
public static IBasicAgreement GetBasicAgreement(DerObjectIdentifier oid)
{
return GetBasicAgreement(oid.Id);
}
public static IBasicAgreement GetBasicAgreement(string algorithm)
{
switch (GetMechanism(algorithm))
{
case "DH":
case "DIFFIEHELLMAN":
return new DHBasicAgreement();
case "ECDH":
return new ECDHBasicAgreement();
case "ECDHC":
case "ECCDH":
return new ECDHCBasicAgreement();
case "ECMQV":
return new ECMqvBasicAgreement();
default:
throw new SecurityUtilityException("Basic Agreement " + algorithm + " not recognised.");
}
}
public static IBasicAgreement GetBasicAgreementWithKdf(DerObjectIdentifier oid, string wrapAlgorithm)
{
return GetBasicAgreementWithKdf(oid.Id, wrapAlgorithm);
}
public static IBasicAgreement GetBasicAgreementWithKdf(string agreeAlgorithm, string wrapAlgorithm)
{
switch (GetMechanism(agreeAlgorithm))
{
case "DHWITHSHA1KDF":
case "ECDHWITHSHA1KDF":
return new ECDHWithKdfBasicAgreement(wrapAlgorithm, new ECDHKekGenerator(new Sha1Digest()));
case "ECMQVWITHSHA1KDF":
return new ECMqvWithKdfBasicAgreement(wrapAlgorithm, new ECDHKekGenerator(new Sha1Digest()));
default:
throw new SecurityUtilityException("Basic Agreement (with KDF) " + agreeAlgorithm + " not recognised.");
}
}
public static IRawAgreement GetRawAgreement(DerObjectIdentifier oid)
{
return GetRawAgreement(oid.Id);
}
public static IRawAgreement GetRawAgreement(string algorithm)
{
string mechanism = GetMechanism(algorithm);
if (mechanism == "X25519")
{
return new X25519Agreement();
}
if (mechanism == "X448")
{
return new X448Agreement();
}
throw new SecurityUtilityException("Raw Agreement " + algorithm + " not recognised.");
}
public static string GetAlgorithmName(DerObjectIdentifier oid)
{
return (string)algorithms[oid.Id];
}
private static string GetMechanism(string algorithm)
{
string text = Platform.ToUpperInvariant(algorithm);
string text2 = (string)algorithms[text];
if (text2 != null)
{
return text2;
}
return text;
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Security.Certificates;
[Serializable]
public class CertificateEncodingException : CertificateException
{
public CertificateEncodingException()
{
}
public CertificateEncodingException(string msg)
: base(msg)
{
}
public CertificateEncodingException(string msg, Exception e)
: base(msg, e)
{
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Security.Certificates;
[Serializable]
public class CertificateException : GeneralSecurityException
{
public CertificateException()
{
}
public CertificateException(string message)
: base(message)
{
}
public CertificateException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Security.Certificates;
[Serializable]
public class CertificateExpiredException : CertificateException
{
public CertificateExpiredException()
{
}
public CertificateExpiredException(string message)
: base(message)
{
}
public CertificateExpiredException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Security.Certificates;
[Serializable]
public class CertificateNotYetValidException : CertificateException
{
public CertificateNotYetValidException()
{
}
public CertificateNotYetValidException(string message)
: base(message)
{
}
public CertificateNotYetValidException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Security.Certificates;
[Serializable]
public class CertificateParsingException : CertificateException
{
public CertificateParsingException()
{
}
public CertificateParsingException(string message)
: base(message)
{
}
public CertificateParsingException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Security.Certificates;
[Serializable]
public class CrlException : GeneralSecurityException
{
public CrlException()
{
}
public CrlException(string msg)
: base(msg)
{
}
public CrlException(string msg, Exception e)
: base(msg, e)
{
}
}

View File

@@ -0,0 +1,674 @@
using System;
using System.Collections;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.Kisa;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Ntt;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Agreement;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Macs;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Security;
public sealed class CipherUtilities
{
private enum CipherAlgorithm
{
AES,
ARC4,
BLOWFISH,
CAMELLIA,
CAST5,
CAST6,
DES,
DESEDE,
ELGAMAL,
GOST28147,
HC128,
HC256,
IDEA,
NOEKEON,
PBEWITHSHAAND128BITRC4,
PBEWITHSHAAND40BITRC4,
RC2,
RC5,
RC5_64,
RC6,
RIJNDAEL,
RSA,
SALSA20,
SEED,
SERPENT,
SKIPJACK,
SM4,
TEA,
THREEFISH_256,
THREEFISH_512,
THREEFISH_1024,
TNEPRES,
TWOFISH,
VMPC,
VMPC_KSA3,
XTEA
}
private enum CipherMode
{
ECB,
NONE,
CBC,
CCM,
CFB,
CTR,
CTS,
EAX,
GCM,
GOFB,
OCB,
OFB,
OPENPGPCFB,
SIC
}
private enum CipherPadding
{
NOPADDING,
RAW,
ISO10126PADDING,
ISO10126D2PADDING,
ISO10126_2PADDING,
ISO7816_4PADDING,
ISO9797_1PADDING,
ISO9796_1,
ISO9796_1PADDING,
OAEP,
OAEPPADDING,
OAEPWITHMD5ANDMGF1PADDING,
OAEPWITHSHA1ANDMGF1PADDING,
OAEPWITHSHA_1ANDMGF1PADDING,
OAEPWITHSHA224ANDMGF1PADDING,
OAEPWITHSHA_224ANDMGF1PADDING,
OAEPWITHSHA256ANDMGF1PADDING,
OAEPWITHSHA_256ANDMGF1PADDING,
OAEPWITHSHA384ANDMGF1PADDING,
OAEPWITHSHA_384ANDMGF1PADDING,
OAEPWITHSHA512ANDMGF1PADDING,
OAEPWITHSHA_512ANDMGF1PADDING,
PKCS1,
PKCS1PADDING,
PKCS5,
PKCS5PADDING,
PKCS7,
PKCS7PADDING,
TBCPADDING,
WITHCTS,
X923PADDING,
ZEROBYTEPADDING
}
private static readonly IDictionary algorithms;
private static readonly IDictionary oids;
public static ICollection Algorithms => oids.Keys;
static CipherUtilities()
{
algorithms = Platform.CreateHashtable();
oids = Platform.CreateHashtable();
((CipherAlgorithm)Enums.GetArbitraryValue(typeof(CipherAlgorithm))).ToString();
((CipherMode)Enums.GetArbitraryValue(typeof(CipherMode))).ToString();
((CipherPadding)Enums.GetArbitraryValue(typeof(CipherPadding))).ToString();
algorithms[NistObjectIdentifiers.IdAes128Ecb.Id] = "AES/ECB/PKCS7PADDING";
algorithms[NistObjectIdentifiers.IdAes192Ecb.Id] = "AES/ECB/PKCS7PADDING";
algorithms[NistObjectIdentifiers.IdAes256Ecb.Id] = "AES/ECB/PKCS7PADDING";
algorithms["AES//PKCS7"] = "AES/ECB/PKCS7PADDING";
algorithms["AES//PKCS7PADDING"] = "AES/ECB/PKCS7PADDING";
algorithms["AES//PKCS5"] = "AES/ECB/PKCS7PADDING";
algorithms["AES//PKCS5PADDING"] = "AES/ECB/PKCS7PADDING";
algorithms[NistObjectIdentifiers.IdAes128Cbc.Id] = "AES/CBC/PKCS7PADDING";
algorithms[NistObjectIdentifiers.IdAes192Cbc.Id] = "AES/CBC/PKCS7PADDING";
algorithms[NistObjectIdentifiers.IdAes256Cbc.Id] = "AES/CBC/PKCS7PADDING";
algorithms[NistObjectIdentifiers.IdAes128Ofb.Id] = "AES/OFB/NOPADDING";
algorithms[NistObjectIdentifiers.IdAes192Ofb.Id] = "AES/OFB/NOPADDING";
algorithms[NistObjectIdentifiers.IdAes256Ofb.Id] = "AES/OFB/NOPADDING";
algorithms[NistObjectIdentifiers.IdAes128Cfb.Id] = "AES/CFB/NOPADDING";
algorithms[NistObjectIdentifiers.IdAes192Cfb.Id] = "AES/CFB/NOPADDING";
algorithms[NistObjectIdentifiers.IdAes256Cfb.Id] = "AES/CFB/NOPADDING";
algorithms["RSA/ECB/PKCS1"] = "RSA//PKCS1PADDING";
algorithms["RSA/ECB/PKCS1PADDING"] = "RSA//PKCS1PADDING";
algorithms[PkcsObjectIdentifiers.RsaEncryption.Id] = "RSA//PKCS1PADDING";
algorithms[PkcsObjectIdentifiers.IdRsaesOaep.Id] = "RSA//OAEPPADDING";
algorithms[OiwObjectIdentifiers.DesCbc.Id] = "DES/CBC";
algorithms[OiwObjectIdentifiers.DesCfb.Id] = "DES/CFB";
algorithms[OiwObjectIdentifiers.DesEcb.Id] = "DES/ECB";
algorithms[OiwObjectIdentifiers.DesOfb.Id] = "DES/OFB";
algorithms[OiwObjectIdentifiers.DesEde.Id] = "DESEDE";
algorithms["TDEA"] = "DESEDE";
algorithms[PkcsObjectIdentifiers.DesEde3Cbc.Id] = "DESEDE/CBC";
algorithms[PkcsObjectIdentifiers.RC2Cbc.Id] = "RC2/CBC";
algorithms["1.3.6.1.4.1.188.7.1.1.2"] = "IDEA/CBC";
algorithms["1.2.840.113533.7.66.10"] = "CAST5/CBC";
algorithms["RC4"] = "ARC4";
algorithms["ARCFOUR"] = "ARC4";
algorithms["1.2.840.113549.3.4"] = "ARC4";
algorithms["PBEWITHSHA1AND128BITRC4"] = "PBEWITHSHAAND128BITRC4";
algorithms[PkcsObjectIdentifiers.PbeWithShaAnd128BitRC4.Id] = "PBEWITHSHAAND128BITRC4";
algorithms["PBEWITHSHA1AND40BITRC4"] = "PBEWITHSHAAND40BITRC4";
algorithms[PkcsObjectIdentifiers.PbeWithShaAnd40BitRC4.Id] = "PBEWITHSHAAND40BITRC4";
algorithms["PBEWITHSHA1ANDDES"] = "PBEWITHSHA1ANDDES-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithSha1AndDesCbc.Id] = "PBEWITHSHA1ANDDES-CBC";
algorithms["PBEWITHSHA1ANDRC2"] = "PBEWITHSHA1ANDRC2-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithSha1AndRC2Cbc.Id] = "PBEWITHSHA1ANDRC2-CBC";
algorithms["PBEWITHSHA1AND3-KEYTRIPLEDES-CBC"] = "PBEWITHSHAAND3-KEYTRIPLEDES-CBC";
algorithms["PBEWITHSHAAND3KEYTRIPLEDES"] = "PBEWITHSHAAND3-KEYTRIPLEDES-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithShaAnd3KeyTripleDesCbc.Id] = "PBEWITHSHAAND3-KEYTRIPLEDES-CBC";
algorithms["PBEWITHSHA1ANDDESEDE"] = "PBEWITHSHAAND3-KEYTRIPLEDES-CBC";
algorithms["PBEWITHSHA1AND2-KEYTRIPLEDES-CBC"] = "PBEWITHSHAAND2-KEYTRIPLEDES-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithShaAnd2KeyTripleDesCbc.Id] = "PBEWITHSHAAND2-KEYTRIPLEDES-CBC";
algorithms["PBEWITHSHA1AND128BITRC2-CBC"] = "PBEWITHSHAAND128BITRC2-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithShaAnd128BitRC2Cbc.Id] = "PBEWITHSHAAND128BITRC2-CBC";
algorithms["PBEWITHSHA1AND40BITRC2-CBC"] = "PBEWITHSHAAND40BITRC2-CBC";
algorithms[PkcsObjectIdentifiers.PbewithShaAnd40BitRC2Cbc.Id] = "PBEWITHSHAAND40BITRC2-CBC";
algorithms["PBEWITHSHA1AND128BITAES-CBC-BC"] = "PBEWITHSHAAND128BITAES-CBC-BC";
algorithms["PBEWITHSHA-1AND128BITAES-CBC-BC"] = "PBEWITHSHAAND128BITAES-CBC-BC";
algorithms["PBEWITHSHA1AND192BITAES-CBC-BC"] = "PBEWITHSHAAND192BITAES-CBC-BC";
algorithms["PBEWITHSHA-1AND192BITAES-CBC-BC"] = "PBEWITHSHAAND192BITAES-CBC-BC";
algorithms["PBEWITHSHA1AND256BITAES-CBC-BC"] = "PBEWITHSHAAND256BITAES-CBC-BC";
algorithms["PBEWITHSHA-1AND256BITAES-CBC-BC"] = "PBEWITHSHAAND256BITAES-CBC-BC";
algorithms["PBEWITHSHA-256AND128BITAES-CBC-BC"] = "PBEWITHSHA256AND128BITAES-CBC-BC";
algorithms["PBEWITHSHA-256AND192BITAES-CBC-BC"] = "PBEWITHSHA256AND192BITAES-CBC-BC";
algorithms["PBEWITHSHA-256AND256BITAES-CBC-BC"] = "PBEWITHSHA256AND256BITAES-CBC-BC";
algorithms["GOST"] = "GOST28147";
algorithms["GOST-28147"] = "GOST28147";
algorithms[CryptoProObjectIdentifiers.GostR28147Cbc.Id] = "GOST28147/CBC/PKCS7PADDING";
algorithms["RC5-32"] = "RC5";
algorithms[NttObjectIdentifiers.IdCamellia128Cbc.Id] = "CAMELLIA/CBC/PKCS7PADDING";
algorithms[NttObjectIdentifiers.IdCamellia192Cbc.Id] = "CAMELLIA/CBC/PKCS7PADDING";
algorithms[NttObjectIdentifiers.IdCamellia256Cbc.Id] = "CAMELLIA/CBC/PKCS7PADDING";
algorithms[KisaObjectIdentifiers.IdSeedCbc.Id] = "SEED/CBC/PKCS7PADDING";
algorithms["1.3.6.1.4.1.3029.1.2"] = "BLOWFISH/CBC";
}
private CipherUtilities()
{
}
public static DerObjectIdentifier GetObjectIdentifier(string mechanism)
{
if (mechanism == null)
{
throw new ArgumentNullException("mechanism");
}
mechanism = Platform.ToUpperInvariant(mechanism);
string text = (string)algorithms[mechanism];
if (text != null)
{
mechanism = text;
}
return (DerObjectIdentifier)oids[mechanism];
}
public static IBufferedCipher GetCipher(DerObjectIdentifier oid)
{
return GetCipher(oid.Id);
}
public static IBufferedCipher GetCipher(string algorithm)
{
if (algorithm == null)
{
throw new ArgumentNullException("algorithm");
}
algorithm = Platform.ToUpperInvariant(algorithm);
string text = (string)algorithms[algorithm];
if (text != null)
{
algorithm = text;
}
IBasicAgreement basicAgreement = null;
if (algorithm == "IES")
{
basicAgreement = new DHBasicAgreement();
}
else if (algorithm == "ECIES")
{
basicAgreement = new ECDHBasicAgreement();
}
if (basicAgreement != null)
{
return new BufferedIesCipher(new IesEngine(basicAgreement, new Kdf2BytesGenerator(new Sha1Digest()), new HMac(new Sha1Digest())));
}
if (Platform.StartsWith(algorithm, "PBE"))
{
if (Platform.EndsWith(algorithm, "-CBC"))
{
if (algorithm == "PBEWITHSHA1ANDDES-CBC")
{
return new PaddedBufferedBlockCipher(new CbcBlockCipher(new DesEngine()));
}
if (algorithm == "PBEWITHSHA1ANDRC2-CBC")
{
return new PaddedBufferedBlockCipher(new CbcBlockCipher(new RC2Engine()));
}
if (Strings.IsOneOf(algorithm, "PBEWITHSHAAND2-KEYTRIPLEDES-CBC", "PBEWITHSHAAND3-KEYTRIPLEDES-CBC"))
{
return new PaddedBufferedBlockCipher(new CbcBlockCipher(new DesEdeEngine()));
}
if (Strings.IsOneOf(algorithm, "PBEWITHSHAAND128BITRC2-CBC", "PBEWITHSHAAND40BITRC2-CBC"))
{
return new PaddedBufferedBlockCipher(new CbcBlockCipher(new RC2Engine()));
}
}
else if ((Platform.EndsWith(algorithm, "-BC") || Platform.EndsWith(algorithm, "-OPENSSL")) && Strings.IsOneOf(algorithm, "PBEWITHSHAAND128BITAES-CBC-BC", "PBEWITHSHAAND192BITAES-CBC-BC", "PBEWITHSHAAND256BITAES-CBC-BC", "PBEWITHSHA256AND128BITAES-CBC-BC", "PBEWITHSHA256AND192BITAES-CBC-BC", "PBEWITHSHA256AND256BITAES-CBC-BC", "PBEWITHMD5AND128BITAES-CBC-OPENSSL", "PBEWITHMD5AND192BITAES-CBC-OPENSSL", "PBEWITHMD5AND256BITAES-CBC-OPENSSL"))
{
return new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()));
}
}
string[] array = algorithm.Split(new char[1] { '/' });
IBlockCipher blockCipher = null;
IAsymmetricBlockCipher asymmetricBlockCipher = null;
IStreamCipher streamCipher = null;
string text2 = array[0];
string text3 = (string)algorithms[text2];
if (text3 != null)
{
text2 = text3;
}
CipherAlgorithm cipherAlgorithm;
try
{
cipherAlgorithm = (CipherAlgorithm)Enums.GetEnumValue(typeof(CipherAlgorithm), text2);
}
catch (ArgumentException)
{
throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
}
switch (cipherAlgorithm)
{
case CipherAlgorithm.AES:
blockCipher = new AesEngine();
break;
case CipherAlgorithm.ARC4:
streamCipher = new RC4Engine();
break;
case CipherAlgorithm.BLOWFISH:
blockCipher = new BlowfishEngine();
break;
case CipherAlgorithm.CAMELLIA:
blockCipher = new CamelliaEngine();
break;
case CipherAlgorithm.CAST5:
blockCipher = new Cast5Engine();
break;
case CipherAlgorithm.CAST6:
blockCipher = new Cast6Engine();
break;
case CipherAlgorithm.DES:
blockCipher = new DesEngine();
break;
case CipherAlgorithm.DESEDE:
blockCipher = new DesEdeEngine();
break;
case CipherAlgorithm.ELGAMAL:
asymmetricBlockCipher = new ElGamalEngine();
break;
case CipherAlgorithm.GOST28147:
blockCipher = new Gost28147Engine();
break;
case CipherAlgorithm.HC128:
streamCipher = new HC128Engine();
break;
case CipherAlgorithm.HC256:
streamCipher = new HC256Engine();
break;
case CipherAlgorithm.IDEA:
blockCipher = new IdeaEngine();
break;
case CipherAlgorithm.NOEKEON:
blockCipher = new NoekeonEngine();
break;
case CipherAlgorithm.PBEWITHSHAAND128BITRC4:
case CipherAlgorithm.PBEWITHSHAAND40BITRC4:
streamCipher = new RC4Engine();
break;
case CipherAlgorithm.RC2:
blockCipher = new RC2Engine();
break;
case CipherAlgorithm.RC5:
blockCipher = new RC532Engine();
break;
case CipherAlgorithm.RC5_64:
blockCipher = new RC564Engine();
break;
case CipherAlgorithm.RC6:
blockCipher = new RC6Engine();
break;
case CipherAlgorithm.RIJNDAEL:
blockCipher = new RijndaelEngine();
break;
case CipherAlgorithm.RSA:
asymmetricBlockCipher = new RsaBlindedEngine();
break;
case CipherAlgorithm.SALSA20:
streamCipher = new Salsa20Engine();
break;
case CipherAlgorithm.SEED:
blockCipher = new SeedEngine();
break;
case CipherAlgorithm.SERPENT:
blockCipher = new SerpentEngine();
break;
case CipherAlgorithm.SKIPJACK:
blockCipher = new SkipjackEngine();
break;
case CipherAlgorithm.SM4:
blockCipher = new SM4Engine();
break;
case CipherAlgorithm.TEA:
blockCipher = new TeaEngine();
break;
case CipherAlgorithm.THREEFISH_256:
blockCipher = new ThreefishEngine(256);
break;
case CipherAlgorithm.THREEFISH_512:
blockCipher = new ThreefishEngine(512);
break;
case CipherAlgorithm.THREEFISH_1024:
blockCipher = new ThreefishEngine(1024);
break;
case CipherAlgorithm.TNEPRES:
blockCipher = new TnepresEngine();
break;
case CipherAlgorithm.TWOFISH:
blockCipher = new TwofishEngine();
break;
case CipherAlgorithm.VMPC:
streamCipher = new VmpcEngine();
break;
case CipherAlgorithm.VMPC_KSA3:
streamCipher = new VmpcKsa3Engine();
break;
case CipherAlgorithm.XTEA:
blockCipher = new XteaEngine();
break;
default:
throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
}
if (streamCipher != null)
{
if (array.Length > 1)
{
throw new ArgumentException("Modes and paddings not used for stream ciphers");
}
return new BufferedStreamCipher(streamCipher);
}
bool flag = false;
bool flag2 = true;
IBlockCipherPadding blockCipherPadding = null;
IAeadBlockCipher aeadBlockCipher = null;
if (array.Length > 2)
{
if (streamCipher != null)
{
throw new ArgumentException("Paddings not used for stream ciphers");
}
string text4 = array[2];
CipherPadding cipherPadding;
if (text4 == "")
{
cipherPadding = CipherPadding.RAW;
}
else if (text4 == "X9.23PADDING")
{
cipherPadding = CipherPadding.X923PADDING;
}
else
{
try
{
cipherPadding = (CipherPadding)Enums.GetEnumValue(typeof(CipherPadding), text4);
}
catch (ArgumentException)
{
throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
}
}
switch (cipherPadding)
{
case CipherPadding.NOPADDING:
flag2 = false;
break;
case CipherPadding.ISO10126PADDING:
case CipherPadding.ISO10126D2PADDING:
case CipherPadding.ISO10126_2PADDING:
blockCipherPadding = new ISO10126d2Padding();
break;
case CipherPadding.ISO7816_4PADDING:
case CipherPadding.ISO9797_1PADDING:
blockCipherPadding = new ISO7816d4Padding();
break;
case CipherPadding.ISO9796_1:
case CipherPadding.ISO9796_1PADDING:
asymmetricBlockCipher = new ISO9796d1Encoding(asymmetricBlockCipher);
break;
case CipherPadding.OAEP:
case CipherPadding.OAEPPADDING:
asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher);
break;
case CipherPadding.OAEPWITHMD5ANDMGF1PADDING:
asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher, new MD5Digest());
break;
case CipherPadding.OAEPWITHSHA1ANDMGF1PADDING:
case CipherPadding.OAEPWITHSHA_1ANDMGF1PADDING:
asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher, new Sha1Digest());
break;
case CipherPadding.OAEPWITHSHA224ANDMGF1PADDING:
case CipherPadding.OAEPWITHSHA_224ANDMGF1PADDING:
asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher, new Sha224Digest());
break;
case CipherPadding.OAEPWITHSHA256ANDMGF1PADDING:
case CipherPadding.OAEPWITHSHA_256ANDMGF1PADDING:
asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher, new Sha256Digest());
break;
case CipherPadding.OAEPWITHSHA384ANDMGF1PADDING:
case CipherPadding.OAEPWITHSHA_384ANDMGF1PADDING:
asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher, new Sha384Digest());
break;
case CipherPadding.OAEPWITHSHA512ANDMGF1PADDING:
case CipherPadding.OAEPWITHSHA_512ANDMGF1PADDING:
asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher, new Sha512Digest());
break;
case CipherPadding.PKCS1:
case CipherPadding.PKCS1PADDING:
asymmetricBlockCipher = new Pkcs1Encoding(asymmetricBlockCipher);
break;
case CipherPadding.PKCS5:
case CipherPadding.PKCS5PADDING:
case CipherPadding.PKCS7:
case CipherPadding.PKCS7PADDING:
blockCipherPadding = new Pkcs7Padding();
break;
case CipherPadding.TBCPADDING:
blockCipherPadding = new TbcPadding();
break;
case CipherPadding.WITHCTS:
flag = true;
break;
case CipherPadding.X923PADDING:
blockCipherPadding = new X923Padding();
break;
case CipherPadding.ZEROBYTEPADDING:
blockCipherPadding = new ZeroBytePadding();
break;
default:
throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
case CipherPadding.RAW:
break;
}
}
string text5 = "";
if (array.Length > 1)
{
text5 = array[1];
int digitIndex = GetDigitIndex(text5);
string text6 = ((digitIndex >= 0) ? text5.Substring(0, digitIndex) : text5);
try
{
switch ((text6 == "") ? CipherMode.NONE : ((CipherMode)Enums.GetEnumValue(typeof(CipherMode), text6)))
{
case CipherMode.CBC:
blockCipher = new CbcBlockCipher(blockCipher);
break;
case CipherMode.CCM:
aeadBlockCipher = new CcmBlockCipher(blockCipher);
break;
case CipherMode.CFB:
{
int bitBlockSize = ((digitIndex < 0) ? (8 * blockCipher.GetBlockSize()) : int.Parse(text5.Substring(digitIndex)));
blockCipher = new CfbBlockCipher(blockCipher, bitBlockSize);
break;
}
case CipherMode.CTR:
blockCipher = new SicBlockCipher(blockCipher);
break;
case CipherMode.CTS:
flag = true;
blockCipher = new CbcBlockCipher(blockCipher);
break;
case CipherMode.EAX:
aeadBlockCipher = new EaxBlockCipher(blockCipher);
break;
case CipherMode.GCM:
aeadBlockCipher = new GcmBlockCipher(blockCipher);
break;
case CipherMode.GOFB:
blockCipher = new GOfbBlockCipher(blockCipher);
break;
case CipherMode.OCB:
aeadBlockCipher = new OcbBlockCipher(blockCipher, CreateBlockCipher(cipherAlgorithm));
break;
case CipherMode.OFB:
{
int blockSize = ((digitIndex < 0) ? (8 * blockCipher.GetBlockSize()) : int.Parse(text5.Substring(digitIndex)));
blockCipher = new OfbBlockCipher(blockCipher, blockSize);
break;
}
case CipherMode.OPENPGPCFB:
blockCipher = new OpenPgpCfbBlockCipher(blockCipher);
break;
case CipherMode.SIC:
if (blockCipher.GetBlockSize() < 16)
{
throw new ArgumentException("Warning: SIC-Mode can become a twotime-pad if the blocksize of the cipher is too small. Use a cipher with a block size of at least 128 bits (e.g. AES)");
}
blockCipher = new SicBlockCipher(blockCipher);
break;
default:
throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
case CipherMode.ECB:
case CipherMode.NONE:
break;
}
}
catch (ArgumentException)
{
throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
}
}
if (aeadBlockCipher != null)
{
if (flag)
{
throw new SecurityUtilityException("CTS mode not valid for AEAD ciphers.");
}
if (flag2 && array.Length > 2 && array[2] != "")
{
throw new SecurityUtilityException("Bad padding specified for AEAD cipher.");
}
return new BufferedAeadBlockCipher(aeadBlockCipher);
}
if (blockCipher != null)
{
if (flag)
{
return new CtsBlockCipher(blockCipher);
}
if (blockCipherPadding != null)
{
return new PaddedBufferedBlockCipher(blockCipher, blockCipherPadding);
}
if (!flag2 || blockCipher.IsPartialBlockOkay)
{
return new BufferedBlockCipher(blockCipher);
}
return new PaddedBufferedBlockCipher(blockCipher);
}
if (asymmetricBlockCipher != null)
{
return new BufferedAsymmetricBlockCipher(asymmetricBlockCipher);
}
throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
}
public static string GetAlgorithmName(DerObjectIdentifier oid)
{
return (string)algorithms[oid.Id];
}
private static int GetDigitIndex(string s)
{
for (int i = 0; i < s.Length; i++)
{
if (char.IsDigit(s[i]))
{
return i;
}
}
return -1;
}
private static IBlockCipher CreateBlockCipher(CipherAlgorithm cipherAlgorithm)
{
return cipherAlgorithm switch
{
CipherAlgorithm.AES => new AesEngine(),
CipherAlgorithm.BLOWFISH => new BlowfishEngine(),
CipherAlgorithm.CAMELLIA => new CamelliaEngine(),
CipherAlgorithm.CAST5 => new Cast5Engine(),
CipherAlgorithm.CAST6 => new Cast6Engine(),
CipherAlgorithm.DES => new DesEngine(),
CipherAlgorithm.DESEDE => new DesEdeEngine(),
CipherAlgorithm.GOST28147 => new Gost28147Engine(),
CipherAlgorithm.IDEA => new IdeaEngine(),
CipherAlgorithm.NOEKEON => new NoekeonEngine(),
CipherAlgorithm.RC2 => new RC2Engine(),
CipherAlgorithm.RC5 => new RC532Engine(),
CipherAlgorithm.RC5_64 => new RC564Engine(),
CipherAlgorithm.RC6 => new RC6Engine(),
CipherAlgorithm.RIJNDAEL => new RijndaelEngine(),
CipherAlgorithm.SEED => new SeedEngine(),
CipherAlgorithm.SERPENT => new SerpentEngine(),
CipherAlgorithm.SKIPJACK => new SkipjackEngine(),
CipherAlgorithm.SM4 => new SM4Engine(),
CipherAlgorithm.TEA => new TeaEngine(),
CipherAlgorithm.THREEFISH_256 => new ThreefishEngine(256),
CipherAlgorithm.THREEFISH_512 => new ThreefishEngine(512),
CipherAlgorithm.THREEFISH_1024 => new ThreefishEngine(1024),
CipherAlgorithm.TNEPRES => new TnepresEngine(),
CipherAlgorithm.TWOFISH => new TwofishEngine(),
CipherAlgorithm.XTEA => new XteaEngine(),
_ => throw new SecurityUtilityException(string.Concat("Cipher ", cipherAlgorithm, " not recognised or not a block cipher")),
};
}
}

View File

@@ -0,0 +1,324 @@
using System;
using System.Collections;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.GM;
using Org.BouncyCastle.Asn1.Misc;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Rosstandart;
using Org.BouncyCastle.Asn1.TeleTrust;
using Org.BouncyCastle.Asn1.UA;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Security;
public sealed class DigestUtilities
{
private enum DigestAlgorithm
{
BLAKE2B_160,
BLAKE2B_256,
BLAKE2B_384,
BLAKE2B_512,
BLAKE2S_128,
BLAKE2S_160,
BLAKE2S_224,
BLAKE2S_256,
DSTU7564_256,
DSTU7564_384,
DSTU7564_512,
GOST3411,
GOST3411_2012_256,
GOST3411_2012_512,
KECCAK_224,
KECCAK_256,
KECCAK_288,
KECCAK_384,
KECCAK_512,
MD2,
MD4,
MD5,
NONE,
RIPEMD128,
RIPEMD160,
RIPEMD256,
RIPEMD320,
SHA_1,
SHA_224,
SHA_256,
SHA_384,
SHA_512,
SHA_512_224,
SHA_512_256,
SHA3_224,
SHA3_256,
SHA3_384,
SHA3_512,
SHAKE128,
SHAKE256,
SM3,
TIGER,
WHIRLPOOL
}
private static readonly IDictionary algorithms;
private static readonly IDictionary oids;
public static ICollection Algorithms => oids.Keys;
private DigestUtilities()
{
}
static DigestUtilities()
{
algorithms = Platform.CreateHashtable();
oids = Platform.CreateHashtable();
((DigestAlgorithm)Enums.GetArbitraryValue(typeof(DigestAlgorithm))).ToString();
algorithms[PkcsObjectIdentifiers.MD2.Id] = "MD2";
algorithms[PkcsObjectIdentifiers.MD4.Id] = "MD4";
algorithms[PkcsObjectIdentifiers.MD5.Id] = "MD5";
algorithms["SHA1"] = "SHA-1";
algorithms[OiwObjectIdentifiers.IdSha1.Id] = "SHA-1";
algorithms["SHA224"] = "SHA-224";
algorithms[NistObjectIdentifiers.IdSha224.Id] = "SHA-224";
algorithms["SHA256"] = "SHA-256";
algorithms[NistObjectIdentifiers.IdSha256.Id] = "SHA-256";
algorithms["SHA384"] = "SHA-384";
algorithms[NistObjectIdentifiers.IdSha384.Id] = "SHA-384";
algorithms["SHA512"] = "SHA-512";
algorithms[NistObjectIdentifiers.IdSha512.Id] = "SHA-512";
algorithms["SHA512/224"] = "SHA-512/224";
algorithms[NistObjectIdentifiers.IdSha512_224.Id] = "SHA-512/224";
algorithms["SHA512/256"] = "SHA-512/256";
algorithms[NistObjectIdentifiers.IdSha512_256.Id] = "SHA-512/256";
algorithms["RIPEMD-128"] = "RIPEMD128";
algorithms[TeleTrusTObjectIdentifiers.RipeMD128.Id] = "RIPEMD128";
algorithms["RIPEMD-160"] = "RIPEMD160";
algorithms[TeleTrusTObjectIdentifiers.RipeMD160.Id] = "RIPEMD160";
algorithms["RIPEMD-256"] = "RIPEMD256";
algorithms[TeleTrusTObjectIdentifiers.RipeMD256.Id] = "RIPEMD256";
algorithms["RIPEMD-320"] = "RIPEMD320";
algorithms[CryptoProObjectIdentifiers.GostR3411.Id] = "GOST3411";
algorithms["KECCAK224"] = "KECCAK-224";
algorithms["KECCAK256"] = "KECCAK-256";
algorithms["KECCAK288"] = "KECCAK-288";
algorithms["KECCAK384"] = "KECCAK-384";
algorithms["KECCAK512"] = "KECCAK-512";
algorithms[NistObjectIdentifiers.IdSha3_224.Id] = "SHA3-224";
algorithms[NistObjectIdentifiers.IdSha3_256.Id] = "SHA3-256";
algorithms[NistObjectIdentifiers.IdSha3_384.Id] = "SHA3-384";
algorithms[NistObjectIdentifiers.IdSha3_512.Id] = "SHA3-512";
algorithms[NistObjectIdentifiers.IdShake128.Id] = "SHAKE128";
algorithms[NistObjectIdentifiers.IdShake256.Id] = "SHAKE256";
algorithms[GMObjectIdentifiers.sm3.Id] = "SM3";
algorithms[MiscObjectIdentifiers.id_blake2b160.Id] = "BLAKE2B-160";
algorithms[MiscObjectIdentifiers.id_blake2b256.Id] = "BLAKE2B-256";
algorithms[MiscObjectIdentifiers.id_blake2b384.Id] = "BLAKE2B-384";
algorithms[MiscObjectIdentifiers.id_blake2b512.Id] = "BLAKE2B-512";
algorithms[MiscObjectIdentifiers.id_blake2s128.Id] = "BLAKE2S-128";
algorithms[MiscObjectIdentifiers.id_blake2s160.Id] = "BLAKE2S-160";
algorithms[MiscObjectIdentifiers.id_blake2s224.Id] = "BLAKE2S-224";
algorithms[MiscObjectIdentifiers.id_blake2s256.Id] = "BLAKE2S-256";
algorithms[RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256.Id] = "GOST3411-2012-256";
algorithms[RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512.Id] = "GOST3411-2012-512";
algorithms[UAObjectIdentifiers.dstu7564digest_256.Id] = "DSTU7564-256";
algorithms[UAObjectIdentifiers.dstu7564digest_384.Id] = "DSTU7564-384";
algorithms[UAObjectIdentifiers.dstu7564digest_512.Id] = "DSTU7564-512";
oids["MD2"] = PkcsObjectIdentifiers.MD2;
oids["MD4"] = PkcsObjectIdentifiers.MD4;
oids["MD5"] = PkcsObjectIdentifiers.MD5;
oids["SHA-1"] = OiwObjectIdentifiers.IdSha1;
oids["SHA-224"] = NistObjectIdentifiers.IdSha224;
oids["SHA-256"] = NistObjectIdentifiers.IdSha256;
oids["SHA-384"] = NistObjectIdentifiers.IdSha384;
oids["SHA-512"] = NistObjectIdentifiers.IdSha512;
oids["SHA-512/224"] = NistObjectIdentifiers.IdSha512_224;
oids["SHA-512/256"] = NistObjectIdentifiers.IdSha512_256;
oids["SHA3-224"] = NistObjectIdentifiers.IdSha3_224;
oids["SHA3-256"] = NistObjectIdentifiers.IdSha3_256;
oids["SHA3-384"] = NistObjectIdentifiers.IdSha3_384;
oids["SHA3-512"] = NistObjectIdentifiers.IdSha3_512;
oids["SHAKE128"] = NistObjectIdentifiers.IdShake128;
oids["SHAKE256"] = NistObjectIdentifiers.IdShake256;
oids["RIPEMD128"] = TeleTrusTObjectIdentifiers.RipeMD128;
oids["RIPEMD160"] = TeleTrusTObjectIdentifiers.RipeMD160;
oids["RIPEMD256"] = TeleTrusTObjectIdentifiers.RipeMD256;
oids["GOST3411"] = CryptoProObjectIdentifiers.GostR3411;
oids["SM3"] = GMObjectIdentifiers.sm3;
oids["BLAKE2B-160"] = MiscObjectIdentifiers.id_blake2b160;
oids["BLAKE2B-256"] = MiscObjectIdentifiers.id_blake2b256;
oids["BLAKE2B-384"] = MiscObjectIdentifiers.id_blake2b384;
oids["BLAKE2B-512"] = MiscObjectIdentifiers.id_blake2b512;
oids["BLAKE2S-128"] = MiscObjectIdentifiers.id_blake2s128;
oids["BLAKE2S-160"] = MiscObjectIdentifiers.id_blake2s160;
oids["BLAKE2S-224"] = MiscObjectIdentifiers.id_blake2s224;
oids["BLAKE2S-256"] = MiscObjectIdentifiers.id_blake2s256;
oids["GOST3411-2012-256"] = RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256;
oids["GOST3411-2012-512"] = RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512;
oids["DSTU7564-256"] = UAObjectIdentifiers.dstu7564digest_256;
oids["DSTU7564-384"] = UAObjectIdentifiers.dstu7564digest_384;
oids["DSTU7564-512"] = UAObjectIdentifiers.dstu7564digest_512;
}
public static DerObjectIdentifier GetObjectIdentifier(string mechanism)
{
if (mechanism == null)
{
throw new ArgumentNullException("mechanism");
}
mechanism = Platform.ToUpperInvariant(mechanism);
string text = (string)algorithms[mechanism];
if (text != null)
{
mechanism = text;
}
return (DerObjectIdentifier)oids[mechanism];
}
public static IDigest GetDigest(DerObjectIdentifier id)
{
return GetDigest(id.Id);
}
public static IDigest GetDigest(string algorithm)
{
string text = Platform.ToUpperInvariant(algorithm);
string text2 = (string)algorithms[text];
if (text2 == null)
{
text2 = text;
}
try
{
switch ((DigestAlgorithm)Enums.GetEnumValue(typeof(DigestAlgorithm), text2))
{
case DigestAlgorithm.BLAKE2B_160:
return new Blake2bDigest(160);
case DigestAlgorithm.BLAKE2B_256:
return new Blake2bDigest(256);
case DigestAlgorithm.BLAKE2B_384:
return new Blake2bDigest(384);
case DigestAlgorithm.BLAKE2B_512:
return new Blake2bDigest(512);
case DigestAlgorithm.BLAKE2S_128:
return new Blake2sDigest(128);
case DigestAlgorithm.BLAKE2S_160:
return new Blake2sDigest(160);
case DigestAlgorithm.BLAKE2S_224:
return new Blake2sDigest(224);
case DigestAlgorithm.BLAKE2S_256:
return new Blake2sDigest(256);
case DigestAlgorithm.DSTU7564_256:
return new Dstu7564Digest(256);
case DigestAlgorithm.DSTU7564_384:
return new Dstu7564Digest(384);
case DigestAlgorithm.DSTU7564_512:
return new Dstu7564Digest(512);
case DigestAlgorithm.GOST3411:
return new Gost3411Digest();
case DigestAlgorithm.GOST3411_2012_256:
return new Gost3411_2012_256Digest();
case DigestAlgorithm.GOST3411_2012_512:
return new Gost3411_2012_512Digest();
case DigestAlgorithm.KECCAK_224:
return new KeccakDigest(224);
case DigestAlgorithm.KECCAK_256:
return new KeccakDigest(256);
case DigestAlgorithm.KECCAK_288:
return new KeccakDigest(288);
case DigestAlgorithm.KECCAK_384:
return new KeccakDigest(384);
case DigestAlgorithm.KECCAK_512:
return new KeccakDigest(512);
case DigestAlgorithm.MD2:
return new MD2Digest();
case DigestAlgorithm.MD4:
return new MD4Digest();
case DigestAlgorithm.MD5:
return new MD5Digest();
case DigestAlgorithm.NONE:
return new NullDigest();
case DigestAlgorithm.RIPEMD128:
return new RipeMD128Digest();
case DigestAlgorithm.RIPEMD160:
return new RipeMD160Digest();
case DigestAlgorithm.RIPEMD256:
return new RipeMD256Digest();
case DigestAlgorithm.RIPEMD320:
return new RipeMD320Digest();
case DigestAlgorithm.SHA_1:
return new Sha1Digest();
case DigestAlgorithm.SHA_224:
return new Sha224Digest();
case DigestAlgorithm.SHA_256:
return new Sha256Digest();
case DigestAlgorithm.SHA_384:
return new Sha384Digest();
case DigestAlgorithm.SHA_512:
return new Sha512Digest();
case DigestAlgorithm.SHA_512_224:
return new Sha512tDigest(224);
case DigestAlgorithm.SHA_512_256:
return new Sha512tDigest(256);
case DigestAlgorithm.SHA3_224:
return new Sha3Digest(224);
case DigestAlgorithm.SHA3_256:
return new Sha3Digest(256);
case DigestAlgorithm.SHA3_384:
return new Sha3Digest(384);
case DigestAlgorithm.SHA3_512:
return new Sha3Digest(512);
case DigestAlgorithm.SHAKE128:
return new ShakeDigest(128);
case DigestAlgorithm.SHAKE256:
return new ShakeDigest(256);
case DigestAlgorithm.SM3:
return new SM3Digest();
case DigestAlgorithm.TIGER:
return new TigerDigest();
case DigestAlgorithm.WHIRLPOOL:
return new WhirlpoolDigest();
}
}
catch (ArgumentException)
{
}
throw new SecurityUtilityException("Digest " + text2 + " not recognised.");
}
public static string GetAlgorithmName(DerObjectIdentifier oid)
{
return (string)algorithms[oid.Id];
}
public static byte[] CalculateDigest(DerObjectIdentifier id, byte[] input)
{
return CalculateDigest(id.Id, input);
}
public static byte[] CalculateDigest(string algorithm, byte[] input)
{
IDigest digest = GetDigest(algorithm);
digest.BlockUpdate(input, 0, input.Length);
return DoFinal(digest);
}
public static byte[] DoFinal(IDigest digest)
{
byte[] array = new byte[digest.GetDigestSize()];
digest.DoFinal(array, 0);
return array;
}
public static byte[] DoFinal(IDigest digest, byte[] input)
{
digest.BlockUpdate(input, 0, input.Length);
return DoFinal(digest);
}
}

View File

@@ -0,0 +1,203 @@
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.X509;
namespace Org.BouncyCastle.Security;
public sealed class DotNetUtilities
{
private DotNetUtilities()
{
}
public static System.Security.Cryptography.X509Certificates.X509Certificate ToX509Certificate(X509CertificateStructure x509Struct)
{
return new System.Security.Cryptography.X509Certificates.X509Certificate(x509Struct.GetDerEncoded());
}
public static System.Security.Cryptography.X509Certificates.X509Certificate ToX509Certificate(Org.BouncyCastle.X509.X509Certificate x509Cert)
{
return new System.Security.Cryptography.X509Certificates.X509Certificate(x509Cert.GetEncoded());
}
public static Org.BouncyCastle.X509.X509Certificate FromX509Certificate(System.Security.Cryptography.X509Certificates.X509Certificate x509Cert)
{
return new X509CertificateParser().ReadCertificate(x509Cert.GetRawCertData());
}
public static AsymmetricCipherKeyPair GetDsaKeyPair(DSA dsa)
{
return GetDsaKeyPair(dsa.ExportParameters(includePrivateParameters: true));
}
public static AsymmetricCipherKeyPair GetDsaKeyPair(DSAParameters dp)
{
DsaValidationParameters parameters = ((dp.Seed != null) ? new DsaValidationParameters(dp.Seed, dp.Counter) : null);
DsaParameters parameters2 = new DsaParameters(new BigInteger(1, dp.P), new BigInteger(1, dp.Q), new BigInteger(1, dp.G), parameters);
DsaPublicKeyParameters publicParameter = new DsaPublicKeyParameters(new BigInteger(1, dp.Y), parameters2);
DsaPrivateKeyParameters privateParameter = new DsaPrivateKeyParameters(new BigInteger(1, dp.X), parameters2);
return new AsymmetricCipherKeyPair(publicParameter, privateParameter);
}
public static DsaPublicKeyParameters GetDsaPublicKey(DSA dsa)
{
return GetDsaPublicKey(dsa.ExportParameters(includePrivateParameters: false));
}
public static DsaPublicKeyParameters GetDsaPublicKey(DSAParameters dp)
{
DsaValidationParameters parameters = ((dp.Seed != null) ? new DsaValidationParameters(dp.Seed, dp.Counter) : null);
DsaParameters parameters2 = new DsaParameters(new BigInteger(1, dp.P), new BigInteger(1, dp.Q), new BigInteger(1, dp.G), parameters);
return new DsaPublicKeyParameters(new BigInteger(1, dp.Y), parameters2);
}
public static AsymmetricCipherKeyPair GetRsaKeyPair(RSA rsa)
{
return GetRsaKeyPair(rsa.ExportParameters(includePrivateParameters: true));
}
public static AsymmetricCipherKeyPair GetRsaKeyPair(RSAParameters rp)
{
BigInteger modulus = new BigInteger(1, rp.Modulus);
BigInteger bigInteger = new BigInteger(1, rp.Exponent);
RsaKeyParameters publicParameter = new RsaKeyParameters(isPrivate: false, modulus, bigInteger);
RsaPrivateCrtKeyParameters privateParameter = new RsaPrivateCrtKeyParameters(modulus, bigInteger, new BigInteger(1, rp.D), new BigInteger(1, rp.P), new BigInteger(1, rp.Q), new BigInteger(1, rp.DP), new BigInteger(1, rp.DQ), new BigInteger(1, rp.InverseQ));
return new AsymmetricCipherKeyPair(publicParameter, privateParameter);
}
public static RsaKeyParameters GetRsaPublicKey(RSA rsa)
{
return GetRsaPublicKey(rsa.ExportParameters(includePrivateParameters: false));
}
public static RsaKeyParameters GetRsaPublicKey(RSAParameters rp)
{
return new RsaKeyParameters(isPrivate: false, new BigInteger(1, rp.Modulus), new BigInteger(1, rp.Exponent));
}
public static AsymmetricCipherKeyPair GetKeyPair(AsymmetricAlgorithm privateKey)
{
if (privateKey is DSA)
{
return GetDsaKeyPair((DSA)privateKey);
}
if (privateKey is RSA)
{
return GetRsaKeyPair((RSA)privateKey);
}
throw new ArgumentException("Unsupported algorithm specified", "privateKey");
}
public static RSA ToRSA(RsaKeyParameters rsaKey)
{
return CreateRSAProvider(ToRSAParameters(rsaKey));
}
public static RSA ToRSA(RsaKeyParameters rsaKey, CspParameters csp)
{
return CreateRSAProvider(ToRSAParameters(rsaKey), csp);
}
public static RSA ToRSA(RsaPrivateCrtKeyParameters privKey)
{
return CreateRSAProvider(ToRSAParameters(privKey));
}
public static RSA ToRSA(RsaPrivateCrtKeyParameters privKey, CspParameters csp)
{
return CreateRSAProvider(ToRSAParameters(privKey), csp);
}
public static RSA ToRSA(RsaPrivateKeyStructure privKey)
{
return CreateRSAProvider(ToRSAParameters(privKey));
}
public static RSA ToRSA(RsaPrivateKeyStructure privKey, CspParameters csp)
{
return CreateRSAProvider(ToRSAParameters(privKey), csp);
}
public static RSAParameters ToRSAParameters(RsaKeyParameters rsaKey)
{
RSAParameters result = new RSAParameters
{
Modulus = rsaKey.Modulus.ToByteArrayUnsigned()
};
if (rsaKey.IsPrivate)
{
result.D = ConvertRSAParametersField(rsaKey.Exponent, result.Modulus.Length);
}
else
{
result.Exponent = rsaKey.Exponent.ToByteArrayUnsigned();
}
return result;
}
public static RSAParameters ToRSAParameters(RsaPrivateCrtKeyParameters privKey)
{
RSAParameters result = default(RSAParameters);
result.Modulus = privKey.Modulus.ToByteArrayUnsigned();
result.Exponent = privKey.PublicExponent.ToByteArrayUnsigned();
result.P = privKey.P.ToByteArrayUnsigned();
result.Q = privKey.Q.ToByteArrayUnsigned();
result.D = ConvertRSAParametersField(privKey.Exponent, result.Modulus.Length);
result.DP = ConvertRSAParametersField(privKey.DP, result.P.Length);
result.DQ = ConvertRSAParametersField(privKey.DQ, result.Q.Length);
result.InverseQ = ConvertRSAParametersField(privKey.QInv, result.Q.Length);
return result;
}
public static RSAParameters ToRSAParameters(RsaPrivateKeyStructure privKey)
{
RSAParameters result = default(RSAParameters);
result.Modulus = privKey.Modulus.ToByteArrayUnsigned();
result.Exponent = privKey.PublicExponent.ToByteArrayUnsigned();
result.P = privKey.Prime1.ToByteArrayUnsigned();
result.Q = privKey.Prime2.ToByteArrayUnsigned();
result.D = ConvertRSAParametersField(privKey.PrivateExponent, result.Modulus.Length);
result.DP = ConvertRSAParametersField(privKey.Exponent1, result.P.Length);
result.DQ = ConvertRSAParametersField(privKey.Exponent2, result.Q.Length);
result.InverseQ = ConvertRSAParametersField(privKey.Coefficient, result.Q.Length);
return result;
}
private static byte[] ConvertRSAParametersField(BigInteger n, int size)
{
byte[] array = n.ToByteArrayUnsigned();
if (array.Length == size)
{
return array;
}
if (array.Length > size)
{
throw new ArgumentException("Specified size too small", "size");
}
byte[] array2 = new byte[size];
Array.Copy(array, 0, array2, size - array.Length, array.Length);
return array2;
}
private static RSA CreateRSAProvider(RSAParameters rp)
{
CspParameters cspParameters = new CspParameters();
cspParameters.KeyContainerName = $"BouncyCastle-{Guid.NewGuid()}";
RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider(cspParameters);
rSACryptoServiceProvider.ImportParameters(rp);
return rSACryptoServiceProvider;
}
private static RSA CreateRSAProvider(RSAParameters rp, CspParameters csp)
{
RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider(csp);
rSACryptoServiceProvider.ImportParameters(rp);
return rSACryptoServiceProvider;
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Security;
[Serializable]
public class EncryptionException : IOException
{
public EncryptionException(string message)
: base(message)
{
}
public EncryptionException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Security;
[Serializable]
public class GeneralSecurityException : Exception
{
public GeneralSecurityException()
{
}
public GeneralSecurityException(string message)
: base(message)
{
}
public GeneralSecurityException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,269 @@
using System.Collections;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.EdEC;
using Org.BouncyCastle.Asn1.Iana;
using Org.BouncyCastle.Asn1.Kisa;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Ntt;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Rosstandart;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Security;
public sealed class GeneratorUtilities
{
private static readonly IDictionary kgAlgorithms;
private static readonly IDictionary kpgAlgorithms;
private static readonly IDictionary defaultKeySizes;
private GeneratorUtilities()
{
}
static GeneratorUtilities()
{
kgAlgorithms = Platform.CreateHashtable();
kpgAlgorithms = Platform.CreateHashtable();
defaultKeySizes = Platform.CreateHashtable();
AddKgAlgorithm("AES", "AESWRAP");
AddKgAlgorithm("AES128", "2.16.840.1.101.3.4.2", NistObjectIdentifiers.IdAes128Cbc, NistObjectIdentifiers.IdAes128Cfb, NistObjectIdentifiers.IdAes128Ecb, NistObjectIdentifiers.IdAes128Ofb, NistObjectIdentifiers.IdAes128Wrap);
AddKgAlgorithm("AES192", "2.16.840.1.101.3.4.22", NistObjectIdentifiers.IdAes192Cbc, NistObjectIdentifiers.IdAes192Cfb, NistObjectIdentifiers.IdAes192Ecb, NistObjectIdentifiers.IdAes192Ofb, NistObjectIdentifiers.IdAes192Wrap);
AddKgAlgorithm("AES256", "2.16.840.1.101.3.4.42", NistObjectIdentifiers.IdAes256Cbc, NistObjectIdentifiers.IdAes256Cfb, NistObjectIdentifiers.IdAes256Ecb, NistObjectIdentifiers.IdAes256Ofb, NistObjectIdentifiers.IdAes256Wrap);
AddKgAlgorithm("BLOWFISH", "1.3.6.1.4.1.3029.1.2");
AddKgAlgorithm("CAMELLIA", "CAMELLIAWRAP");
AddKgAlgorithm("CAMELLIA128", NttObjectIdentifiers.IdCamellia128Cbc, NttObjectIdentifiers.IdCamellia128Wrap);
AddKgAlgorithm("CAMELLIA192", NttObjectIdentifiers.IdCamellia192Cbc, NttObjectIdentifiers.IdCamellia192Wrap);
AddKgAlgorithm("CAMELLIA256", NttObjectIdentifiers.IdCamellia256Cbc, NttObjectIdentifiers.IdCamellia256Wrap);
AddKgAlgorithm("CAST5", "1.2.840.113533.7.66.10");
AddKgAlgorithm("CAST6");
AddKgAlgorithm("DES", OiwObjectIdentifiers.DesCbc, OiwObjectIdentifiers.DesCfb, OiwObjectIdentifiers.DesEcb, OiwObjectIdentifiers.DesOfb);
AddKgAlgorithm("DESEDE", "DESEDEWRAP", "TDEA", OiwObjectIdentifiers.DesEde);
AddKgAlgorithm("DESEDE3", PkcsObjectIdentifiers.DesEde3Cbc, PkcsObjectIdentifiers.IdAlgCms3DesWrap);
AddKgAlgorithm("GOST28147", "GOST", "GOST-28147", CryptoProObjectIdentifiers.GostR28147Cbc);
AddKgAlgorithm("HC128");
AddKgAlgorithm("HC256");
AddKgAlgorithm("IDEA", "1.3.6.1.4.1.188.7.1.1.2");
AddKgAlgorithm("NOEKEON");
AddKgAlgorithm("RC2", PkcsObjectIdentifiers.RC2Cbc, PkcsObjectIdentifiers.IdAlgCmsRC2Wrap);
AddKgAlgorithm("RC4", "ARC4", "1.2.840.113549.3.4");
AddKgAlgorithm("RC5", "RC5-32");
AddKgAlgorithm("RC5-64");
AddKgAlgorithm("RC6");
AddKgAlgorithm("RIJNDAEL");
AddKgAlgorithm("SALSA20");
AddKgAlgorithm("SEED", KisaObjectIdentifiers.IdNpkiAppCmsSeedWrap, KisaObjectIdentifiers.IdSeedCbc);
AddKgAlgorithm("SERPENT");
AddKgAlgorithm("SKIPJACK");
AddKgAlgorithm("SM4");
AddKgAlgorithm("TEA");
AddKgAlgorithm("THREEFISH-256");
AddKgAlgorithm("THREEFISH-512");
AddKgAlgorithm("THREEFISH-1024");
AddKgAlgorithm("TNEPRES");
AddKgAlgorithm("TWOFISH");
AddKgAlgorithm("VMPC");
AddKgAlgorithm("VMPC-KSA3");
AddKgAlgorithm("XTEA");
AddHMacKeyGenerator("MD2");
AddHMacKeyGenerator("MD4");
AddHMacKeyGenerator("MD5", IanaObjectIdentifiers.HmacMD5);
AddHMacKeyGenerator("SHA1", PkcsObjectIdentifiers.IdHmacWithSha1, IanaObjectIdentifiers.HmacSha1);
AddHMacKeyGenerator("SHA224", PkcsObjectIdentifiers.IdHmacWithSha224);
AddHMacKeyGenerator("SHA256", PkcsObjectIdentifiers.IdHmacWithSha256);
AddHMacKeyGenerator("SHA384", PkcsObjectIdentifiers.IdHmacWithSha384);
AddHMacKeyGenerator("SHA512", PkcsObjectIdentifiers.IdHmacWithSha512);
AddHMacKeyGenerator("SHA512/224");
AddHMacKeyGenerator("SHA512/256");
AddHMacKeyGenerator("KECCAK224");
AddHMacKeyGenerator("KECCAK256");
AddHMacKeyGenerator("KECCAK288");
AddHMacKeyGenerator("KECCAK384");
AddHMacKeyGenerator("KECCAK512");
AddHMacKeyGenerator("SHA3-224", NistObjectIdentifiers.IdHMacWithSha3_224);
AddHMacKeyGenerator("SHA3-256", NistObjectIdentifiers.IdHMacWithSha3_256);
AddHMacKeyGenerator("SHA3-384", NistObjectIdentifiers.IdHMacWithSha3_384);
AddHMacKeyGenerator("SHA3-512", NistObjectIdentifiers.IdHMacWithSha3_512);
AddHMacKeyGenerator("RIPEMD128");
AddHMacKeyGenerator("RIPEMD160", IanaObjectIdentifiers.HmacRipeMD160);
AddHMacKeyGenerator("TIGER", IanaObjectIdentifiers.HmacTiger);
AddHMacKeyGenerator("GOST3411-2012-256", RosstandartObjectIdentifiers.id_tc26_hmac_gost_3411_12_256);
AddHMacKeyGenerator("GOST3411-2012-512", RosstandartObjectIdentifiers.id_tc26_hmac_gost_3411_12_512);
AddKpgAlgorithm("DH", "DIFFIEHELLMAN");
AddKpgAlgorithm("DSA");
AddKpgAlgorithm("EC", X9ObjectIdentifiers.DHSinglePassStdDHSha1KdfScheme);
AddKpgAlgorithm("ECDH", "ECIES");
AddKpgAlgorithm("ECDHC");
AddKpgAlgorithm("ECMQV", X9ObjectIdentifiers.MqvSinglePassSha1KdfScheme);
AddKpgAlgorithm("ECDSA");
AddKpgAlgorithm("ECGOST3410", "ECGOST-3410", "GOST-3410-2001");
AddKpgAlgorithm("Ed25519", "Ed25519ctx", "Ed25519ph", EdECObjectIdentifiers.id_Ed25519);
AddKpgAlgorithm("Ed448", "Ed448ph", EdECObjectIdentifiers.id_Ed448);
AddKpgAlgorithm("ELGAMAL");
AddKpgAlgorithm("GOST3410", "GOST-3410", "GOST-3410-94");
AddKpgAlgorithm("RSA", "1.2.840.113549.1.1.1");
AddKpgAlgorithm("X25519", EdECObjectIdentifiers.id_X25519);
AddKpgAlgorithm("X448", EdECObjectIdentifiers.id_X448);
AddDefaultKeySizeEntries(64, "DES");
AddDefaultKeySizeEntries(80, "SKIPJACK");
AddDefaultKeySizeEntries(128, "AES128", "BLOWFISH", "CAMELLIA128", "CAST5", "DESEDE", "HC128", "HMACMD2", "HMACMD4", "HMACMD5", "HMACRIPEMD128", "IDEA", "NOEKEON", "RC2", "RC4", "RC5", "SALSA20", "SEED", "SM4", "TEA", "XTEA", "VMPC", "VMPC-KSA3");
AddDefaultKeySizeEntries(160, "HMACRIPEMD160", "HMACSHA1");
AddDefaultKeySizeEntries(192, "AES", "AES192", "CAMELLIA192", "DESEDE3", "HMACTIGER", "RIJNDAEL", "SERPENT", "TNEPRES");
AddDefaultKeySizeEntries(224, "HMACSHA3-224", "HMACKECCAK224", "HMACSHA224", "HMACSHA512/224");
AddDefaultKeySizeEntries(256, "AES256", "CAMELLIA", "CAMELLIA256", "CAST6", "GOST28147", "HC256", "HMACGOST3411-2012-256", "HMACSHA3-256", "HMACKECCAK256", "HMACSHA256", "HMACSHA512/256", "RC5-64", "RC6", "THREEFISH-256", "TWOFISH");
AddDefaultKeySizeEntries(288, "HMACKECCAK288");
AddDefaultKeySizeEntries(384, "HMACSHA3-384", "HMACKECCAK384", "HMACSHA384");
AddDefaultKeySizeEntries(512, "HMACGOST3411-2012-512", "HMACSHA3-512", "HMACKECCAK512", "HMACSHA512", "THREEFISH-512");
AddDefaultKeySizeEntries(1024, "THREEFISH-1024");
}
private static void AddDefaultKeySizeEntries(int size, params string[] algorithms)
{
foreach (string key in algorithms)
{
defaultKeySizes.Add(key, size);
}
}
private static void AddKgAlgorithm(string canonicalName, params object[] aliases)
{
kgAlgorithms[Platform.ToUpperInvariant(canonicalName)] = canonicalName;
foreach (object obj in aliases)
{
kgAlgorithms[Platform.ToUpperInvariant(obj.ToString())] = canonicalName;
}
}
private static void AddKpgAlgorithm(string canonicalName, params object[] aliases)
{
kpgAlgorithms[Platform.ToUpperInvariant(canonicalName)] = canonicalName;
foreach (object obj in aliases)
{
kpgAlgorithms[Platform.ToUpperInvariant(obj.ToString())] = canonicalName;
}
}
private static void AddHMacKeyGenerator(string algorithm, params object[] aliases)
{
string text = "HMAC" + algorithm;
kgAlgorithms[text] = text;
kgAlgorithms["HMAC-" + algorithm] = text;
kgAlgorithms["HMAC/" + algorithm] = text;
foreach (object obj in aliases)
{
kgAlgorithms[Platform.ToUpperInvariant(obj.ToString())] = text;
}
}
internal static string GetCanonicalKeyGeneratorAlgorithm(string algorithm)
{
return (string)kgAlgorithms[Platform.ToUpperInvariant(algorithm)];
}
internal static string GetCanonicalKeyPairGeneratorAlgorithm(string algorithm)
{
return (string)kpgAlgorithms[Platform.ToUpperInvariant(algorithm)];
}
public static CipherKeyGenerator GetKeyGenerator(DerObjectIdentifier oid)
{
return GetKeyGenerator(oid.Id);
}
public static CipherKeyGenerator GetKeyGenerator(string algorithm)
{
string canonicalKeyGeneratorAlgorithm = GetCanonicalKeyGeneratorAlgorithm(algorithm);
if (canonicalKeyGeneratorAlgorithm == null)
{
throw new SecurityUtilityException("KeyGenerator " + algorithm + " not recognised.");
}
int num = FindDefaultKeySize(canonicalKeyGeneratorAlgorithm);
if (num == -1)
{
throw new SecurityUtilityException("KeyGenerator " + algorithm + " (" + canonicalKeyGeneratorAlgorithm + ") not supported.");
}
switch (canonicalKeyGeneratorAlgorithm)
{
case "DES":
return new DesKeyGenerator(num);
case "DESEDE":
case "DESEDE3":
return new DesEdeKeyGenerator(num);
default:
return new CipherKeyGenerator(num);
}
}
public static IAsymmetricCipherKeyPairGenerator GetKeyPairGenerator(DerObjectIdentifier oid)
{
return GetKeyPairGenerator(oid.Id);
}
public static IAsymmetricCipherKeyPairGenerator GetKeyPairGenerator(string algorithm)
{
string canonicalKeyPairGeneratorAlgorithm = GetCanonicalKeyPairGeneratorAlgorithm(algorithm);
if (canonicalKeyPairGeneratorAlgorithm == null)
{
throw new SecurityUtilityException("KeyPairGenerator " + algorithm + " not recognised.");
}
if (canonicalKeyPairGeneratorAlgorithm == "DH")
{
return new DHKeyPairGenerator();
}
if (canonicalKeyPairGeneratorAlgorithm == "DSA")
{
return new DsaKeyPairGenerator();
}
if (Platform.StartsWith(canonicalKeyPairGeneratorAlgorithm, "EC"))
{
return new ECKeyPairGenerator(canonicalKeyPairGeneratorAlgorithm);
}
return canonicalKeyPairGeneratorAlgorithm switch
{
"Ed25519" => new Ed25519KeyPairGenerator(),
"Ed448" => new Ed448KeyPairGenerator(),
"ELGAMAL" => new ElGamalKeyPairGenerator(),
"GOST3410" => new Gost3410KeyPairGenerator(),
"RSA" => new RsaKeyPairGenerator(),
"X25519" => new X25519KeyPairGenerator(),
"X448" => new X448KeyPairGenerator(),
_ => throw new SecurityUtilityException("KeyPairGenerator " + algorithm + " (" + canonicalKeyPairGeneratorAlgorithm + ") not supported."),
};
}
internal static int GetDefaultKeySize(DerObjectIdentifier oid)
{
return GetDefaultKeySize(oid.Id);
}
internal static int GetDefaultKeySize(string algorithm)
{
string canonicalKeyGeneratorAlgorithm = GetCanonicalKeyGeneratorAlgorithm(algorithm);
if (canonicalKeyGeneratorAlgorithm == null)
{
throw new SecurityUtilityException("KeyGenerator " + algorithm + " not recognised.");
}
int num = FindDefaultKeySize(canonicalKeyGeneratorAlgorithm);
if (num == -1)
{
throw new SecurityUtilityException("KeyGenerator " + algorithm + " (" + canonicalKeyGeneratorAlgorithm + ") not supported.");
}
return num;
}
private static int FindDefaultKeySize(string canonicalName)
{
if (!defaultKeySizes.Contains(canonicalName))
{
return -1;
}
return (int)defaultKeySizes[canonicalName];
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Security;
[Serializable]
public class InvalidKeyException : KeyException
{
public InvalidKeyException()
{
}
public InvalidKeyException(string message)
: base(message)
{
}
public InvalidKeyException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Security;
[Serializable]
public class InvalidParameterException : KeyException
{
public InvalidParameterException()
{
}
public InvalidParameterException(string message)
: base(message)
{
}
public InvalidParameterException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Security;
[Serializable]
public class KeyException : GeneralSecurityException
{
public KeyException()
{
}
public KeyException(string message)
: base(message)
{
}
public KeyException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,169 @@
using System.Collections;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Iana;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Rosstandart;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Macs;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Security;
public sealed class MacUtilities
{
private static readonly IDictionary algorithms;
private MacUtilities()
{
}
static MacUtilities()
{
algorithms = Platform.CreateHashtable();
algorithms[IanaObjectIdentifiers.HmacMD5.Id] = "HMAC-MD5";
algorithms[IanaObjectIdentifiers.HmacRipeMD160.Id] = "HMAC-RIPEMD160";
algorithms[IanaObjectIdentifiers.HmacSha1.Id] = "HMAC-SHA1";
algorithms[IanaObjectIdentifiers.HmacTiger.Id] = "HMAC-TIGER";
algorithms[PkcsObjectIdentifiers.IdHmacWithSha1.Id] = "HMAC-SHA1";
algorithms[PkcsObjectIdentifiers.IdHmacWithSha224.Id] = "HMAC-SHA224";
algorithms[PkcsObjectIdentifiers.IdHmacWithSha256.Id] = "HMAC-SHA256";
algorithms[PkcsObjectIdentifiers.IdHmacWithSha384.Id] = "HMAC-SHA384";
algorithms[PkcsObjectIdentifiers.IdHmacWithSha512.Id] = "HMAC-SHA512";
algorithms[NistObjectIdentifiers.IdHMacWithSha3_224.Id] = "HMAC-SHA3-224";
algorithms[NistObjectIdentifiers.IdHMacWithSha3_256.Id] = "HMAC-SHA3-256";
algorithms[NistObjectIdentifiers.IdHMacWithSha3_384.Id] = "HMAC-SHA3-384";
algorithms[NistObjectIdentifiers.IdHMacWithSha3_512.Id] = "HMAC-SHA3-512";
algorithms[RosstandartObjectIdentifiers.id_tc26_hmac_gost_3411_12_256.Id] = "HMAC-GOST3411-2012-256";
algorithms[RosstandartObjectIdentifiers.id_tc26_hmac_gost_3411_12_512.Id] = "HMAC-GOST3411-2012-512";
algorithms["DES"] = "DESMAC";
algorithms["DES/CFB8"] = "DESMAC/CFB8";
algorithms["DES64"] = "DESMAC64";
algorithms["DESEDE"] = "DESEDEMAC";
algorithms[PkcsObjectIdentifiers.DesEde3Cbc.Id] = "DESEDEMAC";
algorithms["DESEDE/CFB8"] = "DESEDEMAC/CFB8";
algorithms["DESISO9797MAC"] = "DESWITHISO9797";
algorithms["DESEDE64"] = "DESEDEMAC64";
algorithms["DESEDE64WITHISO7816-4PADDING"] = "DESEDEMAC64WITHISO7816-4PADDING";
algorithms["DESEDEISO9797ALG1MACWITHISO7816-4PADDING"] = "DESEDEMAC64WITHISO7816-4PADDING";
algorithms["DESEDEISO9797ALG1WITHISO7816-4PADDING"] = "DESEDEMAC64WITHISO7816-4PADDING";
algorithms["ISO9797ALG3"] = "ISO9797ALG3MAC";
algorithms["ISO9797ALG3MACWITHISO7816-4PADDING"] = "ISO9797ALG3WITHISO7816-4PADDING";
algorithms["SKIPJACK"] = "SKIPJACKMAC";
algorithms["SKIPJACK/CFB8"] = "SKIPJACKMAC/CFB8";
algorithms["IDEA"] = "IDEAMAC";
algorithms["IDEA/CFB8"] = "IDEAMAC/CFB8";
algorithms["RC2"] = "RC2MAC";
algorithms["RC2/CFB8"] = "RC2MAC/CFB8";
algorithms["RC5"] = "RC5MAC";
algorithms["RC5/CFB8"] = "RC5MAC/CFB8";
algorithms["GOST28147"] = "GOST28147MAC";
algorithms["VMPC"] = "VMPCMAC";
algorithms["VMPC-MAC"] = "VMPCMAC";
algorithms["SIPHASH"] = "SIPHASH-2-4";
algorithms["PBEWITHHMACSHA"] = "PBEWITHHMACSHA1";
algorithms["1.3.14.3.2.26"] = "PBEWITHHMACSHA1";
}
public static IMac GetMac(DerObjectIdentifier id)
{
return GetMac(id.Id);
}
public static IMac GetMac(string algorithm)
{
string text = Platform.ToUpperInvariant(algorithm);
string text2 = (string)algorithms[text];
if (text2 == null)
{
text2 = text;
}
if (Platform.StartsWith(text2, "PBEWITH"))
{
text2 = text2.Substring("PBEWITH".Length);
}
if (Platform.StartsWith(text2, "HMAC"))
{
string algorithm2 = ((!Platform.StartsWith(text2, "HMAC-") && !Platform.StartsWith(text2, "HMAC/")) ? text2.Substring(4) : text2.Substring(5));
return new HMac(DigestUtilities.GetDigest(algorithm2));
}
switch (text2)
{
case "AESCMAC":
return new CMac(new AesEngine());
case "DESMAC":
return new CbcBlockCipherMac(new DesEngine());
case "DESMAC/CFB8":
return new CfbBlockCipherMac(new DesEngine());
case "DESMAC64":
return new CbcBlockCipherMac(new DesEngine(), 64);
case "DESEDECMAC":
return new CMac(new DesEdeEngine());
case "DESEDEMAC":
return new CbcBlockCipherMac(new DesEdeEngine());
case "DESEDEMAC/CFB8":
return new CfbBlockCipherMac(new DesEdeEngine());
case "DESEDEMAC64":
return new CbcBlockCipherMac(new DesEdeEngine(), 64);
case "DESEDEMAC64WITHISO7816-4PADDING":
return new CbcBlockCipherMac(new DesEdeEngine(), 64, new ISO7816d4Padding());
case "DESWITHISO9797":
case "ISO9797ALG3MAC":
return new ISO9797Alg3Mac(new DesEngine());
case "ISO9797ALG3WITHISO7816-4PADDING":
return new ISO9797Alg3Mac(new DesEngine(), new ISO7816d4Padding());
case "SKIPJACKMAC":
return new CbcBlockCipherMac(new SkipjackEngine());
case "SKIPJACKMAC/CFB8":
return new CfbBlockCipherMac(new SkipjackEngine());
case "IDEAMAC":
return new CbcBlockCipherMac(new IdeaEngine());
case "IDEAMAC/CFB8":
return new CfbBlockCipherMac(new IdeaEngine());
case "RC2MAC":
return new CbcBlockCipherMac(new RC2Engine());
case "RC2MAC/CFB8":
return new CfbBlockCipherMac(new RC2Engine());
case "RC5MAC":
return new CbcBlockCipherMac(new RC532Engine());
case "RC5MAC/CFB8":
return new CfbBlockCipherMac(new RC532Engine());
case "GOST28147MAC":
return new Gost28147Mac();
case "VMPCMAC":
return new VmpcMac();
case "SIPHASH-2-4":
return new SipHash();
default:
throw new SecurityUtilityException("Mac " + text2 + " not recognised.");
}
}
public static string GetAlgorithmName(DerObjectIdentifier oid)
{
return (string)algorithms[oid.Id];
}
public static byte[] CalculateMac(string algorithm, ICipherParameters cp, byte[] input)
{
IMac mac = GetMac(algorithm);
mac.Init(cp);
mac.BlockUpdate(input, 0, input.Length);
return DoFinal(mac);
}
public static byte[] DoFinal(IMac mac)
{
byte[] array = new byte[mac.GetMacSize()];
mac.DoFinal(array, 0);
return array;
}
public static byte[] DoFinal(IMac mac, byte[] input)
{
mac.BlockUpdate(input, 0, input.Length);
return DoFinal(mac);
}
}

View File

@@ -0,0 +1,22 @@
using System;
namespace Org.BouncyCastle.Security;
[Serializable]
[Obsolete("Never thrown")]
public class NoSuchAlgorithmException : GeneralSecurityException
{
public NoSuchAlgorithmException()
{
}
public NoSuchAlgorithmException(string message)
: base(message)
{
}
public NoSuchAlgorithmException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,250 @@
using System;
using System.Collections;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.Kisa;
using Org.BouncyCastle.Asn1.Misc;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Ntt;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Security;
public sealed class ParameterUtilities
{
private static readonly IDictionary algorithms;
private static readonly IDictionary basicIVSizes;
private ParameterUtilities()
{
}
static ParameterUtilities()
{
algorithms = Platform.CreateHashtable();
basicIVSizes = Platform.CreateHashtable();
AddAlgorithm("AES", "AESWRAP");
AddAlgorithm("AES128", "2.16.840.1.101.3.4.2", NistObjectIdentifiers.IdAes128Cbc, NistObjectIdentifiers.IdAes128Cfb, NistObjectIdentifiers.IdAes128Ecb, NistObjectIdentifiers.IdAes128Ofb, NistObjectIdentifiers.IdAes128Wrap);
AddAlgorithm("AES192", "2.16.840.1.101.3.4.22", NistObjectIdentifiers.IdAes192Cbc, NistObjectIdentifiers.IdAes192Cfb, NistObjectIdentifiers.IdAes192Ecb, NistObjectIdentifiers.IdAes192Ofb, NistObjectIdentifiers.IdAes192Wrap);
AddAlgorithm("AES256", "2.16.840.1.101.3.4.42", NistObjectIdentifiers.IdAes256Cbc, NistObjectIdentifiers.IdAes256Cfb, NistObjectIdentifiers.IdAes256Ecb, NistObjectIdentifiers.IdAes256Ofb, NistObjectIdentifiers.IdAes256Wrap);
AddAlgorithm("BLOWFISH", "1.3.6.1.4.1.3029.1.2");
AddAlgorithm("CAMELLIA", "CAMELLIAWRAP");
AddAlgorithm("CAMELLIA128", NttObjectIdentifiers.IdCamellia128Cbc, NttObjectIdentifiers.IdCamellia128Wrap);
AddAlgorithm("CAMELLIA192", NttObjectIdentifiers.IdCamellia192Cbc, NttObjectIdentifiers.IdCamellia192Wrap);
AddAlgorithm("CAMELLIA256", NttObjectIdentifiers.IdCamellia256Cbc, NttObjectIdentifiers.IdCamellia256Wrap);
AddAlgorithm("CAST5", "1.2.840.113533.7.66.10");
AddAlgorithm("CAST6");
AddAlgorithm("DES", OiwObjectIdentifiers.DesCbc, OiwObjectIdentifiers.DesCfb, OiwObjectIdentifiers.DesEcb, OiwObjectIdentifiers.DesOfb);
AddAlgorithm("DESEDE", "DESEDEWRAP", "TDEA", OiwObjectIdentifiers.DesEde, PkcsObjectIdentifiers.IdAlgCms3DesWrap);
AddAlgorithm("DESEDE3", PkcsObjectIdentifiers.DesEde3Cbc);
AddAlgorithm("GOST28147", "GOST", "GOST-28147", CryptoProObjectIdentifiers.GostR28147Cbc);
AddAlgorithm("HC128");
AddAlgorithm("HC256");
AddAlgorithm("IDEA", "1.3.6.1.4.1.188.7.1.1.2");
AddAlgorithm("NOEKEON");
AddAlgorithm("RC2", PkcsObjectIdentifiers.RC2Cbc, PkcsObjectIdentifiers.IdAlgCmsRC2Wrap);
AddAlgorithm("RC4", "ARC4", "1.2.840.113549.3.4");
AddAlgorithm("RC5", "RC5-32");
AddAlgorithm("RC5-64");
AddAlgorithm("RC6");
AddAlgorithm("RIJNDAEL");
AddAlgorithm("SALSA20");
AddAlgorithm("SEED", KisaObjectIdentifiers.IdNpkiAppCmsSeedWrap, KisaObjectIdentifiers.IdSeedCbc);
AddAlgorithm("SERPENT");
AddAlgorithm("SKIPJACK");
AddAlgorithm("SM4");
AddAlgorithm("TEA");
AddAlgorithm("THREEFISH-256");
AddAlgorithm("THREEFISH-512");
AddAlgorithm("THREEFISH-1024");
AddAlgorithm("TNEPRES");
AddAlgorithm("TWOFISH");
AddAlgorithm("VMPC");
AddAlgorithm("VMPC-KSA3");
AddAlgorithm("XTEA");
AddBasicIVSizeEntries(8, "BLOWFISH", "DES", "DESEDE", "DESEDE3");
AddBasicIVSizeEntries(16, "AES", "AES128", "AES192", "AES256", "CAMELLIA", "CAMELLIA128", "CAMELLIA192", "CAMELLIA256", "NOEKEON", "SEED", "SM4");
}
private static void AddAlgorithm(string canonicalName, params object[] aliases)
{
algorithms[canonicalName] = canonicalName;
foreach (object obj in aliases)
{
algorithms[obj.ToString()] = canonicalName;
}
}
private static void AddBasicIVSizeEntries(int size, params string[] algorithms)
{
foreach (string key in algorithms)
{
basicIVSizes.Add(key, size);
}
}
public static string GetCanonicalAlgorithmName(string algorithm)
{
return (string)algorithms[Platform.ToUpperInvariant(algorithm)];
}
public static KeyParameter CreateKeyParameter(DerObjectIdentifier algOid, byte[] keyBytes)
{
return CreateKeyParameter(algOid.Id, keyBytes, 0, keyBytes.Length);
}
public static KeyParameter CreateKeyParameter(string algorithm, byte[] keyBytes)
{
return CreateKeyParameter(algorithm, keyBytes, 0, keyBytes.Length);
}
public static KeyParameter CreateKeyParameter(DerObjectIdentifier algOid, byte[] keyBytes, int offset, int length)
{
return CreateKeyParameter(algOid.Id, keyBytes, offset, length);
}
public static KeyParameter CreateKeyParameter(string algorithm, byte[] keyBytes, int offset, int length)
{
if (algorithm == null)
{
throw new ArgumentNullException("algorithm");
}
string canonicalAlgorithmName = GetCanonicalAlgorithmName(algorithm);
if (canonicalAlgorithmName == null)
{
throw new SecurityUtilityException("Algorithm " + algorithm + " not recognised.");
}
switch (canonicalAlgorithmName)
{
case "DES":
return new DesParameters(keyBytes, offset, length);
case "DESEDE":
case "DESEDE3":
return new DesEdeParameters(keyBytes, offset, length);
case "RC2":
return new RC2Parameters(keyBytes, offset, length);
default:
return new KeyParameter(keyBytes, offset, length);
}
}
public static ICipherParameters GetCipherParameters(DerObjectIdentifier algOid, ICipherParameters key, Asn1Object asn1Params)
{
return GetCipherParameters(algOid.Id, key, asn1Params);
}
public static ICipherParameters GetCipherParameters(string algorithm, ICipherParameters key, Asn1Object asn1Params)
{
if (algorithm == null)
{
throw new ArgumentNullException("algorithm");
}
string canonicalAlgorithmName = GetCanonicalAlgorithmName(algorithm);
if (canonicalAlgorithmName == null)
{
throw new SecurityUtilityException("Algorithm " + algorithm + " not recognised.");
}
byte[] array = null;
try
{
int num = FindBasicIVSize(canonicalAlgorithmName);
if (num == -1)
{
switch (canonicalAlgorithmName)
{
default:
goto end_IL_0030;
case "RIJNDAEL":
case "SKIPJACK":
case "TWOFISH":
break;
case "CAST5":
array = Cast5CbcParameters.GetInstance(asn1Params).GetIV();
goto end_IL_0030;
case "IDEA":
array = IdeaCbcPar.GetInstance(asn1Params).GetIV();
goto end_IL_0030;
case "RC2":
array = RC2CbcParameter.GetInstance(asn1Params).GetIV();
goto end_IL_0030;
}
}
array = ((Asn1OctetString)asn1Params).GetOctets();
end_IL_0030:;
}
catch (Exception innerException)
{
throw new ArgumentException("Could not process ASN.1 parameters", innerException);
}
if (array != null)
{
return new ParametersWithIV(key, array);
}
throw new SecurityUtilityException("Algorithm " + algorithm + " not recognised.");
}
public static Asn1Encodable GenerateParameters(DerObjectIdentifier algID, SecureRandom random)
{
return GenerateParameters(algID.Id, random);
}
public static Asn1Encodable GenerateParameters(string algorithm, SecureRandom random)
{
if (algorithm == null)
{
throw new ArgumentNullException("algorithm");
}
string canonicalAlgorithmName = GetCanonicalAlgorithmName(algorithm);
if (canonicalAlgorithmName == null)
{
throw new SecurityUtilityException("Algorithm " + algorithm + " not recognised.");
}
int num = FindBasicIVSize(canonicalAlgorithmName);
if (num != -1)
{
return CreateIVOctetString(random, num);
}
return canonicalAlgorithmName switch
{
"CAST5" => new Cast5CbcParameters(CreateIV(random, 8), 128),
"IDEA" => new IdeaCbcPar(CreateIV(random, 8)),
"RC2" => new RC2CbcParameter(CreateIV(random, 8)),
_ => throw new SecurityUtilityException("Algorithm " + algorithm + " not recognised."),
};
}
public static ICipherParameters WithRandom(ICipherParameters cp, SecureRandom random)
{
if (random != null)
{
cp = new ParametersWithRandom(cp, random);
}
return cp;
}
private static Asn1OctetString CreateIVOctetString(SecureRandom random, int ivLength)
{
return new DerOctetString(CreateIV(random, ivLength));
}
private static byte[] CreateIV(SecureRandom random, int ivLength)
{
byte[] array = new byte[ivLength];
random.NextBytes(array);
return array;
}
private static int FindBasicIVSize(string canonicalName)
{
if (!basicIVSizes.Contains(canonicalName))
{
return -1;
}
return (int)basicIVSizes[canonicalName];
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Security;
[Serializable]
public class PasswordException : IOException
{
public PasswordException(string message)
: base(message)
{
}
public PasswordException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,558 @@
using System;
using System.Collections;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.BC;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.TeleTrust;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Security;
public sealed class PbeUtilities
{
private const string Pkcs5S1 = "Pkcs5S1";
private const string Pkcs5S2 = "Pkcs5S2";
private const string Pkcs12 = "Pkcs12";
private const string OpenSsl = "OpenSsl";
private static readonly IDictionary algorithms;
private static readonly IDictionary algorithmType;
private static readonly IDictionary oids;
public static ICollection Algorithms => oids.Keys;
private PbeUtilities()
{
}
static PbeUtilities()
{
algorithms = Platform.CreateHashtable();
algorithmType = Platform.CreateHashtable();
oids = Platform.CreateHashtable();
algorithms["PKCS5SCHEME1"] = "Pkcs5scheme1";
algorithms["PKCS5SCHEME2"] = "Pkcs5scheme2";
algorithms[PkcsObjectIdentifiers.IdPbeS2.Id] = "Pkcs5scheme2";
algorithms["PBEWITHMD2ANDDES-CBC"] = "PBEwithMD2andDES-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithMD2AndDesCbc.Id] = "PBEwithMD2andDES-CBC";
algorithms["PBEWITHMD2ANDRC2-CBC"] = "PBEwithMD2andRC2-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithMD2AndRC2Cbc.Id] = "PBEwithMD2andRC2-CBC";
algorithms["PBEWITHMD5ANDDES-CBC"] = "PBEwithMD5andDES-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithMD5AndDesCbc.Id] = "PBEwithMD5andDES-CBC";
algorithms["PBEWITHMD5ANDRC2-CBC"] = "PBEwithMD5andRC2-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithMD5AndRC2Cbc.Id] = "PBEwithMD5andRC2-CBC";
algorithms["PBEWITHSHA1ANDDES"] = "PBEwithSHA-1andDES-CBC";
algorithms["PBEWITHSHA-1ANDDES"] = "PBEwithSHA-1andDES-CBC";
algorithms["PBEWITHSHA1ANDDES-CBC"] = "PBEwithSHA-1andDES-CBC";
algorithms["PBEWITHSHA-1ANDDES-CBC"] = "PBEwithSHA-1andDES-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithSha1AndDesCbc.Id] = "PBEwithSHA-1andDES-CBC";
algorithms["PBEWITHSHA1ANDRC2"] = "PBEwithSHA-1andRC2-CBC";
algorithms["PBEWITHSHA-1ANDRC2"] = "PBEwithSHA-1andRC2-CBC";
algorithms["PBEWITHSHA1ANDRC2-CBC"] = "PBEwithSHA-1andRC2-CBC";
algorithms["PBEWITHSHA-1ANDRC2-CBC"] = "PBEwithSHA-1andRC2-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithSha1AndRC2Cbc.Id] = "PBEwithSHA-1andRC2-CBC";
algorithms["PKCS12"] = "Pkcs12";
algorithms[BCObjectIdentifiers.bc_pbe_sha1_pkcs12_aes128_cbc.Id] = "PBEwithSHA-1and128bitAES-CBC-BC";
algorithms[BCObjectIdentifiers.bc_pbe_sha1_pkcs12_aes192_cbc.Id] = "PBEwithSHA-1and192bitAES-CBC-BC";
algorithms[BCObjectIdentifiers.bc_pbe_sha1_pkcs12_aes256_cbc.Id] = "PBEwithSHA-1and256bitAES-CBC-BC";
algorithms[BCObjectIdentifiers.bc_pbe_sha256_pkcs12_aes128_cbc.Id] = "PBEwithSHA-256and128bitAES-CBC-BC";
algorithms[BCObjectIdentifiers.bc_pbe_sha256_pkcs12_aes192_cbc.Id] = "PBEwithSHA-256and192bitAES-CBC-BC";
algorithms[BCObjectIdentifiers.bc_pbe_sha256_pkcs12_aes256_cbc.Id] = "PBEwithSHA-256and256bitAES-CBC-BC";
algorithms["PBEWITHSHAAND128BITRC4"] = "PBEwithSHA-1and128bitRC4";
algorithms["PBEWITHSHA1AND128BITRC4"] = "PBEwithSHA-1and128bitRC4";
algorithms["PBEWITHSHA-1AND128BITRC4"] = "PBEwithSHA-1and128bitRC4";
algorithms[PkcsObjectIdentifiers.PbeWithShaAnd128BitRC4.Id] = "PBEwithSHA-1and128bitRC4";
algorithms["PBEWITHSHAAND40BITRC4"] = "PBEwithSHA-1and40bitRC4";
algorithms["PBEWITHSHA1AND40BITRC4"] = "PBEwithSHA-1and40bitRC4";
algorithms["PBEWITHSHA-1AND40BITRC4"] = "PBEwithSHA-1and40bitRC4";
algorithms[PkcsObjectIdentifiers.PbeWithShaAnd40BitRC4.Id] = "PBEwithSHA-1and40bitRC4";
algorithms["PBEWITHSHAAND3-KEYDESEDE-CBC"] = "PBEwithSHA-1and3-keyDESEDE-CBC";
algorithms["PBEWITHSHAAND3-KEYTRIPLEDES-CBC"] = "PBEwithSHA-1and3-keyDESEDE-CBC";
algorithms["PBEWITHSHA1AND3-KEYDESEDE-CBC"] = "PBEwithSHA-1and3-keyDESEDE-CBC";
algorithms["PBEWITHSHA1AND3-KEYTRIPLEDES-CBC"] = "PBEwithSHA-1and3-keyDESEDE-CBC";
algorithms["PBEWITHSHA-1AND3-KEYDESEDE-CBC"] = "PBEwithSHA-1and3-keyDESEDE-CBC";
algorithms["PBEWITHSHA-1AND3-KEYTRIPLEDES-CBC"] = "PBEwithSHA-1and3-keyDESEDE-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithShaAnd3KeyTripleDesCbc.Id] = "PBEwithSHA-1and3-keyDESEDE-CBC";
algorithms["PBEWITHSHAAND2-KEYDESEDE-CBC"] = "PBEwithSHA-1and2-keyDESEDE-CBC";
algorithms["PBEWITHSHAAND2-KEYTRIPLEDES-CBC"] = "PBEwithSHA-1and2-keyDESEDE-CBC";
algorithms["PBEWITHSHA1AND2-KEYDESEDE-CBC"] = "PBEwithSHA-1and2-keyDESEDE-CBC";
algorithms["PBEWITHSHA1AND2-KEYTRIPLEDES-CBC"] = "PBEwithSHA-1and2-keyDESEDE-CBC";
algorithms["PBEWITHSHA-1AND2-KEYDESEDE-CBC"] = "PBEwithSHA-1and2-keyDESEDE-CBC";
algorithms["PBEWITHSHA-1AND2-KEYTRIPLEDES-CBC"] = "PBEwithSHA-1and2-keyDESEDE-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithShaAnd2KeyTripleDesCbc.Id] = "PBEwithSHA-1and2-keyDESEDE-CBC";
algorithms["PBEWITHSHAAND128BITRC2-CBC"] = "PBEwithSHA-1and128bitRC2-CBC";
algorithms["PBEWITHSHA1AND128BITRC2-CBC"] = "PBEwithSHA-1and128bitRC2-CBC";
algorithms["PBEWITHSHA-1AND128BITRC2-CBC"] = "PBEwithSHA-1and128bitRC2-CBC";
algorithms[PkcsObjectIdentifiers.PbeWithShaAnd128BitRC2Cbc.Id] = "PBEwithSHA-1and128bitRC2-CBC";
algorithms["PBEWITHSHAAND40BITRC2-CBC"] = "PBEwithSHA-1and40bitRC2-CBC";
algorithms["PBEWITHSHA1AND40BITRC2-CBC"] = "PBEwithSHA-1and40bitRC2-CBC";
algorithms["PBEWITHSHA-1AND40BITRC2-CBC"] = "PBEwithSHA-1and40bitRC2-CBC";
algorithms[PkcsObjectIdentifiers.PbewithShaAnd40BitRC2Cbc.Id] = "PBEwithSHA-1and40bitRC2-CBC";
algorithms["PBEWITHSHAAND128BITAES-CBC-BC"] = "PBEwithSHA-1and128bitAES-CBC-BC";
algorithms["PBEWITHSHA1AND128BITAES-CBC-BC"] = "PBEwithSHA-1and128bitAES-CBC-BC";
algorithms["PBEWITHSHA-1AND128BITAES-CBC-BC"] = "PBEwithSHA-1and128bitAES-CBC-BC";
algorithms["PBEWITHSHAAND192BITAES-CBC-BC"] = "PBEwithSHA-1and192bitAES-CBC-BC";
algorithms["PBEWITHSHA1AND192BITAES-CBC-BC"] = "PBEwithSHA-1and192bitAES-CBC-BC";
algorithms["PBEWITHSHA-1AND192BITAES-CBC-BC"] = "PBEwithSHA-1and192bitAES-CBC-BC";
algorithms["PBEWITHSHAAND256BITAES-CBC-BC"] = "PBEwithSHA-1and256bitAES-CBC-BC";
algorithms["PBEWITHSHA1AND256BITAES-CBC-BC"] = "PBEwithSHA-1and256bitAES-CBC-BC";
algorithms["PBEWITHSHA-1AND256BITAES-CBC-BC"] = "PBEwithSHA-1and256bitAES-CBC-BC";
algorithms["PBEWITHSHA256AND128BITAES-CBC-BC"] = "PBEwithSHA-256and128bitAES-CBC-BC";
algorithms["PBEWITHSHA-256AND128BITAES-CBC-BC"] = "PBEwithSHA-256and128bitAES-CBC-BC";
algorithms["PBEWITHSHA256AND192BITAES-CBC-BC"] = "PBEwithSHA-256and192bitAES-CBC-BC";
algorithms["PBEWITHSHA-256AND192BITAES-CBC-BC"] = "PBEwithSHA-256and192bitAES-CBC-BC";
algorithms["PBEWITHSHA256AND256BITAES-CBC-BC"] = "PBEwithSHA-256and256bitAES-CBC-BC";
algorithms["PBEWITHSHA-256AND256BITAES-CBC-BC"] = "PBEwithSHA-256and256bitAES-CBC-BC";
algorithms["PBEWITHSHAANDIDEA"] = "PBEwithSHA-1andIDEA-CBC";
algorithms["PBEWITHSHAANDIDEA-CBC"] = "PBEwithSHA-1andIDEA-CBC";
algorithms["PBEWITHSHAANDTWOFISH"] = "PBEwithSHA-1andTWOFISH-CBC";
algorithms["PBEWITHSHAANDTWOFISH-CBC"] = "PBEwithSHA-1andTWOFISH-CBC";
algorithms["PBEWITHHMACSHA1"] = "PBEwithHmacSHA-1";
algorithms["PBEWITHHMACSHA-1"] = "PBEwithHmacSHA-1";
algorithms[OiwObjectIdentifiers.IdSha1.Id] = "PBEwithHmacSHA-1";
algorithms["PBEWITHHMACSHA224"] = "PBEwithHmacSHA-224";
algorithms["PBEWITHHMACSHA-224"] = "PBEwithHmacSHA-224";
algorithms[NistObjectIdentifiers.IdSha224.Id] = "PBEwithHmacSHA-224";
algorithms["PBEWITHHMACSHA256"] = "PBEwithHmacSHA-256";
algorithms["PBEWITHHMACSHA-256"] = "PBEwithHmacSHA-256";
algorithms[NistObjectIdentifiers.IdSha256.Id] = "PBEwithHmacSHA-256";
algorithms["PBEWITHHMACRIPEMD128"] = "PBEwithHmacRipeMD128";
algorithms[TeleTrusTObjectIdentifiers.RipeMD128.Id] = "PBEwithHmacRipeMD128";
algorithms["PBEWITHHMACRIPEMD160"] = "PBEwithHmacRipeMD160";
algorithms[TeleTrusTObjectIdentifiers.RipeMD160.Id] = "PBEwithHmacRipeMD160";
algorithms["PBEWITHHMACRIPEMD256"] = "PBEwithHmacRipeMD256";
algorithms[TeleTrusTObjectIdentifiers.RipeMD256.Id] = "PBEwithHmacRipeMD256";
algorithms["PBEWITHHMACTIGER"] = "PBEwithHmacTiger";
algorithms["PBEWITHMD5AND128BITAES-CBC-OPENSSL"] = "PBEwithMD5and128bitAES-CBC-OpenSSL";
algorithms["PBEWITHMD5AND192BITAES-CBC-OPENSSL"] = "PBEwithMD5and192bitAES-CBC-OpenSSL";
algorithms["PBEWITHMD5AND256BITAES-CBC-OPENSSL"] = "PBEwithMD5and256bitAES-CBC-OpenSSL";
algorithmType["Pkcs5scheme1"] = "Pkcs5S1";
algorithmType["Pkcs5scheme2"] = "Pkcs5S2";
algorithmType["PBEwithMD2andDES-CBC"] = "Pkcs5S1";
algorithmType["PBEwithMD2andRC2-CBC"] = "Pkcs5S1";
algorithmType["PBEwithMD5andDES-CBC"] = "Pkcs5S1";
algorithmType["PBEwithMD5andRC2-CBC"] = "Pkcs5S1";
algorithmType["PBEwithSHA-1andDES-CBC"] = "Pkcs5S1";
algorithmType["PBEwithSHA-1andRC2-CBC"] = "Pkcs5S1";
algorithmType["Pkcs12"] = "Pkcs12";
algorithmType["PBEwithSHA-1and128bitRC4"] = "Pkcs12";
algorithmType["PBEwithSHA-1and40bitRC4"] = "Pkcs12";
algorithmType["PBEwithSHA-1and3-keyDESEDE-CBC"] = "Pkcs12";
algorithmType["PBEwithSHA-1and2-keyDESEDE-CBC"] = "Pkcs12";
algorithmType["PBEwithSHA-1and128bitRC2-CBC"] = "Pkcs12";
algorithmType["PBEwithSHA-1and40bitRC2-CBC"] = "Pkcs12";
algorithmType["PBEwithSHA-1and128bitAES-CBC-BC"] = "Pkcs12";
algorithmType["PBEwithSHA-1and192bitAES-CBC-BC"] = "Pkcs12";
algorithmType["PBEwithSHA-1and256bitAES-CBC-BC"] = "Pkcs12";
algorithmType["PBEwithSHA-256and128bitAES-CBC-BC"] = "Pkcs12";
algorithmType["PBEwithSHA-256and192bitAES-CBC-BC"] = "Pkcs12";
algorithmType["PBEwithSHA-256and256bitAES-CBC-BC"] = "Pkcs12";
algorithmType["PBEwithSHA-1andIDEA-CBC"] = "Pkcs12";
algorithmType["PBEwithSHA-1andTWOFISH-CBC"] = "Pkcs12";
algorithmType["PBEwithHmacSHA-1"] = "Pkcs12";
algorithmType["PBEwithHmacSHA-224"] = "Pkcs12";
algorithmType["PBEwithHmacSHA-256"] = "Pkcs12";
algorithmType["PBEwithHmacRipeMD128"] = "Pkcs12";
algorithmType["PBEwithHmacRipeMD160"] = "Pkcs12";
algorithmType["PBEwithHmacRipeMD256"] = "Pkcs12";
algorithmType["PBEwithHmacTiger"] = "Pkcs12";
algorithmType["PBEwithMD5and128bitAES-CBC-OpenSSL"] = "OpenSsl";
algorithmType["PBEwithMD5and192bitAES-CBC-OpenSSL"] = "OpenSsl";
algorithmType["PBEwithMD5and256bitAES-CBC-OpenSSL"] = "OpenSsl";
oids["PBEwithMD2andDES-CBC"] = PkcsObjectIdentifiers.PbeWithMD2AndDesCbc;
oids["PBEwithMD2andRC2-CBC"] = PkcsObjectIdentifiers.PbeWithMD2AndRC2Cbc;
oids["PBEwithMD5andDES-CBC"] = PkcsObjectIdentifiers.PbeWithMD5AndDesCbc;
oids["PBEwithMD5andRC2-CBC"] = PkcsObjectIdentifiers.PbeWithMD5AndRC2Cbc;
oids["PBEwithSHA-1andDES-CBC"] = PkcsObjectIdentifiers.PbeWithSha1AndDesCbc;
oids["PBEwithSHA-1andRC2-CBC"] = PkcsObjectIdentifiers.PbeWithSha1AndRC2Cbc;
oids["PBEwithSHA-1and128bitRC4"] = PkcsObjectIdentifiers.PbeWithShaAnd128BitRC4;
oids["PBEwithSHA-1and40bitRC4"] = PkcsObjectIdentifiers.PbeWithShaAnd40BitRC4;
oids["PBEwithSHA-1and3-keyDESEDE-CBC"] = PkcsObjectIdentifiers.PbeWithShaAnd3KeyTripleDesCbc;
oids["PBEwithSHA-1and2-keyDESEDE-CBC"] = PkcsObjectIdentifiers.PbeWithShaAnd2KeyTripleDesCbc;
oids["PBEwithSHA-1and128bitRC2-CBC"] = PkcsObjectIdentifiers.PbeWithShaAnd128BitRC2Cbc;
oids["PBEwithSHA-1and40bitRC2-CBC"] = PkcsObjectIdentifiers.PbewithShaAnd40BitRC2Cbc;
oids["PBEwithHmacSHA-1"] = OiwObjectIdentifiers.IdSha1;
oids["PBEwithHmacSHA-224"] = NistObjectIdentifiers.IdSha224;
oids["PBEwithHmacSHA-256"] = NistObjectIdentifiers.IdSha256;
oids["PBEwithHmacRipeMD128"] = TeleTrusTObjectIdentifiers.RipeMD128;
oids["PBEwithHmacRipeMD160"] = TeleTrusTObjectIdentifiers.RipeMD160;
oids["PBEwithHmacRipeMD256"] = TeleTrusTObjectIdentifiers.RipeMD256;
oids["Pkcs5scheme2"] = PkcsObjectIdentifiers.IdPbeS2;
}
private static PbeParametersGenerator MakePbeGenerator(string type, IDigest digest, byte[] key, byte[] salt, int iterationCount)
{
PbeParametersGenerator pbeParametersGenerator;
if (type.Equals("Pkcs5S1"))
{
pbeParametersGenerator = new Pkcs5S1ParametersGenerator(digest);
}
else if (type.Equals("Pkcs5S2"))
{
pbeParametersGenerator = new Pkcs5S2ParametersGenerator();
}
else if (type.Equals("Pkcs12"))
{
pbeParametersGenerator = new Pkcs12ParametersGenerator(digest);
}
else
{
if (!type.Equals("OpenSsl"))
{
throw new ArgumentException("Unknown PBE type: " + type, "type");
}
pbeParametersGenerator = new OpenSslPbeParametersGenerator();
}
pbeParametersGenerator.Init(key, salt, iterationCount);
return pbeParametersGenerator;
}
public static DerObjectIdentifier GetObjectIdentifier(string mechanism)
{
mechanism = (string)algorithms[Platform.ToUpperInvariant(mechanism)];
if (mechanism != null)
{
return (DerObjectIdentifier)oids[mechanism];
}
return null;
}
public static bool IsPkcs12(string algorithm)
{
string text = (string)algorithms[Platform.ToUpperInvariant(algorithm)];
if (text != null)
{
return "Pkcs12".Equals(algorithmType[text]);
}
return false;
}
public static bool IsPkcs5Scheme1(string algorithm)
{
string text = (string)algorithms[Platform.ToUpperInvariant(algorithm)];
if (text != null)
{
return "Pkcs5S1".Equals(algorithmType[text]);
}
return false;
}
public static bool IsPkcs5Scheme2(string algorithm)
{
string text = (string)algorithms[Platform.ToUpperInvariant(algorithm)];
if (text != null)
{
return "Pkcs5S2".Equals(algorithmType[text]);
}
return false;
}
public static bool IsOpenSsl(string algorithm)
{
string text = (string)algorithms[Platform.ToUpperInvariant(algorithm)];
if (text != null)
{
return "OpenSsl".Equals(algorithmType[text]);
}
return false;
}
public static bool IsPbeAlgorithm(string algorithm)
{
string text = (string)algorithms[Platform.ToUpperInvariant(algorithm)];
if (text != null)
{
return algorithmType[text] != null;
}
return false;
}
public static Asn1Encodable GenerateAlgorithmParameters(DerObjectIdentifier algorithmOid, byte[] salt, int iterationCount)
{
return GenerateAlgorithmParameters(algorithmOid.Id, salt, iterationCount);
}
public static Asn1Encodable GenerateAlgorithmParameters(string algorithm, byte[] salt, int iterationCount)
{
if (IsPkcs12(algorithm))
{
return new Pkcs12PbeParams(salt, iterationCount);
}
if (IsPkcs5Scheme2(algorithm))
{
return new Pbkdf2Params(salt, iterationCount);
}
return new PbeParameter(salt, iterationCount);
}
public static ICipherParameters GenerateCipherParameters(DerObjectIdentifier algorithmOid, char[] password, Asn1Encodable pbeParameters)
{
return GenerateCipherParameters(algorithmOid.Id, password, wrongPkcs12Zero: false, pbeParameters);
}
public static ICipherParameters GenerateCipherParameters(DerObjectIdentifier algorithmOid, char[] password, bool wrongPkcs12Zero, Asn1Encodable pbeParameters)
{
return GenerateCipherParameters(algorithmOid.Id, password, wrongPkcs12Zero, pbeParameters);
}
public static ICipherParameters GenerateCipherParameters(AlgorithmIdentifier algID, char[] password)
{
return GenerateCipherParameters(algID.Algorithm.Id, password, wrongPkcs12Zero: false, algID.Parameters);
}
public static ICipherParameters GenerateCipherParameters(AlgorithmIdentifier algID, char[] password, bool wrongPkcs12Zero)
{
return GenerateCipherParameters(algID.Algorithm.Id, password, wrongPkcs12Zero, algID.Parameters);
}
public static ICipherParameters GenerateCipherParameters(string algorithm, char[] password, Asn1Encodable pbeParameters)
{
return GenerateCipherParameters(algorithm, password, wrongPkcs12Zero: false, pbeParameters);
}
public static ICipherParameters GenerateCipherParameters(string algorithm, char[] password, bool wrongPkcs12Zero, Asn1Encodable pbeParameters)
{
string text = (string)algorithms[Platform.ToUpperInvariant(algorithm)];
byte[] array = null;
byte[] salt = null;
int iterationCount = 0;
if (IsPkcs12(text))
{
Pkcs12PbeParams instance = Pkcs12PbeParams.GetInstance(pbeParameters);
salt = instance.GetIV();
iterationCount = instance.Iterations.IntValue;
array = PbeParametersGenerator.Pkcs12PasswordToBytes(password, wrongPkcs12Zero);
}
else if (!IsPkcs5Scheme2(text))
{
PbeParameter instance2 = PbeParameter.GetInstance(pbeParameters);
salt = instance2.GetSalt();
iterationCount = instance2.IterationCount.IntValue;
array = PbeParametersGenerator.Pkcs5PasswordToBytes(password);
}
ICipherParameters parameters = null;
if (IsPkcs5Scheme2(text))
{
PbeS2Parameters instance3 = PbeS2Parameters.GetInstance(pbeParameters.ToAsn1Object());
AlgorithmIdentifier encryptionScheme = instance3.EncryptionScheme;
DerObjectIdentifier algorithm2 = encryptionScheme.Algorithm;
Asn1Object obj = encryptionScheme.Parameters.ToAsn1Object();
Pbkdf2Params instance4 = Pbkdf2Params.GetInstance(instance3.KeyDerivationFunc.Parameters.ToAsn1Object());
byte[] array2;
if (algorithm2.Equals(PkcsObjectIdentifiers.RC2Cbc))
{
RC2CbcParameter instance5 = RC2CbcParameter.GetInstance(obj);
array2 = instance5.GetIV();
}
else
{
array2 = Asn1OctetString.GetInstance(obj).GetOctets();
}
salt = instance4.GetSalt();
iterationCount = instance4.IterationCount.IntValue;
array = PbeParametersGenerator.Pkcs5PasswordToBytes(password);
int keySize = ((instance4.KeyLength != null) ? (instance4.KeyLength.IntValue * 8) : GeneratorUtilities.GetDefaultKeySize(algorithm2));
PbeParametersGenerator pbeParametersGenerator = MakePbeGenerator((string)algorithmType[text], null, array, salt, iterationCount);
parameters = pbeParametersGenerator.GenerateDerivedParameters(algorithm2.Id, keySize);
if (array2 != null && !Arrays.AreEqual(array2, new byte[array2.Length]))
{
parameters = new ParametersWithIV(parameters, array2);
}
}
else if (Platform.StartsWith(text, "PBEwithSHA-1"))
{
PbeParametersGenerator pbeParametersGenerator2 = MakePbeGenerator((string)algorithmType[text], new Sha1Digest(), array, salt, iterationCount);
if (text.Equals("PBEwithSHA-1and128bitAES-CBC-BC"))
{
parameters = pbeParametersGenerator2.GenerateDerivedParameters("AES", 128, 128);
}
else if (text.Equals("PBEwithSHA-1and192bitAES-CBC-BC"))
{
parameters = pbeParametersGenerator2.GenerateDerivedParameters("AES", 192, 128);
}
else if (text.Equals("PBEwithSHA-1and256bitAES-CBC-BC"))
{
parameters = pbeParametersGenerator2.GenerateDerivedParameters("AES", 256, 128);
}
else if (text.Equals("PBEwithSHA-1and128bitRC4"))
{
parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC4", 128);
}
else if (text.Equals("PBEwithSHA-1and40bitRC4"))
{
parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC4", 40);
}
else if (text.Equals("PBEwithSHA-1and3-keyDESEDE-CBC"))
{
parameters = pbeParametersGenerator2.GenerateDerivedParameters("DESEDE", 192, 64);
}
else if (text.Equals("PBEwithSHA-1and2-keyDESEDE-CBC"))
{
parameters = pbeParametersGenerator2.GenerateDerivedParameters("DESEDE", 128, 64);
}
else if (text.Equals("PBEwithSHA-1and128bitRC2-CBC"))
{
parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC2", 128, 64);
}
else if (text.Equals("PBEwithSHA-1and40bitRC2-CBC"))
{
parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC2", 40, 64);
}
else if (text.Equals("PBEwithSHA-1andDES-CBC"))
{
parameters = pbeParametersGenerator2.GenerateDerivedParameters("DES", 64, 64);
}
else if (text.Equals("PBEwithSHA-1andRC2-CBC"))
{
parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC2", 64, 64);
}
}
else if (Platform.StartsWith(text, "PBEwithSHA-256"))
{
PbeParametersGenerator pbeParametersGenerator3 = MakePbeGenerator((string)algorithmType[text], new Sha256Digest(), array, salt, iterationCount);
if (text.Equals("PBEwithSHA-256and128bitAES-CBC-BC"))
{
parameters = pbeParametersGenerator3.GenerateDerivedParameters("AES", 128, 128);
}
else if (text.Equals("PBEwithSHA-256and192bitAES-CBC-BC"))
{
parameters = pbeParametersGenerator3.GenerateDerivedParameters("AES", 192, 128);
}
else if (text.Equals("PBEwithSHA-256and256bitAES-CBC-BC"))
{
parameters = pbeParametersGenerator3.GenerateDerivedParameters("AES", 256, 128);
}
}
else if (Platform.StartsWith(text, "PBEwithMD5"))
{
PbeParametersGenerator pbeParametersGenerator4 = MakePbeGenerator((string)algorithmType[text], new MD5Digest(), array, salt, iterationCount);
if (text.Equals("PBEwithMD5andDES-CBC"))
{
parameters = pbeParametersGenerator4.GenerateDerivedParameters("DES", 64, 64);
}
else if (text.Equals("PBEwithMD5andRC2-CBC"))
{
parameters = pbeParametersGenerator4.GenerateDerivedParameters("RC2", 64, 64);
}
else if (text.Equals("PBEwithMD5and128bitAES-CBC-OpenSSL"))
{
parameters = pbeParametersGenerator4.GenerateDerivedParameters("AES", 128, 128);
}
else if (text.Equals("PBEwithMD5and192bitAES-CBC-OpenSSL"))
{
parameters = pbeParametersGenerator4.GenerateDerivedParameters("AES", 192, 128);
}
else if (text.Equals("PBEwithMD5and256bitAES-CBC-OpenSSL"))
{
parameters = pbeParametersGenerator4.GenerateDerivedParameters("AES", 256, 128);
}
}
else if (Platform.StartsWith(text, "PBEwithMD2"))
{
PbeParametersGenerator pbeParametersGenerator5 = MakePbeGenerator((string)algorithmType[text], new MD2Digest(), array, salt, iterationCount);
if (text.Equals("PBEwithMD2andDES-CBC"))
{
parameters = pbeParametersGenerator5.GenerateDerivedParameters("DES", 64, 64);
}
else if (text.Equals("PBEwithMD2andRC2-CBC"))
{
parameters = pbeParametersGenerator5.GenerateDerivedParameters("RC2", 64, 64);
}
}
else if (Platform.StartsWith(text, "PBEwithHmac"))
{
string algorithm3 = text.Substring("PBEwithHmac".Length);
IDigest digest = DigestUtilities.GetDigest(algorithm3);
PbeParametersGenerator pbeParametersGenerator6 = MakePbeGenerator((string)algorithmType[text], digest, array, salt, iterationCount);
int keySize2 = digest.GetDigestSize() * 8;
parameters = pbeParametersGenerator6.GenerateDerivedMacParameters(keySize2);
}
Array.Clear(array, 0, array.Length);
return FixDesParity(text, parameters);
}
public static object CreateEngine(DerObjectIdentifier algorithmOid)
{
return CreateEngine(algorithmOid.Id);
}
public static object CreateEngine(AlgorithmIdentifier algID)
{
string id = algID.Algorithm.Id;
if (IsPkcs5Scheme2(id))
{
PbeS2Parameters instance = PbeS2Parameters.GetInstance(algID.Parameters.ToAsn1Object());
AlgorithmIdentifier encryptionScheme = instance.EncryptionScheme;
return CipherUtilities.GetCipher(encryptionScheme.Algorithm);
}
return CreateEngine(id);
}
public static object CreateEngine(string algorithm)
{
string text = (string)algorithms[Platform.ToUpperInvariant(algorithm)];
if (Platform.StartsWith(text, "PBEwithHmac"))
{
string text2 = text.Substring("PBEwithHmac".Length);
return MacUtilities.GetMac("HMAC/" + text2);
}
if (Platform.StartsWith(text, "PBEwithMD2") || Platform.StartsWith(text, "PBEwithMD5") || Platform.StartsWith(text, "PBEwithSHA-1") || Platform.StartsWith(text, "PBEwithSHA-256"))
{
if (Platform.EndsWith(text, "AES-CBC-BC") || Platform.EndsWith(text, "AES-CBC-OPENSSL"))
{
return CipherUtilities.GetCipher("AES/CBC");
}
if (Platform.EndsWith(text, "DES-CBC"))
{
return CipherUtilities.GetCipher("DES/CBC");
}
if (Platform.EndsWith(text, "DESEDE-CBC"))
{
return CipherUtilities.GetCipher("DESEDE/CBC");
}
if (Platform.EndsWith(text, "RC2-CBC"))
{
return CipherUtilities.GetCipher("RC2/CBC");
}
if (Platform.EndsWith(text, "RC4"))
{
return CipherUtilities.GetCipher("RC4");
}
}
return null;
}
public static string GetEncodingName(DerObjectIdentifier oid)
{
return (string)algorithms[oid.Id];
}
private static ICipherParameters FixDesParity(string mechanism, ICipherParameters parameters)
{
if (!Platform.EndsWith(mechanism, "DES-CBC") && !Platform.EndsWith(mechanism, "DESEDE-CBC"))
{
return parameters;
}
if (parameters is ParametersWithIV)
{
ParametersWithIV parametersWithIV = (ParametersWithIV)parameters;
return new ParametersWithIV(FixDesParity(mechanism, parametersWithIV.Parameters), parametersWithIV.GetIV());
}
KeyParameter keyParameter = (KeyParameter)parameters;
byte[] key = keyParameter.GetKey();
DesParameters.SetOddParity(key);
return new KeyParameter(key);
}
}

View File

@@ -0,0 +1,228 @@
using System;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.EdEC;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Rosstandart;
using Org.BouncyCastle.Asn1.Sec;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Security;
public sealed class PrivateKeyFactory
{
private PrivateKeyFactory()
{
}
public static AsymmetricKeyParameter CreateKey(byte[] privateKeyInfoData)
{
return CreateKey(PrivateKeyInfo.GetInstance(Asn1Object.FromByteArray(privateKeyInfoData)));
}
public static AsymmetricKeyParameter CreateKey(Stream inStr)
{
return CreateKey(PrivateKeyInfo.GetInstance(Asn1Object.FromStream(inStr)));
}
public static AsymmetricKeyParameter CreateKey(PrivateKeyInfo keyInfo)
{
AlgorithmIdentifier privateKeyAlgorithm = keyInfo.PrivateKeyAlgorithm;
DerObjectIdentifier algorithm = privateKeyAlgorithm.Algorithm;
if (algorithm.Equals(PkcsObjectIdentifiers.RsaEncryption) || algorithm.Equals(X509ObjectIdentifiers.IdEARsa) || algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss) || algorithm.Equals(PkcsObjectIdentifiers.IdRsaesOaep))
{
RsaPrivateKeyStructure instance = RsaPrivateKeyStructure.GetInstance(keyInfo.ParsePrivateKey());
return new RsaPrivateCrtKeyParameters(instance.Modulus, instance.PublicExponent, instance.PrivateExponent, instance.Prime1, instance.Prime2, instance.Exponent1, instance.Exponent2, instance.Coefficient);
}
if (algorithm.Equals(PkcsObjectIdentifiers.DhKeyAgreement))
{
DHParameter dHParameter = new DHParameter(Asn1Sequence.GetInstance(privateKeyAlgorithm.Parameters.ToAsn1Object()));
DerInteger derInteger = (DerInteger)keyInfo.ParsePrivateKey();
int l = dHParameter.L?.IntValue ?? 0;
DHParameters parameters = new DHParameters(dHParameter.P, dHParameter.G, null, l);
return new DHPrivateKeyParameters(derInteger.Value, parameters, algorithm);
}
if (algorithm.Equals(OiwObjectIdentifiers.ElGamalAlgorithm))
{
ElGamalParameter elGamalParameter = new ElGamalParameter(Asn1Sequence.GetInstance(privateKeyAlgorithm.Parameters.ToAsn1Object()));
DerInteger derInteger2 = (DerInteger)keyInfo.ParsePrivateKey();
return new ElGamalPrivateKeyParameters(derInteger2.Value, new ElGamalParameters(elGamalParameter.P, elGamalParameter.G));
}
if (algorithm.Equals(X9ObjectIdentifiers.IdDsa))
{
DerInteger derInteger3 = (DerInteger)keyInfo.ParsePrivateKey();
Asn1Encodable parameters2 = privateKeyAlgorithm.Parameters;
DsaParameters parameters3 = null;
if (parameters2 != null)
{
DsaParameter instance2 = DsaParameter.GetInstance(parameters2.ToAsn1Object());
parameters3 = new DsaParameters(instance2.P, instance2.Q, instance2.G);
}
return new DsaPrivateKeyParameters(derInteger3.Value, parameters3);
}
if (algorithm.Equals(X9ObjectIdentifiers.IdECPublicKey))
{
X962Parameters x962Parameters = new X962Parameters(privateKeyAlgorithm.Parameters.ToAsn1Object());
X9ECParameters x9ECParameters = ((!x962Parameters.IsNamedCurve) ? new X9ECParameters((Asn1Sequence)x962Parameters.Parameters) : ECKeyPairGenerator.FindECCurveByOid((DerObjectIdentifier)x962Parameters.Parameters));
ECPrivateKeyStructure instance3 = ECPrivateKeyStructure.GetInstance(keyInfo.ParsePrivateKey());
BigInteger key = instance3.GetKey();
if (x962Parameters.IsNamedCurve)
{
return new ECPrivateKeyParameters("EC", key, (DerObjectIdentifier)x962Parameters.Parameters);
}
ECDomainParameters parameters4 = new ECDomainParameters(x9ECParameters.Curve, x9ECParameters.G, x9ECParameters.N, x9ECParameters.H, x9ECParameters.GetSeed());
return new ECPrivateKeyParameters(key, parameters4);
}
if (algorithm.Equals(CryptoProObjectIdentifiers.GostR3410x2001))
{
Gost3410PublicKeyAlgParameters gost3410PublicKeyAlgParameters = new Gost3410PublicKeyAlgParameters(Asn1Sequence.GetInstance(privateKeyAlgorithm.Parameters.ToAsn1Object()));
ECDomainParameters byOid = ECGost3410NamedCurves.GetByOid(gost3410PublicKeyAlgParameters.PublicKeyParamSet);
if (byOid == null)
{
throw new ArgumentException("Unrecognized curve OID for GostR3410x2001 private key");
}
Asn1Object asn1Object = keyInfo.ParsePrivateKey();
ECPrivateKeyStructure eCPrivateKeyStructure = ((!(asn1Object is DerInteger)) ? ECPrivateKeyStructure.GetInstance(asn1Object) : new ECPrivateKeyStructure(byOid.N.BitLength, ((DerInteger)asn1Object).PositiveValue));
return new ECPrivateKeyParameters("ECGOST3410", eCPrivateKeyStructure.GetKey(), gost3410PublicKeyAlgParameters.PublicKeyParamSet);
}
if (algorithm.Equals(CryptoProObjectIdentifiers.GostR3410x94))
{
Gost3410PublicKeyAlgParameters instance4 = Gost3410PublicKeyAlgParameters.GetInstance(privateKeyAlgorithm.Parameters);
Asn1Object asn1Object2 = keyInfo.ParsePrivateKey();
BigInteger x = ((!(asn1Object2 is DerInteger)) ? new BigInteger(1, Arrays.Reverse(Asn1OctetString.GetInstance(asn1Object2).GetOctets())) : DerInteger.GetInstance(asn1Object2).PositiveValue);
return new Gost3410PrivateKeyParameters(x, instance4.PublicKeyParamSet);
}
if (algorithm.Equals(EdECObjectIdentifiers.id_X25519))
{
return new X25519PrivateKeyParameters(GetRawKey(keyInfo, X25519PrivateKeyParameters.KeySize), 0);
}
if (algorithm.Equals(EdECObjectIdentifiers.id_X448))
{
return new X448PrivateKeyParameters(GetRawKey(keyInfo, X448PrivateKeyParameters.KeySize), 0);
}
if (algorithm.Equals(EdECObjectIdentifiers.id_Ed25519))
{
return new Ed25519PrivateKeyParameters(GetRawKey(keyInfo, Ed25519PrivateKeyParameters.KeySize), 0);
}
if (algorithm.Equals(EdECObjectIdentifiers.id_Ed448))
{
return new Ed448PrivateKeyParameters(GetRawKey(keyInfo, Ed448PrivateKeyParameters.KeySize), 0);
}
if (algorithm.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512) || algorithm.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256))
{
Gost3410PublicKeyAlgParameters instance5 = Gost3410PublicKeyAlgParameters.GetInstance(keyInfo.PrivateKeyAlgorithm.Parameters);
ECGost3410Parameters eCGost3410Parameters = null;
BigInteger bigInteger = null;
Asn1Object asn1Object3 = keyInfo.PrivateKeyAlgorithm.Parameters.ToAsn1Object();
if (asn1Object3 is Asn1Sequence && (Asn1Sequence.GetInstance(asn1Object3).Count == 2 || Asn1Sequence.GetInstance(asn1Object3).Count == 3))
{
ECDomainParameters byOid2 = ECGost3410NamedCurves.GetByOid(instance5.PublicKeyParamSet);
eCGost3410Parameters = new ECGost3410Parameters(new ECNamedDomainParameters(instance5.PublicKeyParamSet, byOid2), instance5.PublicKeyParamSet, instance5.DigestParamSet, instance5.EncryptionParamSet);
Asn1Encodable asn1Encodable = keyInfo.ParsePrivateKey();
if (asn1Encodable is DerInteger)
{
bigInteger = DerInteger.GetInstance(asn1Encodable).PositiveValue;
}
else
{
byte[] octets = Asn1OctetString.GetInstance(asn1Encodable).GetOctets();
byte[] array = new byte[octets.Length];
for (int i = 0; i != octets.Length; i++)
{
array[i] = octets[octets.Length - 1 - i];
}
bigInteger = new BigInteger(1, array);
}
}
else
{
X962Parameters instance6 = X962Parameters.GetInstance(keyInfo.PrivateKeyAlgorithm.Parameters);
if (instance6.IsNamedCurve)
{
DerObjectIdentifier instance7 = DerObjectIdentifier.GetInstance(instance6.Parameters);
X9ECParameters byOid3 = ECNamedCurveTable.GetByOid(instance7);
if (byOid3 == null)
{
ECDomainParameters byOid4 = ECGost3410NamedCurves.GetByOid(instance7);
eCGost3410Parameters = new ECGost3410Parameters(new ECNamedDomainParameters(instance7, byOid4.Curve, byOid4.G, byOid4.N, byOid4.H, byOid4.GetSeed()), instance5.PublicKeyParamSet, instance5.DigestParamSet, instance5.EncryptionParamSet);
}
else
{
eCGost3410Parameters = new ECGost3410Parameters(new ECNamedDomainParameters(instance7, byOid3.Curve, byOid3.G, byOid3.N, byOid3.H, byOid3.GetSeed()), instance5.PublicKeyParamSet, instance5.DigestParamSet, instance5.EncryptionParamSet);
}
}
else if (instance6.IsImplicitlyCA)
{
eCGost3410Parameters = null;
}
else
{
X9ECParameters instance8 = X9ECParameters.GetInstance(instance6.Parameters);
eCGost3410Parameters = new ECGost3410Parameters(new ECNamedDomainParameters(algorithm, instance8.Curve, instance8.G, instance8.N, instance8.H, instance8.GetSeed()), instance5.PublicKeyParamSet, instance5.DigestParamSet, instance5.EncryptionParamSet);
}
Asn1Encodable asn1Encodable2 = keyInfo.ParsePrivateKey();
if (asn1Encodable2 is DerInteger)
{
DerInteger instance9 = DerInteger.GetInstance(asn1Encodable2);
bigInteger = instance9.Value;
}
else
{
ECPrivateKeyStructure instance10 = ECPrivateKeyStructure.GetInstance(asn1Encodable2);
bigInteger = instance10.GetKey();
}
}
return new ECPrivateKeyParameters(bigInteger, new ECGost3410Parameters(eCGost3410Parameters, instance5.PublicKeyParamSet, instance5.DigestParamSet, instance5.EncryptionParamSet));
}
throw new SecurityUtilityException("algorithm identifier in private key not recognised");
}
private static byte[] GetRawKey(PrivateKeyInfo keyInfo, int expectedSize)
{
byte[] octets = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets();
if (expectedSize != octets.Length)
{
throw new SecurityUtilityException("private key encoding has incorrect length");
}
return octets;
}
public static AsymmetricKeyParameter DecryptKey(char[] passPhrase, EncryptedPrivateKeyInfo encInfo)
{
return CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(passPhrase, encInfo));
}
public static AsymmetricKeyParameter DecryptKey(char[] passPhrase, byte[] encryptedPrivateKeyInfoData)
{
return DecryptKey(passPhrase, Asn1Object.FromByteArray(encryptedPrivateKeyInfoData));
}
public static AsymmetricKeyParameter DecryptKey(char[] passPhrase, Stream encryptedPrivateKeyInfoStream)
{
return DecryptKey(passPhrase, Asn1Object.FromStream(encryptedPrivateKeyInfoStream));
}
private static AsymmetricKeyParameter DecryptKey(char[] passPhrase, Asn1Object asn1Object)
{
return DecryptKey(passPhrase, EncryptedPrivateKeyInfo.GetInstance(asn1Object));
}
public static byte[] EncryptKey(DerObjectIdentifier algorithm, char[] passPhrase, byte[] salt, int iterationCount, AsymmetricKeyParameter key)
{
return EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(algorithm, passPhrase, salt, iterationCount, key).GetEncoded();
}
public static byte[] EncryptKey(string algorithm, char[] passPhrase, byte[] salt, int iterationCount, AsymmetricKeyParameter key)
{
return EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(algorithm, passPhrase, salt, iterationCount, key).GetEncoded();
}
}

View File

@@ -0,0 +1,233 @@
using System;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.EdEC;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Rosstandart;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.EC;
namespace Org.BouncyCastle.Security;
public sealed class PublicKeyFactory
{
private PublicKeyFactory()
{
}
public static AsymmetricKeyParameter CreateKey(byte[] keyInfoData)
{
return CreateKey(SubjectPublicKeyInfo.GetInstance(Asn1Object.FromByteArray(keyInfoData)));
}
public static AsymmetricKeyParameter CreateKey(Stream inStr)
{
return CreateKey(SubjectPublicKeyInfo.GetInstance(Asn1Object.FromStream(inStr)));
}
public static AsymmetricKeyParameter CreateKey(SubjectPublicKeyInfo keyInfo)
{
AlgorithmIdentifier algorithmID = keyInfo.AlgorithmID;
DerObjectIdentifier algorithm = algorithmID.Algorithm;
if (algorithm.Equals(PkcsObjectIdentifiers.RsaEncryption) || algorithm.Equals(X509ObjectIdentifiers.IdEARsa) || algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss) || algorithm.Equals(PkcsObjectIdentifiers.IdRsaesOaep))
{
RsaPublicKeyStructure instance = RsaPublicKeyStructure.GetInstance(keyInfo.GetPublicKey());
return new RsaKeyParameters(isPrivate: false, instance.Modulus, instance.PublicExponent);
}
if (algorithm.Equals(X9ObjectIdentifiers.DHPublicNumber))
{
Asn1Sequence instance2 = Asn1Sequence.GetInstance(algorithmID.Parameters.ToAsn1Object());
DHPublicKey instance3 = DHPublicKey.GetInstance(keyInfo.GetPublicKey());
BigInteger value = instance3.Y.Value;
if (IsPkcsDHParam(instance2))
{
return ReadPkcsDHParam(algorithm, value, instance2);
}
DHDomainParameters instance4 = DHDomainParameters.GetInstance(instance2);
BigInteger value2 = instance4.P.Value;
BigInteger value3 = instance4.G.Value;
BigInteger value4 = instance4.Q.Value;
BigInteger j = null;
if (instance4.J != null)
{
j = instance4.J.Value;
}
DHValidationParameters validation = null;
DHValidationParms validationParms = instance4.ValidationParms;
if (validationParms != null)
{
byte[] bytes = validationParms.Seed.GetBytes();
BigInteger value5 = validationParms.PgenCounter.Value;
validation = new DHValidationParameters(bytes, value5.IntValue);
}
return new DHPublicKeyParameters(value, new DHParameters(value2, value3, value4, j, validation));
}
if (algorithm.Equals(PkcsObjectIdentifiers.DhKeyAgreement))
{
Asn1Sequence instance5 = Asn1Sequence.GetInstance(algorithmID.Parameters.ToAsn1Object());
DerInteger derInteger = (DerInteger)keyInfo.GetPublicKey();
return ReadPkcsDHParam(algorithm, derInteger.Value, instance5);
}
if (algorithm.Equals(OiwObjectIdentifiers.ElGamalAlgorithm))
{
ElGamalParameter elGamalParameter = new ElGamalParameter(Asn1Sequence.GetInstance(algorithmID.Parameters.ToAsn1Object()));
DerInteger derInteger2 = (DerInteger)keyInfo.GetPublicKey();
return new ElGamalPublicKeyParameters(derInteger2.Value, new ElGamalParameters(elGamalParameter.P, elGamalParameter.G));
}
if (algorithm.Equals(X9ObjectIdentifiers.IdDsa) || algorithm.Equals(OiwObjectIdentifiers.DsaWithSha1))
{
DerInteger derInteger3 = (DerInteger)keyInfo.GetPublicKey();
Asn1Encodable parameters = algorithmID.Parameters;
DsaParameters parameters2 = null;
if (parameters != null)
{
DsaParameter instance6 = DsaParameter.GetInstance(parameters.ToAsn1Object());
parameters2 = new DsaParameters(instance6.P, instance6.Q, instance6.G);
}
return new DsaPublicKeyParameters(derInteger3.Value, parameters2);
}
if (algorithm.Equals(X9ObjectIdentifiers.IdECPublicKey))
{
X962Parameters x962Parameters = new X962Parameters(algorithmID.Parameters.ToAsn1Object());
X9ECParameters x9ECParameters = ((!x962Parameters.IsNamedCurve) ? new X9ECParameters((Asn1Sequence)x962Parameters.Parameters) : ECKeyPairGenerator.FindECCurveByOid((DerObjectIdentifier)x962Parameters.Parameters));
Asn1OctetString s = new DerOctetString(keyInfo.PublicKeyData.GetBytes());
X9ECPoint x9ECPoint = new X9ECPoint(x9ECParameters.Curve, s);
ECPoint point = x9ECPoint.Point;
if (x962Parameters.IsNamedCurve)
{
return new ECPublicKeyParameters("EC", point, (DerObjectIdentifier)x962Parameters.Parameters);
}
ECDomainParameters parameters3 = new ECDomainParameters(x9ECParameters.Curve, x9ECParameters.G, x9ECParameters.N, x9ECParameters.H, x9ECParameters.GetSeed());
return new ECPublicKeyParameters(point, parameters3);
}
if (algorithm.Equals(CryptoProObjectIdentifiers.GostR3410x2001))
{
Gost3410PublicKeyAlgParameters gost3410PublicKeyAlgParameters = new Gost3410PublicKeyAlgParameters((Asn1Sequence)algorithmID.Parameters);
Asn1OctetString asn1OctetString;
try
{
asn1OctetString = (Asn1OctetString)keyInfo.GetPublicKey();
}
catch (IOException)
{
throw new ArgumentException("invalid info structure in GOST3410 public key");
}
byte[] octets = asn1OctetString.GetOctets();
byte[] array = new byte[32];
byte[] array2 = new byte[32];
for (int i = 0; i != array2.Length; i++)
{
array[i] = octets[31 - i];
}
for (int k = 0; k != array.Length; k++)
{
array2[k] = octets[63 - k];
}
ECDomainParameters byOid = ECGost3410NamedCurves.GetByOid(gost3410PublicKeyAlgParameters.PublicKeyParamSet);
if (byOid == null)
{
return null;
}
ECPoint q = byOid.Curve.CreatePoint(new BigInteger(1, array), new BigInteger(1, array2));
return new ECPublicKeyParameters("ECGOST3410", q, gost3410PublicKeyAlgParameters.PublicKeyParamSet);
}
if (algorithm.Equals(CryptoProObjectIdentifiers.GostR3410x94))
{
Gost3410PublicKeyAlgParameters gost3410PublicKeyAlgParameters2 = new Gost3410PublicKeyAlgParameters((Asn1Sequence)algorithmID.Parameters);
DerOctetString derOctetString;
try
{
derOctetString = (DerOctetString)keyInfo.GetPublicKey();
}
catch (IOException)
{
throw new ArgumentException("invalid info structure in GOST3410 public key");
}
byte[] octets2 = derOctetString.GetOctets();
byte[] array3 = new byte[octets2.Length];
for (int l = 0; l != octets2.Length; l++)
{
array3[l] = octets2[octets2.Length - 1 - l];
}
BigInteger y = new BigInteger(1, array3);
return new Gost3410PublicKeyParameters(y, gost3410PublicKeyAlgParameters2.PublicKeyParamSet);
}
if (algorithm.Equals(EdECObjectIdentifiers.id_X25519))
{
return new X25519PublicKeyParameters(GetRawKey(keyInfo, X25519PublicKeyParameters.KeySize), 0);
}
if (algorithm.Equals(EdECObjectIdentifiers.id_X448))
{
return new X448PublicKeyParameters(GetRawKey(keyInfo, X448PublicKeyParameters.KeySize), 0);
}
if (algorithm.Equals(EdECObjectIdentifiers.id_Ed25519))
{
return new Ed25519PublicKeyParameters(GetRawKey(keyInfo, Ed25519PublicKeyParameters.KeySize), 0);
}
if (algorithm.Equals(EdECObjectIdentifiers.id_Ed448))
{
return new Ed448PublicKeyParameters(GetRawKey(keyInfo, Ed448PublicKeyParameters.KeySize), 0);
}
if (algorithm.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256) || algorithm.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512))
{
byte[] str = ((DerOctetString)Asn1Object.FromByteArray(keyInfo.PublicKeyData.GetOctets())).str;
int num = 32;
if (algorithm.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512))
{
num = 64;
}
int num2 = 2 * num;
byte[] array4 = new byte[1 + num2];
array4[0] = 4;
for (int m = 1; m <= num; m++)
{
array4[m] = str[num - m];
array4[m + num] = str[num2 - m];
}
Gost3410PublicKeyAlgParameters instance7 = Gost3410PublicKeyAlgParameters.GetInstance(keyInfo.AlgorithmID.Parameters);
ECGost3410Parameters eCGost3410Parameters = new ECGost3410Parameters(new ECNamedDomainParameters(instance7.PublicKeyParamSet, ECGost3410NamedCurves.GetByOid(instance7.PublicKeyParamSet)), instance7.PublicKeyParamSet, instance7.DigestParamSet, instance7.EncryptionParamSet);
return new ECPublicKeyParameters(eCGost3410Parameters.Curve.DecodePoint(array4), eCGost3410Parameters);
}
throw new SecurityUtilityException("algorithm identifier in public key not recognised: " + algorithm);
}
private static byte[] GetRawKey(SubjectPublicKeyInfo keyInfo, int expectedSize)
{
byte[] octets = keyInfo.PublicKeyData.GetOctets();
if (expectedSize != octets.Length)
{
throw new SecurityUtilityException("public key encoding has incorrect length");
}
return octets;
}
private static bool IsPkcsDHParam(Asn1Sequence seq)
{
if (seq.Count == 2)
{
return true;
}
if (seq.Count > 3)
{
return false;
}
DerInteger instance = DerInteger.GetInstance(seq[2]);
DerInteger instance2 = DerInteger.GetInstance(seq[0]);
return instance.Value.CompareTo(BigInteger.ValueOf(instance2.Value.BitLength)) <= 0;
}
private static DHPublicKeyParameters ReadPkcsDHParam(DerObjectIdentifier algOid, BigInteger y, Asn1Sequence seq)
{
DHParameter dHParameter = new DHParameter(seq);
int l = dHParameter.L?.IntValue ?? 0;
DHParameters parameters = new DHParameters(dHParameter.P, dHParameter.G, null, l);
return new DHPublicKeyParameters(y, parameters, algOid);
}
}

View File

@@ -0,0 +1,195 @@
using System;
using System.Threading;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Prng;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Security;
public class SecureRandom : Random
{
private static long counter = Times.NanoTime();
private static readonly SecureRandom master = new SecureRandom(new CryptoApiRandomGenerator());
protected readonly IRandomGenerator generator;
private static readonly double DoubleScale = System.Math.Pow(2.0, 64.0);
private static SecureRandom Master => master;
private static long NextCounterValue()
{
return Interlocked.Increment(ref counter);
}
private static DigestRandomGenerator CreatePrng(string digestName, bool autoSeed)
{
IDigest digest = DigestUtilities.GetDigest(digestName);
if (digest == null)
{
return null;
}
DigestRandomGenerator digestRandomGenerator = new DigestRandomGenerator(digest);
if (autoSeed)
{
digestRandomGenerator.AddSeedMaterial(NextCounterValue());
digestRandomGenerator.AddSeedMaterial(GetNextBytes(Master, digest.GetDigestSize()));
}
return digestRandomGenerator;
}
public static byte[] GetNextBytes(SecureRandom secureRandom, int length)
{
byte[] array = new byte[length];
secureRandom.NextBytes(array);
return array;
}
public static SecureRandom GetInstance(string algorithm)
{
return GetInstance(algorithm, autoSeed: true);
}
public static SecureRandom GetInstance(string algorithm, bool autoSeed)
{
string text = Platform.ToUpperInvariant(algorithm);
if (Platform.EndsWith(text, "PRNG"))
{
string digestName = text.Substring(0, text.Length - "PRNG".Length);
DigestRandomGenerator digestRandomGenerator = CreatePrng(digestName, autoSeed);
if (digestRandomGenerator != null)
{
return new SecureRandom(digestRandomGenerator);
}
}
throw new ArgumentException("Unrecognised PRNG algorithm: " + algorithm, "algorithm");
}
[Obsolete("Call GenerateSeed() on a SecureRandom instance instead")]
public static byte[] GetSeed(int length)
{
return GetNextBytes(Master, length);
}
public SecureRandom()
: this(CreatePrng("SHA256", autoSeed: true))
{
}
[Obsolete("Use GetInstance/SetSeed instead")]
public SecureRandom(byte[] seed)
: this(CreatePrng("SHA1", autoSeed: false))
{
SetSeed(seed);
}
public SecureRandom(IRandomGenerator generator)
: base(0)
{
this.generator = generator;
}
public virtual byte[] GenerateSeed(int length)
{
return GetNextBytes(Master, length);
}
public virtual void SetSeed(byte[] seed)
{
generator.AddSeedMaterial(seed);
}
public virtual void SetSeed(long seed)
{
generator.AddSeedMaterial(seed);
}
public override int Next()
{
return NextInt() & 0x7FFFFFFF;
}
public override int Next(int maxValue)
{
if (maxValue < 2)
{
if (maxValue < 0)
{
throw new ArgumentOutOfRangeException("maxValue", "cannot be negative");
}
return 0;
}
int num;
if ((maxValue & (maxValue - 1)) == 0)
{
num = NextInt() & 0x7FFFFFFF;
return (int)((long)num * (long)maxValue >> 31);
}
int num2;
do
{
num = NextInt() & 0x7FFFFFFF;
num2 = num % maxValue;
}
while (num - num2 + (maxValue - 1) < 0);
return num2;
}
public override int Next(int minValue, int maxValue)
{
if (maxValue <= minValue)
{
if (maxValue == minValue)
{
return minValue;
}
throw new ArgumentException("maxValue cannot be less than minValue");
}
int num = maxValue - minValue;
if (num > 0)
{
return minValue + Next(num);
}
int num2;
do
{
num2 = NextInt();
}
while (num2 < minValue || num2 >= maxValue);
return num2;
}
public override void NextBytes(byte[] buf)
{
generator.NextBytes(buf);
}
public virtual void NextBytes(byte[] buf, int off, int len)
{
generator.NextBytes(buf, off, len);
}
public override double NextDouble()
{
return Convert.ToDouble((ulong)NextLong()) / DoubleScale;
}
public virtual int NextInt()
{
byte[] array = new byte[4];
NextBytes(array);
uint num = array[0];
num <<= 8;
num |= array[1];
num <<= 8;
num |= array[2];
num <<= 8;
return (int)(num | array[3]);
}
public virtual long NextLong()
{
return (long)(((ulong)(uint)NextInt() << 32) | (uint)NextInt());
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Security;
[Serializable]
public class SecurityUtilityException : Exception
{
public SecurityUtilityException()
{
}
public SecurityUtilityException(string message)
: base(message)
{
}
public SecurityUtilityException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace Org.BouncyCastle.Security;
[Serializable]
public class SignatureException : GeneralSecurityException
{
public SignatureException()
{
}
public SignatureException(string message)
: base(message)
{
}
public SignatureException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,534 @@
using System;
using System.Collections;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Bsi;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.Eac;
using Org.BouncyCastle.Asn1.EdEC;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.TeleTrust;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Signers;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Security;
public sealed class SignerUtilities
{
internal static readonly IDictionary algorithms;
internal static readonly IDictionary oids;
public static ICollection Algorithms => oids.Keys;
private SignerUtilities()
{
}
static SignerUtilities()
{
algorithms = Platform.CreateHashtable();
oids = Platform.CreateHashtable();
algorithms["MD2WITHRSA"] = "MD2withRSA";
algorithms["MD2WITHRSAENCRYPTION"] = "MD2withRSA";
algorithms[PkcsObjectIdentifiers.MD2WithRsaEncryption.Id] = "MD2withRSA";
algorithms["MD4WITHRSA"] = "MD4withRSA";
algorithms["MD4WITHRSAENCRYPTION"] = "MD4withRSA";
algorithms[PkcsObjectIdentifiers.MD4WithRsaEncryption.Id] = "MD4withRSA";
algorithms[OiwObjectIdentifiers.MD4WithRsa.Id] = "MD4withRSA";
algorithms[OiwObjectIdentifiers.MD4WithRsaEncryption.Id] = "MD4withRSA";
algorithms["MD5WITHRSA"] = "MD5withRSA";
algorithms["MD5WITHRSAENCRYPTION"] = "MD5withRSA";
algorithms[PkcsObjectIdentifiers.MD5WithRsaEncryption.Id] = "MD5withRSA";
algorithms[OiwObjectIdentifiers.MD5WithRsa.Id] = "MD5withRSA";
algorithms["SHA1WITHRSA"] = "SHA-1withRSA";
algorithms["SHA1WITHRSAENCRYPTION"] = "SHA-1withRSA";
algorithms["SHA-1WITHRSA"] = "SHA-1withRSA";
algorithms[PkcsObjectIdentifiers.Sha1WithRsaEncryption.Id] = "SHA-1withRSA";
algorithms[OiwObjectIdentifiers.Sha1WithRsa.Id] = "SHA-1withRSA";
algorithms["SHA224WITHRSA"] = "SHA-224withRSA";
algorithms["SHA224WITHRSAENCRYPTION"] = "SHA-224withRSA";
algorithms[PkcsObjectIdentifiers.Sha224WithRsaEncryption.Id] = "SHA-224withRSA";
algorithms["SHA-224WITHRSA"] = "SHA-224withRSA";
algorithms["SHA256WITHRSA"] = "SHA-256withRSA";
algorithms["SHA256WITHRSAENCRYPTION"] = "SHA-256withRSA";
algorithms[PkcsObjectIdentifiers.Sha256WithRsaEncryption.Id] = "SHA-256withRSA";
algorithms["SHA-256WITHRSA"] = "SHA-256withRSA";
algorithms["SHA384WITHRSA"] = "SHA-384withRSA";
algorithms["SHA384WITHRSAENCRYPTION"] = "SHA-384withRSA";
algorithms[PkcsObjectIdentifiers.Sha384WithRsaEncryption.Id] = "SHA-384withRSA";
algorithms["SHA-384WITHRSA"] = "SHA-384withRSA";
algorithms["SHA512WITHRSA"] = "SHA-512withRSA";
algorithms["SHA512WITHRSAENCRYPTION"] = "SHA-512withRSA";
algorithms[PkcsObjectIdentifiers.Sha512WithRsaEncryption.Id] = "SHA-512withRSA";
algorithms["SHA-512WITHRSA"] = "SHA-512withRSA";
algorithms["PSSWITHRSA"] = "PSSwithRSA";
algorithms["RSASSA-PSS"] = "PSSwithRSA";
algorithms[PkcsObjectIdentifiers.IdRsassaPss.Id] = "PSSwithRSA";
algorithms["RSAPSS"] = "PSSwithRSA";
algorithms["SHA1WITHRSAANDMGF1"] = "SHA-1withRSAandMGF1";
algorithms["SHA-1WITHRSAANDMGF1"] = "SHA-1withRSAandMGF1";
algorithms["SHA1WITHRSA/PSS"] = "SHA-1withRSAandMGF1";
algorithms["SHA-1WITHRSA/PSS"] = "SHA-1withRSAandMGF1";
algorithms["SHA224WITHRSAANDMGF1"] = "SHA-224withRSAandMGF1";
algorithms["SHA-224WITHRSAANDMGF1"] = "SHA-224withRSAandMGF1";
algorithms["SHA224WITHRSA/PSS"] = "SHA-224withRSAandMGF1";
algorithms["SHA-224WITHRSA/PSS"] = "SHA-224withRSAandMGF1";
algorithms["SHA256WITHRSAANDMGF1"] = "SHA-256withRSAandMGF1";
algorithms["SHA-256WITHRSAANDMGF1"] = "SHA-256withRSAandMGF1";
algorithms["SHA256WITHRSA/PSS"] = "SHA-256withRSAandMGF1";
algorithms["SHA-256WITHRSA/PSS"] = "SHA-256withRSAandMGF1";
algorithms["SHA384WITHRSAANDMGF1"] = "SHA-384withRSAandMGF1";
algorithms["SHA-384WITHRSAANDMGF1"] = "SHA-384withRSAandMGF1";
algorithms["SHA384WITHRSA/PSS"] = "SHA-384withRSAandMGF1";
algorithms["SHA-384WITHRSA/PSS"] = "SHA-384withRSAandMGF1";
algorithms["SHA512WITHRSAANDMGF1"] = "SHA-512withRSAandMGF1";
algorithms["SHA-512WITHRSAANDMGF1"] = "SHA-512withRSAandMGF1";
algorithms["SHA512WITHRSA/PSS"] = "SHA-512withRSAandMGF1";
algorithms["SHA-512WITHRSA/PSS"] = "SHA-512withRSAandMGF1";
algorithms["RIPEMD128WITHRSA"] = "RIPEMD128withRSA";
algorithms["RIPEMD128WITHRSAENCRYPTION"] = "RIPEMD128withRSA";
algorithms[TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128.Id] = "RIPEMD128withRSA";
algorithms["RIPEMD160WITHRSA"] = "RIPEMD160withRSA";
algorithms["RIPEMD160WITHRSAENCRYPTION"] = "RIPEMD160withRSA";
algorithms[TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160.Id] = "RIPEMD160withRSA";
algorithms["RIPEMD256WITHRSA"] = "RIPEMD256withRSA";
algorithms["RIPEMD256WITHRSAENCRYPTION"] = "RIPEMD256withRSA";
algorithms[TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256.Id] = "RIPEMD256withRSA";
algorithms["NONEWITHRSA"] = "RSA";
algorithms["RSAWITHNONE"] = "RSA";
algorithms["RAWRSA"] = "RSA";
algorithms["RAWRSAPSS"] = "RAWRSASSA-PSS";
algorithms["NONEWITHRSAPSS"] = "RAWRSASSA-PSS";
algorithms["NONEWITHRSASSA-PSS"] = "RAWRSASSA-PSS";
algorithms["NONEWITHDSA"] = "NONEwithDSA";
algorithms["DSAWITHNONE"] = "NONEwithDSA";
algorithms["RAWDSA"] = "NONEwithDSA";
algorithms["DSA"] = "SHA-1withDSA";
algorithms["DSAWITHSHA1"] = "SHA-1withDSA";
algorithms["DSAWITHSHA-1"] = "SHA-1withDSA";
algorithms["SHA/DSA"] = "SHA-1withDSA";
algorithms["SHA1/DSA"] = "SHA-1withDSA";
algorithms["SHA-1/DSA"] = "SHA-1withDSA";
algorithms["SHA1WITHDSA"] = "SHA-1withDSA";
algorithms["SHA-1WITHDSA"] = "SHA-1withDSA";
algorithms[X9ObjectIdentifiers.IdDsaWithSha1.Id] = "SHA-1withDSA";
algorithms[OiwObjectIdentifiers.DsaWithSha1.Id] = "SHA-1withDSA";
algorithms["DSAWITHSHA224"] = "SHA-224withDSA";
algorithms["DSAWITHSHA-224"] = "SHA-224withDSA";
algorithms["SHA224/DSA"] = "SHA-224withDSA";
algorithms["SHA-224/DSA"] = "SHA-224withDSA";
algorithms["SHA224WITHDSA"] = "SHA-224withDSA";
algorithms["SHA-224WITHDSA"] = "SHA-224withDSA";
algorithms[NistObjectIdentifiers.DsaWithSha224.Id] = "SHA-224withDSA";
algorithms["DSAWITHSHA256"] = "SHA-256withDSA";
algorithms["DSAWITHSHA-256"] = "SHA-256withDSA";
algorithms["SHA256/DSA"] = "SHA-256withDSA";
algorithms["SHA-256/DSA"] = "SHA-256withDSA";
algorithms["SHA256WITHDSA"] = "SHA-256withDSA";
algorithms["SHA-256WITHDSA"] = "SHA-256withDSA";
algorithms[NistObjectIdentifiers.DsaWithSha256.Id] = "SHA-256withDSA";
algorithms["DSAWITHSHA384"] = "SHA-384withDSA";
algorithms["DSAWITHSHA-384"] = "SHA-384withDSA";
algorithms["SHA384/DSA"] = "SHA-384withDSA";
algorithms["SHA-384/DSA"] = "SHA-384withDSA";
algorithms["SHA384WITHDSA"] = "SHA-384withDSA";
algorithms["SHA-384WITHDSA"] = "SHA-384withDSA";
algorithms[NistObjectIdentifiers.DsaWithSha384.Id] = "SHA-384withDSA";
algorithms["DSAWITHSHA512"] = "SHA-512withDSA";
algorithms["DSAWITHSHA-512"] = "SHA-512withDSA";
algorithms["SHA512/DSA"] = "SHA-512withDSA";
algorithms["SHA-512/DSA"] = "SHA-512withDSA";
algorithms["SHA512WITHDSA"] = "SHA-512withDSA";
algorithms["SHA-512WITHDSA"] = "SHA-512withDSA";
algorithms[NistObjectIdentifiers.DsaWithSha512.Id] = "SHA-512withDSA";
algorithms["NONEWITHECDSA"] = "NONEwithECDSA";
algorithms["ECDSAWITHNONE"] = "NONEwithECDSA";
algorithms["ECDSA"] = "SHA-1withECDSA";
algorithms["SHA1/ECDSA"] = "SHA-1withECDSA";
algorithms["SHA-1/ECDSA"] = "SHA-1withECDSA";
algorithms["ECDSAWITHSHA1"] = "SHA-1withECDSA";
algorithms["ECDSAWITHSHA-1"] = "SHA-1withECDSA";
algorithms["SHA1WITHECDSA"] = "SHA-1withECDSA";
algorithms["SHA-1WITHECDSA"] = "SHA-1withECDSA";
algorithms[X9ObjectIdentifiers.ECDsaWithSha1.Id] = "SHA-1withECDSA";
algorithms[TeleTrusTObjectIdentifiers.ECSignWithSha1.Id] = "SHA-1withECDSA";
algorithms["SHA224/ECDSA"] = "SHA-224withECDSA";
algorithms["SHA-224/ECDSA"] = "SHA-224withECDSA";
algorithms["ECDSAWITHSHA224"] = "SHA-224withECDSA";
algorithms["ECDSAWITHSHA-224"] = "SHA-224withECDSA";
algorithms["SHA224WITHECDSA"] = "SHA-224withECDSA";
algorithms["SHA-224WITHECDSA"] = "SHA-224withECDSA";
algorithms[X9ObjectIdentifiers.ECDsaWithSha224.Id] = "SHA-224withECDSA";
algorithms["SHA256/ECDSA"] = "SHA-256withECDSA";
algorithms["SHA-256/ECDSA"] = "SHA-256withECDSA";
algorithms["ECDSAWITHSHA256"] = "SHA-256withECDSA";
algorithms["ECDSAWITHSHA-256"] = "SHA-256withECDSA";
algorithms["SHA256WITHECDSA"] = "SHA-256withECDSA";
algorithms["SHA-256WITHECDSA"] = "SHA-256withECDSA";
algorithms[X9ObjectIdentifiers.ECDsaWithSha256.Id] = "SHA-256withECDSA";
algorithms["SHA384/ECDSA"] = "SHA-384withECDSA";
algorithms["SHA-384/ECDSA"] = "SHA-384withECDSA";
algorithms["ECDSAWITHSHA384"] = "SHA-384withECDSA";
algorithms["ECDSAWITHSHA-384"] = "SHA-384withECDSA";
algorithms["SHA384WITHECDSA"] = "SHA-384withECDSA";
algorithms["SHA-384WITHECDSA"] = "SHA-384withECDSA";
algorithms[X9ObjectIdentifiers.ECDsaWithSha384.Id] = "SHA-384withECDSA";
algorithms["SHA512/ECDSA"] = "SHA-512withECDSA";
algorithms["SHA-512/ECDSA"] = "SHA-512withECDSA";
algorithms["ECDSAWITHSHA512"] = "SHA-512withECDSA";
algorithms["ECDSAWITHSHA-512"] = "SHA-512withECDSA";
algorithms["SHA512WITHECDSA"] = "SHA-512withECDSA";
algorithms["SHA-512WITHECDSA"] = "SHA-512withECDSA";
algorithms[X9ObjectIdentifiers.ECDsaWithSha512.Id] = "SHA-512withECDSA";
algorithms["RIPEMD160/ECDSA"] = "RIPEMD160withECDSA";
algorithms["ECDSAWITHRIPEMD160"] = "RIPEMD160withECDSA";
algorithms["RIPEMD160WITHECDSA"] = "RIPEMD160withECDSA";
algorithms[TeleTrusTObjectIdentifiers.ECSignWithRipeMD160.Id] = "RIPEMD160withECDSA";
algorithms["NONEWITHCVC-ECDSA"] = "NONEwithCVC-ECDSA";
algorithms["CVC-ECDSAWITHNONE"] = "NONEwithCVC-ECDSA";
algorithms["SHA1/CVC-ECDSA"] = "SHA-1withCVC-ECDSA";
algorithms["SHA-1/CVC-ECDSA"] = "SHA-1withCVC-ECDSA";
algorithms["CVC-ECDSAWITHSHA1"] = "SHA-1withCVC-ECDSA";
algorithms["CVC-ECDSAWITHSHA-1"] = "SHA-1withCVC-ECDSA";
algorithms["SHA1WITHCVC-ECDSA"] = "SHA-1withCVC-ECDSA";
algorithms["SHA-1WITHCVC-ECDSA"] = "SHA-1withCVC-ECDSA";
algorithms[EacObjectIdentifiers.id_TA_ECDSA_SHA_1.Id] = "SHA-1withCVC-ECDSA";
algorithms["SHA224/CVC-ECDSA"] = "SHA-224withCVC-ECDSA";
algorithms["SHA-224/CVC-ECDSA"] = "SHA-224withCVC-ECDSA";
algorithms["CVC-ECDSAWITHSHA224"] = "SHA-224withCVC-ECDSA";
algorithms["CVC-ECDSAWITHSHA-224"] = "SHA-224withCVC-ECDSA";
algorithms["SHA224WITHCVC-ECDSA"] = "SHA-224withCVC-ECDSA";
algorithms["SHA-224WITHCVC-ECDSA"] = "SHA-224withCVC-ECDSA";
algorithms[EacObjectIdentifiers.id_TA_ECDSA_SHA_224.Id] = "SHA-224withCVC-ECDSA";
algorithms["SHA256/CVC-ECDSA"] = "SHA-256withCVC-ECDSA";
algorithms["SHA-256/CVC-ECDSA"] = "SHA-256withCVC-ECDSA";
algorithms["CVC-ECDSAWITHSHA256"] = "SHA-256withCVC-ECDSA";
algorithms["CVC-ECDSAWITHSHA-256"] = "SHA-256withCVC-ECDSA";
algorithms["SHA256WITHCVC-ECDSA"] = "SHA-256withCVC-ECDSA";
algorithms["SHA-256WITHCVC-ECDSA"] = "SHA-256withCVC-ECDSA";
algorithms[EacObjectIdentifiers.id_TA_ECDSA_SHA_256.Id] = "SHA-256withCVC-ECDSA";
algorithms["SHA384/CVC-ECDSA"] = "SHA-384withCVC-ECDSA";
algorithms["SHA-384/CVC-ECDSA"] = "SHA-384withCVC-ECDSA";
algorithms["CVC-ECDSAWITHSHA384"] = "SHA-384withCVC-ECDSA";
algorithms["CVC-ECDSAWITHSHA-384"] = "SHA-384withCVC-ECDSA";
algorithms["SHA384WITHCVC-ECDSA"] = "SHA-384withCVC-ECDSA";
algorithms["SHA-384WITHCVC-ECDSA"] = "SHA-384withCVC-ECDSA";
algorithms[EacObjectIdentifiers.id_TA_ECDSA_SHA_384.Id] = "SHA-384withCVC-ECDSA";
algorithms["SHA512/CVC-ECDSA"] = "SHA-512withCVC-ECDSA";
algorithms["SHA-512/CVC-ECDSA"] = "SHA-512withCVC-ECDSA";
algorithms["CVC-ECDSAWITHSHA512"] = "SHA-512withCVC-ECDSA";
algorithms["CVC-ECDSAWITHSHA-512"] = "SHA-512withCVC-ECDSA";
algorithms["SHA512WITHCVC-ECDSA"] = "SHA-512withCVC-ECDSA";
algorithms["SHA-512WITHCVC-ECDSA"] = "SHA-512withCVC-ECDSA";
algorithms[EacObjectIdentifiers.id_TA_ECDSA_SHA_512.Id] = "SHA-512withCVC-ECDSA";
algorithms["NONEWITHPLAIN-ECDSA"] = "NONEwithPLAIN-ECDSA";
algorithms["PLAIN-ECDSAWITHNONE"] = "NONEwithPLAIN-ECDSA";
algorithms["SHA1/PLAIN-ECDSA"] = "SHA-1withPLAIN-ECDSA";
algorithms["SHA-1/PLAIN-ECDSA"] = "SHA-1withPLAIN-ECDSA";
algorithms["PLAIN-ECDSAWITHSHA1"] = "SHA-1withPLAIN-ECDSA";
algorithms["PLAIN-ECDSAWITHSHA-1"] = "SHA-1withPLAIN-ECDSA";
algorithms["SHA1WITHPLAIN-ECDSA"] = "SHA-1withPLAIN-ECDSA";
algorithms["SHA-1WITHPLAIN-ECDSA"] = "SHA-1withPLAIN-ECDSA";
algorithms[BsiObjectIdentifiers.ecdsa_plain_SHA1.Id] = "SHA-1withPLAIN-ECDSA";
algorithms["SHA224/PLAIN-ECDSA"] = "SHA-224withPLAIN-ECDSA";
algorithms["SHA-224/PLAIN-ECDSA"] = "SHA-224withPLAIN-ECDSA";
algorithms["PLAIN-ECDSAWITHSHA224"] = "SHA-224withPLAIN-ECDSA";
algorithms["PLAIN-ECDSAWITHSHA-224"] = "SHA-224withPLAIN-ECDSA";
algorithms["SHA224WITHPLAIN-ECDSA"] = "SHA-224withPLAIN-ECDSA";
algorithms["SHA-224WITHPLAIN-ECDSA"] = "SHA-224withPLAIN-ECDSA";
algorithms[BsiObjectIdentifiers.ecdsa_plain_SHA224.Id] = "SHA-224withPLAIN-ECDSA";
algorithms["SHA256/PLAIN-ECDSA"] = "SHA-256withPLAIN-ECDSA";
algorithms["SHA-256/PLAIN-ECDSA"] = "SHA-256withPLAIN-ECDSA";
algorithms["PLAIN-ECDSAWITHSHA256"] = "SHA-256withPLAIN-ECDSA";
algorithms["PLAIN-ECDSAWITHSHA-256"] = "SHA-256withPLAIN-ECDSA";
algorithms["SHA256WITHPLAIN-ECDSA"] = "SHA-256withPLAIN-ECDSA";
algorithms["SHA-256WITHPLAIN-ECDSA"] = "SHA-256withPLAIN-ECDSA";
algorithms[BsiObjectIdentifiers.ecdsa_plain_SHA256.Id] = "SHA-256withPLAIN-ECDSA";
algorithms["SHA384/PLAIN-ECDSA"] = "SHA-384withPLAIN-ECDSA";
algorithms["SHA-384/PLAIN-ECDSA"] = "SHA-384withPLAIN-ECDSA";
algorithms["PLAIN-ECDSAWITHSHA384"] = "SHA-384withPLAIN-ECDSA";
algorithms["PLAIN-ECDSAWITHSHA-384"] = "SHA-384withPLAIN-ECDSA";
algorithms["SHA384WITHPLAIN-ECDSA"] = "SHA-384withPLAIN-ECDSA";
algorithms["SHA-384WITHPLAIN-ECDSA"] = "SHA-384withPLAIN-ECDSA";
algorithms[BsiObjectIdentifiers.ecdsa_plain_SHA384.Id] = "SHA-384withPLAIN-ECDSA";
algorithms["SHA512/PLAIN-ECDSA"] = "SHA-512withPLAIN-ECDSA";
algorithms["SHA-512/PLAIN-ECDSA"] = "SHA-512withPLAIN-ECDSA";
algorithms["PLAIN-ECDSAWITHSHA512"] = "SHA-512withPLAIN-ECDSA";
algorithms["PLAIN-ECDSAWITHSHA-512"] = "SHA-512withPLAIN-ECDSA";
algorithms["SHA512WITHPLAIN-ECDSA"] = "SHA-512withPLAIN-ECDSA";
algorithms["SHA-512WITHPLAIN-ECDSA"] = "SHA-512withPLAIN-ECDSA";
algorithms[BsiObjectIdentifiers.ecdsa_plain_SHA512.Id] = "SHA-512withPLAIN-ECDSA";
algorithms["RIPEMD160/PLAIN-ECDSA"] = "RIPEMD160withPLAIN-ECDSA";
algorithms["PLAIN-ECDSAWITHRIPEMD160"] = "RIPEMD160withPLAIN-ECDSA";
algorithms["RIPEMD160WITHPLAIN-ECDSA"] = "RIPEMD160withPLAIN-ECDSA";
algorithms[BsiObjectIdentifiers.ecdsa_plain_RIPEMD160.Id] = "RIPEMD160withPLAIN-ECDSA";
algorithms["SHA1WITHECNR"] = "SHA-1withECNR";
algorithms["SHA-1WITHECNR"] = "SHA-1withECNR";
algorithms["SHA224WITHECNR"] = "SHA-224withECNR";
algorithms["SHA-224WITHECNR"] = "SHA-224withECNR";
algorithms["SHA256WITHECNR"] = "SHA-256withECNR";
algorithms["SHA-256WITHECNR"] = "SHA-256withECNR";
algorithms["SHA384WITHECNR"] = "SHA-384withECNR";
algorithms["SHA-384WITHECNR"] = "SHA-384withECNR";
algorithms["SHA512WITHECNR"] = "SHA-512withECNR";
algorithms["SHA-512WITHECNR"] = "SHA-512withECNR";
algorithms["GOST-3410"] = "GOST3410";
algorithms["GOST-3410-94"] = "GOST3410";
algorithms["GOST3411WITHGOST3410"] = "GOST3410";
algorithms[CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94.Id] = "GOST3410";
algorithms["ECGOST-3410"] = "ECGOST3410";
algorithms["ECGOST-3410-2001"] = "ECGOST3410";
algorithms["GOST3411WITHECGOST3410"] = "ECGOST3410";
algorithms[CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001.Id] = "ECGOST3410";
algorithms["ED25519"] = "Ed25519";
algorithms[EdECObjectIdentifiers.id_Ed25519.Id] = "Ed25519";
algorithms["ED25519CTX"] = "Ed25519ctx";
algorithms["ED25519PH"] = "Ed25519ph";
algorithms["ED448"] = "Ed448";
algorithms[EdECObjectIdentifiers.id_Ed448.Id] = "Ed448";
algorithms["ED448PH"] = "Ed448ph";
oids["MD2withRSA"] = PkcsObjectIdentifiers.MD2WithRsaEncryption;
oids["MD4withRSA"] = PkcsObjectIdentifiers.MD4WithRsaEncryption;
oids["MD5withRSA"] = PkcsObjectIdentifiers.MD5WithRsaEncryption;
oids["SHA-1withRSA"] = PkcsObjectIdentifiers.Sha1WithRsaEncryption;
oids["SHA-224withRSA"] = PkcsObjectIdentifiers.Sha224WithRsaEncryption;
oids["SHA-256withRSA"] = PkcsObjectIdentifiers.Sha256WithRsaEncryption;
oids["SHA-384withRSA"] = PkcsObjectIdentifiers.Sha384WithRsaEncryption;
oids["SHA-512withRSA"] = PkcsObjectIdentifiers.Sha512WithRsaEncryption;
oids["PSSwithRSA"] = PkcsObjectIdentifiers.IdRsassaPss;
oids["SHA-1withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
oids["SHA-224withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
oids["SHA-256withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
oids["SHA-384withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
oids["SHA-512withRSAandMGF1"] = PkcsObjectIdentifiers.IdRsassaPss;
oids["RIPEMD128withRSA"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128;
oids["RIPEMD160withRSA"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160;
oids["RIPEMD256withRSA"] = TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256;
oids["SHA-1withDSA"] = X9ObjectIdentifiers.IdDsaWithSha1;
oids["SHA-1withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha1;
oids["SHA-224withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha224;
oids["SHA-256withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha256;
oids["SHA-384withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha384;
oids["SHA-512withECDSA"] = X9ObjectIdentifiers.ECDsaWithSha512;
oids["RIPEMD160withECDSA"] = TeleTrusTObjectIdentifiers.ECSignWithRipeMD160;
oids["SHA-1withCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_1;
oids["SHA-224withCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_224;
oids["SHA-256withCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_256;
oids["SHA-384withCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_384;
oids["SHA-512withCVC-ECDSA"] = EacObjectIdentifiers.id_TA_ECDSA_SHA_512;
oids["SHA-1withPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA1;
oids["SHA-224withPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA224;
oids["SHA-256withPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA256;
oids["SHA-384withPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA384;
oids["SHA-512withPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_SHA512;
oids["RIPEMD160withPLAIN-ECDSA"] = BsiObjectIdentifiers.ecdsa_plain_RIPEMD160;
oids["GOST3410"] = CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94;
oids["ECGOST3410"] = CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001;
oids["Ed25519"] = EdECObjectIdentifiers.id_Ed25519;
oids["Ed448"] = EdECObjectIdentifiers.id_Ed448;
}
public static DerObjectIdentifier GetObjectIdentifier(string mechanism)
{
if (mechanism == null)
{
throw new ArgumentNullException("mechanism");
}
mechanism = Platform.ToUpperInvariant(mechanism);
string text = (string)algorithms[mechanism];
if (text != null)
{
mechanism = text;
}
return (DerObjectIdentifier)oids[mechanism];
}
public static Asn1Encodable GetDefaultX509Parameters(DerObjectIdentifier id)
{
return GetDefaultX509Parameters(id.Id);
}
public static Asn1Encodable GetDefaultX509Parameters(string algorithm)
{
if (algorithm == null)
{
throw new ArgumentNullException("algorithm");
}
algorithm = Platform.ToUpperInvariant(algorithm);
string text = (string)algorithms[algorithm];
if (text == null)
{
text = algorithm;
}
if (text == "PSSwithRSA")
{
return GetPssX509Parameters("SHA-1");
}
if (Platform.EndsWith(text, "withRSAandMGF1"))
{
string digestName = text.Substring(0, text.Length - "withRSAandMGF1".Length);
return GetPssX509Parameters(digestName);
}
return DerNull.Instance;
}
private static Asn1Encodable GetPssX509Parameters(string digestName)
{
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(DigestUtilities.GetObjectIdentifier(digestName), DerNull.Instance);
AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, algorithmIdentifier);
int digestSize = DigestUtilities.GetDigest(digestName).GetDigestSize();
return new RsassaPssParameters(algorithmIdentifier, maskGenAlgorithm, new DerInteger(digestSize), new DerInteger(1));
}
public static ISigner GetSigner(DerObjectIdentifier id)
{
return GetSigner(id.Id);
}
public static ISigner GetSigner(string algorithm)
{
if (algorithm == null)
{
throw new ArgumentNullException("algorithm");
}
algorithm = Platform.ToUpperInvariant(algorithm);
string text = (string)algorithms[algorithm];
if (text == null)
{
text = algorithm;
}
if (Platform.StartsWith(text, "Ed"))
{
if (text.Equals("Ed25519"))
{
return new Ed25519Signer();
}
if (text.Equals("Ed25519ctx"))
{
return new Ed25519ctxSigner(Arrays.EmptyBytes);
}
if (text.Equals("Ed25519ph"))
{
return new Ed25519phSigner(Arrays.EmptyBytes);
}
if (text.Equals("Ed448"))
{
return new Ed448Signer(Arrays.EmptyBytes);
}
if (text.Equals("Ed448ph"))
{
return new Ed448phSigner(Arrays.EmptyBytes);
}
}
if (text.Equals("RSA"))
{
return new RsaDigestSigner((IDigest)new NullDigest(), (AlgorithmIdentifier)null);
}
if (text.Equals("RAWRSASSA-PSS"))
{
return PssSigner.CreateRawSigner(new RsaBlindedEngine(), new Sha1Digest());
}
if (text.Equals("PSSwithRSA"))
{
return new PssSigner(new RsaBlindedEngine(), new Sha1Digest());
}
if (Platform.EndsWith(text, "withRSA"))
{
string algorithm2 = text.Substring(0, text.LastIndexOf("with"));
IDigest digest = DigestUtilities.GetDigest(algorithm2);
return new RsaDigestSigner(digest);
}
if (Platform.EndsWith(text, "withRSAandMGF1"))
{
string algorithm3 = text.Substring(0, text.LastIndexOf("with"));
IDigest digest2 = DigestUtilities.GetDigest(algorithm3);
return new PssSigner(new RsaBlindedEngine(), digest2);
}
if (Platform.EndsWith(text, "withDSA"))
{
string algorithm4 = text.Substring(0, text.LastIndexOf("with"));
IDigest digest3 = DigestUtilities.GetDigest(algorithm4);
return new DsaDigestSigner(new DsaSigner(), digest3);
}
if (Platform.EndsWith(text, "withECDSA"))
{
string algorithm5 = text.Substring(0, text.LastIndexOf("with"));
IDigest digest4 = DigestUtilities.GetDigest(algorithm5);
return new DsaDigestSigner(new ECDsaSigner(), digest4);
}
if (Platform.EndsWith(text, "withCVC-ECDSA") || Platform.EndsWith(text, "withPLAIN-ECDSA"))
{
string algorithm6 = text.Substring(0, text.LastIndexOf("with"));
IDigest digest5 = DigestUtilities.GetDigest(algorithm6);
return new DsaDigestSigner(new ECDsaSigner(), digest5, PlainDsaEncoding.Instance);
}
if (Platform.EndsWith(text, "withECNR"))
{
string algorithm7 = text.Substring(0, text.LastIndexOf("with"));
IDigest digest6 = DigestUtilities.GetDigest(algorithm7);
return new DsaDigestSigner(new ECNRSigner(), digest6);
}
if (text.Equals("GOST3410"))
{
return new Gost3410DigestSigner(new Gost3410Signer(), new Gost3411Digest());
}
if (text.Equals("ECGOST3410"))
{
return new Gost3410DigestSigner(new ECGost3410Signer(), new Gost3411Digest());
}
if (text.Equals("SHA1WITHRSA/ISO9796-2"))
{
return new Iso9796d2Signer(new RsaBlindedEngine(), new Sha1Digest(), isImplicit: true);
}
if (text.Equals("MD5WITHRSA/ISO9796-2"))
{
return new Iso9796d2Signer(new RsaBlindedEngine(), new MD5Digest(), isImplicit: true);
}
if (text.Equals("RIPEMD160WITHRSA/ISO9796-2"))
{
return new Iso9796d2Signer(new RsaBlindedEngine(), new RipeMD160Digest(), isImplicit: true);
}
if (Platform.EndsWith(text, "/X9.31"))
{
string text2 = text.Substring(0, text.Length - "/X9.31".Length);
int num = Platform.IndexOf(text2, "WITH");
if (num > 0)
{
int num2 = num + "WITH".Length;
string algorithm8 = text2.Substring(0, num);
IDigest digest7 = DigestUtilities.GetDigest(algorithm8);
string text3 = text2.Substring(num2, text2.Length - num2);
if (text3.Equals("RSA"))
{
IAsymmetricBlockCipher cipher = new RsaBlindedEngine();
return new X931Signer(cipher, digest7);
}
}
}
throw new SecurityUtilityException("Signer " + algorithm + " not recognised.");
}
public static string GetEncodingName(DerObjectIdentifier oid)
{
return (string)algorithms[oid.Id];
}
public static ISigner InitSigner(DerObjectIdentifier algorithmOid, bool forSigning, AsymmetricKeyParameter privateKey, SecureRandom random)
{
return InitSigner(algorithmOid.Id, forSigning, privateKey, random);
}
public static ISigner InitSigner(string algorithm, bool forSigning, AsymmetricKeyParameter privateKey, SecureRandom random)
{
ISigner signer = GetSigner(algorithm);
signer.Init(forSigning, ParameterUtilities.WithRandom(privateKey, random));
return signer;
}
}

View File

@@ -0,0 +1,138 @@
using System;
using System.Collections;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Kisa;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Ntt;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Security;
public sealed class WrapperUtilities
{
private enum WrapAlgorithm
{
AESWRAP,
CAMELLIAWRAP,
DESEDEWRAP,
RC2WRAP,
SEEDWRAP,
DESEDERFC3211WRAP,
AESRFC3211WRAP,
CAMELLIARFC3211WRAP
}
private class BufferedCipherWrapper : IWrapper
{
private readonly IBufferedCipher cipher;
private bool forWrapping;
public string AlgorithmName => cipher.AlgorithmName;
public BufferedCipherWrapper(IBufferedCipher cipher)
{
this.cipher = cipher;
}
public void Init(bool forWrapping, ICipherParameters parameters)
{
this.forWrapping = forWrapping;
cipher.Init(forWrapping, parameters);
}
public byte[] Wrap(byte[] input, int inOff, int length)
{
if (!forWrapping)
{
throw new InvalidOperationException("Not initialised for wrapping");
}
return cipher.DoFinal(input, inOff, length);
}
public byte[] Unwrap(byte[] input, int inOff, int length)
{
if (forWrapping)
{
throw new InvalidOperationException("Not initialised for unwrapping");
}
return cipher.DoFinal(input, inOff, length);
}
}
private static readonly IDictionary algorithms;
private WrapperUtilities()
{
}
static WrapperUtilities()
{
algorithms = Platform.CreateHashtable();
((WrapAlgorithm)Enums.GetArbitraryValue(typeof(WrapAlgorithm))).ToString();
algorithms[NistObjectIdentifiers.IdAes128Wrap.Id] = "AESWRAP";
algorithms[NistObjectIdentifiers.IdAes192Wrap.Id] = "AESWRAP";
algorithms[NistObjectIdentifiers.IdAes256Wrap.Id] = "AESWRAP";
algorithms[NttObjectIdentifiers.IdCamellia128Wrap.Id] = "CAMELLIAWRAP";
algorithms[NttObjectIdentifiers.IdCamellia192Wrap.Id] = "CAMELLIAWRAP";
algorithms[NttObjectIdentifiers.IdCamellia256Wrap.Id] = "CAMELLIAWRAP";
algorithms[PkcsObjectIdentifiers.IdAlgCms3DesWrap.Id] = "DESEDEWRAP";
algorithms["TDEAWRAP"] = "DESEDEWRAP";
algorithms[PkcsObjectIdentifiers.IdAlgCmsRC2Wrap.Id] = "RC2WRAP";
algorithms[KisaObjectIdentifiers.IdNpkiAppCmsSeedWrap.Id] = "SEEDWRAP";
}
public static IWrapper GetWrapper(DerObjectIdentifier oid)
{
return GetWrapper(oid.Id);
}
public static IWrapper GetWrapper(string algorithm)
{
string text = Platform.ToUpperInvariant(algorithm);
string text2 = (string)algorithms[text];
if (text2 == null)
{
text2 = text;
}
try
{
switch ((WrapAlgorithm)Enums.GetEnumValue(typeof(WrapAlgorithm), text2))
{
case WrapAlgorithm.AESWRAP:
return new AesWrapEngine();
case WrapAlgorithm.CAMELLIAWRAP:
return new CamelliaWrapEngine();
case WrapAlgorithm.DESEDEWRAP:
return new DesEdeWrapEngine();
case WrapAlgorithm.RC2WRAP:
return new RC2WrapEngine();
case WrapAlgorithm.SEEDWRAP:
return new SeedWrapEngine();
case WrapAlgorithm.DESEDERFC3211WRAP:
return new Rfc3211WrapEngine(new DesEdeEngine());
case WrapAlgorithm.AESRFC3211WRAP:
return new Rfc3211WrapEngine(new AesEngine());
case WrapAlgorithm.CAMELLIARFC3211WRAP:
return new Rfc3211WrapEngine(new CamelliaEngine());
}
}
catch (ArgumentException)
{
}
IBufferedCipher cipher = CipherUtilities.GetCipher(algorithm);
if (cipher != null)
{
return new BufferedCipherWrapper(cipher);
}
throw new SecurityUtilityException("Wrapper " + algorithm + " not recognised.");
}
public static string GetAlgorithmName(DerObjectIdentifier oid)
{
return (string)algorithms[oid.Id];
}
}