context: using System;
  using System.IO;
  using System.Text;
  
  using Org.BouncyCastle.Asn1.CryptoPro;
  using Org.BouncyCastle.Asn1.X509;
  using Org.BouncyCastle.Crypto;
  using Org.BouncyCastle.Crypto.Parameters;
  using Org.BouncyCastle.Math;
  using Org.BouncyCastle.Security;
  using Org.BouncyCastle.Utilities.Encoders;
  using Org.BouncyCastle.Utilities.IO;
  using Org.BouncyCastle.X509;
  using Org.BouncyCastle.X509.Extension;
  
  namespace Org.BouncyCastle.Cms.Tests
  {
  	public class CmsTestUtil
  	{
  		public static SecureRandom rand;
  		private static IAsymmetricCipherKeyPairGenerator kpg;
  		private static IAsymmetricCipherKeyPairGenerator gostKpg;
  		private static IAsymmetricCipherKeyPairGenerator dsaKpg;
  		private static IAsymmetricCipherKeyPairGenerator ecGostKpg;
  		private static IAsymmetricCipherKeyPairGenerator ecDsaKpg;
  		public static CipherKeyGenerator aes192kg;
  		public static CipherKeyGenerator desede128kg;
  		public static CipherKeyGenerator desede192kg;
  		public static CipherKeyGenerator rc240kg;
  		public static CipherKeyGenerator rc264kg;
  		public static CipherKeyGenerator rc2128kg;
  		public static CipherKeyGenerator aesKg;
  		public static CipherKeyGenerator seedKg;
  		public static CipherKeyGenerator camelliaKg;
  		public static BigInteger serialNumber;
  
  		private static readonly byte[] attrCert = Base64.Decode(
  			  "MIIHQDCCBqkCAQEwgZChgY2kgYowgYcxHDAaBgkqhkiG9w0BCQEWDW1sb3JjaEB2"
  			+ "dC5lZHUxHjAcBgNVBAMTFU1hcmt1cyBMb3JjaCAobWxvcmNoKTEbMBkGA1UECxMS"
  			+ "VmlyZ2luaWEgVGVjaCBVc2VyMRAwDgYDVQQLEwdDbGFzcyAyMQswCQYDVQQKEwJ2"
  			+ "dDELMAkGA1UEBhMCVVMwgYmkgYYwgYMxGzAZBgkqhkiG9w0BCQEWDHNzaGFoQHZ0"
  			+ "LmVkdTEbMBkGA1UEAxMSU3VtaXQgU2hhaCAoc3NoYWgpMRswGQYDVQQLExJWaXJn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25NYXRjaD4KICAgPC9BY3Rpb24+"
  			+ "CiAgPC9BY3Rpb25zPgogPC9UYXJnZXQ+CjwvUnVsZT4KMA0GCSqGSIb3DQEBBAUA"
  			+ "A4GBAGiJSM48XsY90HlYxGmGVSmNR6ZW2As+bot3KAfiCIkUIOAqhcphBS23egTr"
  			+ "6asYwy151HshbPNYz+Cgeqs45KkVzh7bL/0e1r8sDVIaaGIkjHK3CqBABnfSayr3"
  			+ "Rd1yBoDdEv8Qb+3eEPH6ab9021AsLEnJ6LWTmybbOpMNZ3tv");
  
  		private static IAsymmetricCipherKeyPairGenerator Kpg
  		{
  			get
  			{
  				if (kpg == null)
  				{
  					kpg = GeneratorUtilities.GetKeyPairGenerator("RSA");
  					kpg.Init(new RsaKeyGenerationParameters(
  						BigInteger.ValueOf(17), rand, 1024, 25));
  				}
  
  				return kpg;
  			}
  		}
  
  		private static IAsymmetricCipherKeyPairGenerator GostKpg
  		{
  			get
  			{
  				if (gostKpg == null)
  				{
  					gostKpg = GeneratorUtilities.GetKeyPairGenerator("GOST3410");
  					gostKpg.Init(
  						new Gost3410KeyGenerationParameters(
  							rand,
  							CryptoProObjectIdentifiers.GostR3410x94CryptoProA));
  				}
  
  				return gostKpg;
  			}
  		}
  
  		private static IAsymmetricCipherKeyPairGenerator DsaKpg
  		{
  			get
  			{
  				if (dsaKpg == null)
  				{
  					DsaParameters dsaSpec = new DsaParameters(
  						new BigInteger("7434410770759874867539421675728577177024889699586189000788950934679315164676852047058354758883833299702695428196962057871264685291775577130504050839126673"),
  						new BigInteger("1138656671590261728308283492178581223478058193247"),
  						new BigInteger("4182906737723181805517018315469082619513954319976782448649747742951189003482834321192692620856488639629011570381138542789803819092529658402611668375788410"));
  					dsaKpg = GeneratorUtilities.GetKeyPairGenerator("DSA");
  					dsaKpg.Init(new DsaKeyGenerationParameters(rand, dsaSpec));
  				}
  
  				return dsaKpg;
  			}
  		}
  
  		private static IAsymmetricCipherKeyPairGenerator ECGostKpg
  		{
  			get
  			{
  				if (ecGostKpg == null)
  				{
  					ecGostKpg = GeneratorUtilities.GetKeyPairGenerator("ECGOST3410");
  					ecGostKpg.Init(
  						new ECKeyGenerationParameters(
  							CryptoProObjectIdentifiers.GostR3410x2001CryptoProA,
  							new SecureRandom()));
  				}
  
  				return ecGostKpg;
  			}
  		}
  
  		private static IAsymmetricCipherKeyPairGenerator ECDsaKpg
  		{
  			get
  			{
  				if (ecDsaKpg == null)
  				{
  					ecDsaKpg = GeneratorUtilities.GetKeyPairGenerator("ECDSA");
  					ecDsaKpg.Init(new KeyGenerationParameters(rand, 239));
  				}
  
  				return ecDsaKpg;
  			}
  		}
  
  		static CmsTestUtil()
  		{
  		    try
  		    {
  		        rand = new SecureRandom();
  
  				aes192kg = GeneratorUtilities.GetKeyGenerator("AES");
  				aes192kg.Init(new KeyGenerationParameters(rand, 192));
  
  		        desede128kg = GeneratorUtilities.GetKeyGenerator("DESEDE");
  				desede128kg.Init(new KeyGenerationParameters(rand, 112));
  
  				desede192kg = GeneratorUtilities.GetKeyGenerator("DESEDE");
  				desede192kg.Init(new KeyGenerationParameters(rand, 168));
  
  				rc240kg = GeneratorUtilities.GetKeyGenerator("RC2");
  				rc240kg.Init(new KeyGenerationParameters(rand, 40));
  
  				rc264kg = GeneratorUtilities.GetKeyGenerator("RC2");
  				rc264kg.Init(new KeyGenerationParameters(rand, 64));
  
  				rc2128kg = GeneratorUtilities.GetKeyGenerator("RC2");
  				rc2128kg.Init(new KeyGenerationParameters(rand, 128));
  
  				aesKg = GeneratorUtilities.GetKeyGenerator("AES");
  
  				seedKg = GeneratorUtilities.GetKeyGenerator("SEED");
  
  				camelliaKg = GeneratorUtilities.GetKeyGenerator("Camellia");
  
  				serialNumber = BigInteger.One;
  		    }
  		    catch (Exception ex)
  		    {
  		        throw new Exception(ex.ToString());
  		    }
  		}
  
  		public static string DumpBase64(
  			byte[] data)
  		{
  			StringBuilder buf = new StringBuilder();
  
  			data = Base64.Encode(data);
  
  			for (int i = 0; i < data.Length; i += 64)
  			{
  
gt: 				if (i + 64 < data.Length)