Exemplo n.º 1
0
 // Derive a key for a specific cryptographic algorithm.
 public byte[] CryptDeriveKey(String algname, String alghashname,
                              int keySize, byte[] rgbIV)
 {
     if ((algname == "DES" || algname == "RC2") &&
         alghashname == "MD5" && keySize == 8)
     {
         // Use the older PKCS #5 password generation routine.
         MD5 md5 = new MD5CryptoServiceProvider();
         if (strPassword != null)
         {
             byte[] pwd = Encoding.UTF8.GetBytes(strPassword);
             md5.InternalHashCore(pwd, 0, pwd.Length);
             Array.Clear(pwd, 0, pwd.Length);
         }
         if (rgbSalt != null)
         {
             md5.InternalHashCore(rgbSalt, 0, rgbSalt.Length);
         }
         byte[] tempHash = md5.InternalHashFinal();
         md5.Initialize();
         int count = iterations;
         while (count > 1)
         {
             md5.InternalHashCore(tempHash, 0, tempHash.Length);
             Array.Clear(tempHash, 0, tempHash.Length);
             tempHash = md5.InternalHashFinal();
             md5.Initialize();
             --count;
         }
         byte[] key = new byte [8];
         Array.Copy(tempHash, 0, key, 0, 8);
         if (rgbIV != null)
         {
             Array.Copy(tempHash, 8, rgbIV, 0, 8);
         }
         Array.Clear(tempHash, 0, tempHash.Length);
         return(key);
     }
     else
     {
         // Use the newer PKCS #5 password generation routine.
         Reset();
         if (alghashname != null)
         {
             strHashName = alghashname;
         }
         byte[] result = GetBytes(keySize);
         if (rgbIV != null)
         {
             byte[] iv = GetBytes(rgbIV.Length);
             Array.Copy(iv, 0, rgbIV, 0, rgbIV.Length);
             Array.Clear(iv, 0, iv.Length);
         }
         return(result);
     }
 }
	// Derive a key for a specific cryptographic algorithm.
	public byte[] CryptDeriveKey(String algname, String alghashname,
								 int keySize, byte[] rgbIV)
			{
				if((algname == "DES" || algname == "RC2") &&
			   	   alghashname == "MD5" && keySize == 8)
				{
					// Use the older PKCS #5 password generation routine.
					MD5 md5 = new MD5CryptoServiceProvider();
					if(strPassword != null)
					{
						byte[] pwd = Encoding.UTF8.GetBytes(strPassword);
						md5.InternalHashCore(pwd, 0, pwd.Length);
						Array.Clear(pwd, 0, pwd.Length);
					}
					if(rgbSalt != null)
					{
						md5.InternalHashCore(rgbSalt, 0, rgbSalt.Length);
					}
					byte[] tempHash = md5.InternalHashFinal();
					md5.Initialize();
					int count = iterations;
					while(count > 1)
					{
						md5.InternalHashCore(tempHash, 0, tempHash.Length);
						Array.Clear(tempHash, 0, tempHash.Length);
						tempHash = md5.InternalHashFinal();
						md5.Initialize();
						--count;
					}
					byte[] key = new byte [8];
					Array.Copy(tempHash, 0, key, 0, 8);
					if(rgbIV != null)
					{
						Array.Copy(tempHash, 8, rgbIV, 0, 8);
					}
					Array.Clear(tempHash, 0, tempHash.Length);
					return key;
				}
				else
				{
					// Use the newer PKCS #5 password generation routine.
					Reset();
					if(alghashname != null)
					{
						strHashName = alghashname;
					}
					byte[] result = GetBytes(keySize);
					if(rgbIV != null)
					{
						byte[] iv = GetBytes(rgbIV.Length);
						Array.Copy(iv, 0, rgbIV, 0, rgbIV.Length);
						Array.Clear(iv, 0, iv.Length);
					}
					return result;
				}
			}
	// Parse the contents of a certificate data block.
	private void Parse(byte[] data)
			{
				// Clone the data for internal storage.
				rawData = (byte[])(data.Clone());

				// Parse the ASN.1 data to get the field we are interested in.
				ASN1Parser parser = new ASN1Parser(rawData);
				ASN1Parser signed = parser.GetSequence();
				ASN1Parser certInfo = signed.GetSequence();
				if(certInfo.Type == ASN1Parser.ContextSpecific(0))
				{
					// Skip the version field.
					certInfo.Skip();
				}
				serialNumber = certInfo.GetContentsAsArray(ASN1Type.Integer);
				ASN1Parser algId = certInfo.GetSequence();
				issuer = ParseName(certInfo);
				ASN1Parser validity = certInfo.GetSequence();
				effectiveDate = validity.GetUTCTime();
				expirationDate = validity.GetUTCTime();
				name = ParseName(certInfo);
				ASN1Parser keyInfo = certInfo.GetSequence();
				algId = keyInfo.GetSequence();
				keyAlgorithm = ToHex(algId.GetObjectIdentifier());
				if(algId.IsAtEnd() || algId.IsNull())
				{
					keyAlgorithmParameters = null;
				}
				else
				{
					keyAlgorithmParameters = algId.GetWholeAsArray();
				}
				publicKey = keyInfo.GetBitString();

#if CONFIG_CRYPTO
				// Construct an MD5 hash of the certificate.  Is this correct?
				MD5 md5 = new MD5CryptoServiceProvider();
				md5.InternalHashCore(rawData, 0, rawData.Length);
				hash = md5.InternalHashFinal();
				md5.Initialize();
#endif
			}