internal static byte[] TripleDESKeyWrapDecrypt (byte[] rgbKey, byte[] rgbEncryptedWrappedKeyData) {
// Check to see whether the length of the encrypted key is reasonable
if (rgbEncryptedWrappedKeyData.Length != 32 && rgbEncryptedWrappedKeyData.Length != 40
&& rgbEncryptedWrappedKeyData.Length != 48)
throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_KW_BadKeySize"));
TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
// Assume no padding, use CBC mode
tripleDES.Padding = PaddingMode.None;
ICryptoTransform dec1 = tripleDES.CreateDecryptor(rgbKey, s_rgbTripleDES_KW_IV);
byte[] temp2 = dec1.TransformFinalBlock(rgbEncryptedWrappedKeyData, 0, rgbEncryptedWrappedKeyData.Length);
Array.Reverse(temp2);
// Get the IV and temp1
byte[] rgbIV = new byte[8];
Buffer.BlockCopy(temp2, 0, rgbIV, 0, 8);
byte[] temp1 = new byte[temp2.Length - rgbIV.Length];
Buffer.BlockCopy(temp2, 8, temp1, 0, temp1.Length);
ICryptoTransform dec2 = tripleDES.CreateDecryptor(rgbKey, rgbIV);
byte[] rgbWKCKS = dec2.TransformFinalBlock(temp1, 0, temp1.Length);
// checksum the key
byte[] rgbWrappedKeyData = new byte[rgbWKCKS.Length - 8];
Buffer.BlockCopy(rgbWKCKS, 0, rgbWrappedKeyData, 0, rgbWrappedKeyData.Length);
SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
byte[] rgbCKS = sha.ComputeHash(rgbWrappedKeyData);
for (int index = rgbWrappedKeyData.Length, index1 = 0; index < rgbWKCKS.Length; index++, index1++)
if (rgbWKCKS[index] != rgbCKS[index1])
throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_BadWrappedKeySize"));
return rgbWrappedKeyData;
}