[System.Security.SecurityCritical] // auto-generated private ICryptoTransform _NewEncryptor (byte[] rgbKey, CipherMode mode, byte[] rgbIV, int feedbackSize, CryptoAPITransformMode encryptMode) { int cArgs = 0; int[] rgArgIds = new int[10]; Object[] rgArgValues = new Object[10]; // Check for bad values // 1) we don't support OFB mode in DESCryptoServiceProvider if (mode == CipherMode.OFB) throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported")); // 2) we only support CFB with a feedback size of 8 bits if ((mode == CipherMode.CFB) && (feedbackSize != 8)) throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported")); // Build the key if one does not already exist if (rgbKey == null) { rgbKey = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbKey); } // Set the mode for the encryptor (defaults to CBC) if (mode != CipherMode.CBC) { rgArgIds[cArgs] = Constants.KP_MODE; rgArgValues[cArgs] = mode; cArgs += 1; } // If not ECB mode -- pass in an IV if (mode != CipherMode.ECB) { if (rgbIV == null) { rgbIV = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbIV); } // // We truncate IV's that are longer than the block size to 8 bytes : this is // done to maintain backward compatibility with the behavior shipped in V1.x. // The call to set the IV in CryptoAPI will ignore any bytes after the first 8 // bytes. We'll still reject IV's that are shorter than the block size though. // if (rgbIV.Length < 8) throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize")); rgArgIds[cArgs] = Constants.KP_IV; rgArgValues[cArgs] = rgbIV; cArgs += 1; } // If doing OFB or CFB, then we need to set the feed back loop size if ((mode == CipherMode.OFB) || (mode == CipherMode.CFB)) { rgArgIds[cArgs] = Constants.KP_MODE_BITS; rgArgValues[cArgs] = feedbackSize; cArgs += 1; } // Create the encryptpr/decryptor object return new CryptoAPITransform(Constants.CALG_DES, cArgs, rgArgIds, rgArgValues, rgbKey, PaddingValue, mode, BlockSizeValue, feedbackSize, false, encryptMode); }
private ICryptoTransform _NewEncryptor(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int feedbackSize, CryptoAPITransformMode encryptMode) { int index = 0; int[] rgArgIds = new int[10]; object[] rgArgValues = new object[10]; int algid = 0x6603; if (mode == CipherMode.OFB) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported")); } if ((mode == CipherMode.CFB) && (feedbackSize != 8)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported")); } if (rgbKey == null) { rgbKey = new byte[base.KeySizeValue / 8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbKey); } if (mode != CipherMode.CBC) { rgArgIds[index] = 4; rgArgValues[index] = mode; index++; } if (mode != CipherMode.ECB) { if (rgbIV == null) { rgbIV = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbIV); } if (rgbIV.Length < 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize")); } rgArgIds[index] = 1; rgArgValues[index] = rgbIV; index++; } if ((mode == CipherMode.OFB) || (mode == CipherMode.CFB)) { rgArgIds[index] = 5; rgArgValues[index] = feedbackSize; index++; } if (rgbKey.Length == 0x10) { algid = 0x6609; } return new CryptoAPITransform(algid, index, rgArgIds, rgArgValues, rgbKey, base.PaddingValue, mode, base.BlockSizeValue, feedbackSize, false, encryptMode); }
private ICryptoTransform _NewEncryptor(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int effectiveKeySize, int feedbackSize, CryptoAPITransformMode encryptMode) { int index = 0; int[] rgArgIds = new int[10]; object[] rgArgValues = new object[10]; if (mode == CipherMode.OFB) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported")); } if ((mode == CipherMode.CFB) && (feedbackSize != 8)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported")); } if (rgbKey == null) { rgbKey = new byte[base.KeySizeValue / 8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbKey); } int bitLength = rgbKey.Length * 8; if (!base.ValidKeySize(bitLength)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize")); } rgArgIds[index] = 0x13; if (base.EffectiveKeySizeValue == 0) { rgArgValues[index] = bitLength; } else { rgArgValues[index] = effectiveKeySize; } index++; if (mode != CipherMode.CBC) { rgArgIds[index] = 4; rgArgValues[index] = mode; index++; } if (mode != CipherMode.ECB) { if (rgbIV == null) { rgbIV = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbIV); } if (rgbIV.Length < 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize")); } rgArgIds[index] = 1; rgArgValues[index] = rgbIV; index++; } if ((mode == CipherMode.OFB) || (mode == CipherMode.CFB)) { rgArgIds[index] = 5; rgArgValues[index] = feedbackSize; index++; } if (!Utils.HasAlgorithm(0x6602, bitLength)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_AlgKeySizeNotAvailable", new object[] { bitLength })); } return(new CryptoAPITransform(0x6602, index, rgArgIds, rgArgValues, rgbKey, base.PaddingValue, mode, base.BlockSizeValue, feedbackSize, this.m_use40bitSalt, encryptMode)); }
[System.Security.SecurityCritical] // auto-generated private ICryptoTransform _NewEncryptor (byte[] rgbKey, CipherMode mode, byte[] rgbIV, int effectiveKeySize, int feedbackSize, CryptoAPITransformMode encryptMode) { int cArgs = 0; int[] rgArgIds = new int[10]; Object[] rgArgValues = new Object[10]; // Check for bad values // 1) we don't support OFB mode in RC2_CSP if (mode == CipherMode.OFB) throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported")); // 2) we only support CFB with a feedback size of 8 bits if ((mode == CipherMode.CFB) && (feedbackSize != 8)) throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported")); if (rgbKey == null) { rgbKey = new byte[KeySizeValue/8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbKey); } // Check the rgbKey size int keySizeValue = rgbKey.Length * 8; if (!ValidKeySize(keySizeValue)) throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize")); // Deal with effective key length questions rgArgIds[cArgs] = Constants.KP_EFFECTIVE_KEYLEN; if (EffectiveKeySizeValue == 0) { rgArgValues[cArgs] = keySizeValue; } else { rgArgValues[cArgs] = effectiveKeySize; } cArgs += 1; // Set the mode for the encryptor (defaults to CBC) if (mode != CipherMode.CBC) { rgArgIds[cArgs] = Constants.KP_MODE; rgArgValues[cArgs] = mode; cArgs += 1; } // If not ECB mode -- pass in an IV if (mode != CipherMode.ECB) { if (rgbIV == null) { rgbIV = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbIV); } // // We truncate IV's that are longer than the block size to 8 bytes : this is // done to maintain backward compatibility with the behavior shipped in V1.x. // The call to set the IV in CryptoAPI will ignore any bytes after the first 8 // bytes. We'll still reject IV's that are shorter than the block size though. // if (rgbIV.Length < 8) throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize")); rgArgIds[cArgs] = Constants.KP_IV; rgArgValues[cArgs] = rgbIV; cArgs += 1; } // If doing OFB or CFB, then we need to set the feed back loop size if ((mode == CipherMode.OFB) || (mode == CipherMode.CFB)) { rgArgIds[cArgs] = Constants.KP_MODE_BITS; rgArgValues[cArgs] = feedbackSize; cArgs += 1; } if (!Utils.HasAlgorithm(Constants.CALG_RC2, keySizeValue)) throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_AlgKeySizeNotAvailable", keySizeValue)); // Create the encryptor/decryptor object return new CryptoAPITransform(Constants.CALG_RC2, cArgs, rgArgIds, rgArgValues, rgbKey, PaddingValue, mode, BlockSizeValue, feedbackSize, m_use40bitSalt, encryptMode); }
private ICryptoTransform _NewEncryptor(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int effectiveKeySize, int feedbackSize, CryptoAPITransformMode encryptMode) { int index = 0; int[] rgArgIds = new int[10]; object[] rgArgValues = new object[10]; if (mode == CipherMode.OFB) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported")); } if ((mode == CipherMode.CFB) && (feedbackSize != 8)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported")); } if (rgbKey == null) { rgbKey = new byte[base.KeySizeValue / 8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbKey); } int bitLength = rgbKey.Length * 8; if (!base.ValidKeySize(bitLength)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize")); } rgArgIds[index] = 0x13; if (base.EffectiveKeySizeValue == 0) { rgArgValues[index] = bitLength; } else { rgArgValues[index] = effectiveKeySize; } index++; if (mode != CipherMode.CBC) { rgArgIds[index] = 4; rgArgValues[index] = mode; index++; } if (mode != CipherMode.ECB) { if (rgbIV == null) { rgbIV = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbIV); } if (rgbIV.Length < 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize")); } rgArgIds[index] = 1; rgArgValues[index] = rgbIV; index++; } if ((mode == CipherMode.OFB) || (mode == CipherMode.CFB)) { rgArgIds[index] = 5; rgArgValues[index] = feedbackSize; index++; } if (!Utils.HasAlgorithm(0x6602, bitLength)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_AlgKeySizeNotAvailable", new object[] { bitLength })); } return new CryptoAPITransform(0x6602, index, rgArgIds, rgArgValues, rgbKey, base.PaddingValue, mode, base.BlockSizeValue, feedbackSize, this.m_use40bitSalt, encryptMode); }
[System.Security.SecurityCritical] // auto-generated internal CryptoAPITransform(int algid, int cArgs, int[] rgArgIds, Object[] rgArgValues, byte[] rgbKey, PaddingMode padding, CipherMode cipherChainingMode, int blockSize, int feedbackSize, bool useSalt, CryptoAPITransformMode encDecMode) { int dwValue; byte[] rgbValue; BlockSizeValue = blockSize; ModeValue = cipherChainingMode; PaddingValue = padding; encryptOrDecrypt = encDecMode; // Copy the input args int _cArgs = cArgs; int[] _rgArgIds = new int[rgArgIds.Length]; Array.Copy(rgArgIds, _rgArgIds, rgArgIds.Length); _rgbKey = new byte[rgbKey.Length]; Array.Copy(rgbKey, _rgbKey, rgbKey.Length); Object[] _rgArgValues = new Object[rgArgValues.Length]; // an element of rgArgValues can only be an int or a byte[] for (int j = 0; j < rgArgValues.Length; j++) { if (rgArgValues[j] is byte[]) { byte[] rgbOrig = (byte[])rgArgValues[j]; byte[] rgbNew = new byte[rgbOrig.Length]; Array.Copy(rgbOrig, rgbNew, rgbOrig.Length); _rgArgValues[j] = rgbNew; continue; } if (rgArgValues[j] is int) { _rgArgValues[j] = (int)rgArgValues[j]; continue; } if (rgArgValues[j] is CipherMode) { _rgArgValues[j] = (int)rgArgValues[j]; continue; } } _safeProvHandle = Utils.AcquireProvHandle(new CspParameters(Utils.DefaultRsaProviderType)); SafeKeyHandle safeKeyHandle = SafeKeyHandle.InvalidHandle; // _ImportBulkKey will check for failures and throw an exception Utils._ImportBulkKey(_safeProvHandle, algid, useSalt, _rgbKey, ref safeKeyHandle); _safeKeyHandle = safeKeyHandle; for (int i = 0; i < cArgs; i++) { switch (rgArgIds[i]) { case Constants.KP_IV: IVValue = (byte[])_rgArgValues[i]; rgbValue = IVValue; Utils.SetKeyParamRgb(_safeKeyHandle, _rgArgIds[i], rgbValue, rgbValue.Length); break; case Constants.KP_MODE: ModeValue = (CipherMode)_rgArgValues[i]; dwValue = (Int32)_rgArgValues[i]; SetAsDWord: Utils.SetKeyParamDw(_safeKeyHandle, _rgArgIds[i], dwValue); break; case Constants.KP_MODE_BITS: dwValue = (Int32)_rgArgValues[i]; goto SetAsDWord; case Constants.KP_EFFECTIVE_KEYLEN: dwValue = (Int32)_rgArgValues[i]; goto SetAsDWord; default: throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeyParameter"), "_rgArgIds[i]"); } } }
internal CryptoAPITransform(int algid, int cArgs, int[] rgArgIds, object[] rgArgValues, byte[] rgbKey, PaddingMode padding, CipherMode cipherChainingMode, int blockSize, int feedbackSize, bool useSalt, CryptoAPITransformMode encDecMode) { this.BlockSizeValue = blockSize; this.ModeValue = cipherChainingMode; this.PaddingValue = padding; this.encryptOrDecrypt = encDecMode; int[] numArray1 = new int[rgArgIds.Length]; Array.Copy((Array)rgArgIds, (Array)numArray1, rgArgIds.Length); this._rgbKey = new byte[rgbKey.Length]; Array.Copy((Array)rgbKey, (Array)this._rgbKey, rgbKey.Length); object[] objArray = new object[rgArgValues.Length]; for (int index = 0; index < rgArgValues.Length; ++index) { if (rgArgValues[index] is byte[]) { byte[] numArray2 = (byte[])rgArgValues[index]; byte[] numArray3 = new byte[numArray2.Length]; Array.Copy((Array)numArray2, (Array)numArray3, numArray2.Length); objArray[index] = (object)numArray3; } else if (rgArgValues[index] is int) { objArray[index] = (object)(int)rgArgValues[index]; } else if (rgArgValues[index] is CipherMode) { objArray[index] = (object)(int)rgArgValues[index]; } } this._safeProvHandle = Utils.AcquireProvHandle(new CspParameters(24)); SafeKeyHandle invalidHandle = SafeKeyHandle.InvalidHandle; Utils._ImportBulkKey(this._safeProvHandle, algid, useSalt, this._rgbKey, ref invalidHandle); this._safeKeyHandle = invalidHandle; for (int index = 0; index < cArgs; ++index) { int dwValue; switch (rgArgIds[index]) { case 1: this.IVValue = (byte[])objArray[index]; byte[] numArray2 = this.IVValue; SafeKeyHandle hKey = this._safeKeyHandle; int num = numArray1[index]; byte[] numArray3 = numArray2; int length = numArray3.Length; Utils.SetKeyParamRgb(hKey, num, numArray3, length); continue; case 4: this.ModeValue = (CipherMode)objArray[index]; dwValue = (int)objArray[index]; break; case 5: dwValue = (int)objArray[index]; break; case 19: dwValue = (int)objArray[index]; break; default: throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeyParameter"), "_rgArgIds[i]"); } Utils.SetKeyParamDw(this._safeKeyHandle, numArray1[index], dwValue); } }
[System.Security.SecurityCritical] // auto-generated private ICryptoTransform _NewEncryptor(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int feedbackSize, CryptoAPITransformMode encryptMode) { int cArgs = 0; int[] rgArgIds = new int[10]; Object[] rgArgValues = new Object[10]; int algid = Constants.CALG_3DES; // Check for bad values // 1) we don't support OFB mode in TripleDESCryptoServiceProvider if (mode == CipherMode.OFB) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported")); } // 2) we only support CFB with a feedback size of 8 bits if ((mode == CipherMode.CFB) && (feedbackSize != 8)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported")); } // Build the key if one does not already exist if (rgbKey == null) { rgbKey = new byte[KeySizeValue / 8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbKey); } // Set the mode for the encryptor (defaults to CBC) if (mode != CipherMode.CBC) { rgArgIds[cArgs] = Constants.KP_MODE; rgArgValues[cArgs] = mode; cArgs += 1; } // If not ECB mode -- pass in an IV if (mode != CipherMode.ECB) { if (rgbIV == null) { rgbIV = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbIV); } // // We truncate IV's that are longer than the block size to 8 bytes : this is // done to maintain backward compatibility with the behavior shipped in V1.x. // The call to set the IV in CryptoAPI will ignore any bytes after the first 8 // bytes. We'll still reject IV's that are shorter than the block size though. // if (rgbIV.Length < 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize")); } rgArgIds[cArgs] = Constants.KP_IV; rgArgValues[cArgs] = rgbIV; cArgs += 1; } // If doing OFB or CFB, then we need to set the feed back loop size if ((mode == CipherMode.OFB) || (mode == CipherMode.CFB)) { rgArgIds[cArgs] = Constants.KP_MODE_BITS; rgArgValues[cArgs] = feedbackSize; cArgs += 1; } // If the size of rgbKey is 16 bytes, then we're doing two-key 3DES, so switch algids // Note that we assume that if a CSP supports CALG_3DES then it also supports CALG_3DES_112 if (rgbKey.Length == 16) { algid = Constants.CALG_3DES_112; } // Create the encryptor object return(new CryptoAPITransform(algid, cArgs, rgArgIds, rgArgValues, rgbKey, PaddingValue, mode, BlockSizeValue, feedbackSize, false, encryptMode)); }
internal CryptoAPITransform(int algid, int cArgs, int[] rgArgIds, object[] rgArgValues, byte[] rgbKey, PaddingMode padding, CipherMode cipherChainingMode, int blockSize, int feedbackSize, bool useSalt, CryptoAPITransformMode encDecMode) { this.BlockSizeValue = blockSize; this.ModeValue = cipherChainingMode; this.PaddingValue = padding; this.encryptOrDecrypt = encDecMode; int[] array = new int[rgArgIds.Length]; Array.Copy(rgArgIds, array, rgArgIds.Length); this._rgbKey = new byte[rgbKey.Length]; Array.Copy(rgbKey, this._rgbKey, rgbKey.Length); object[] array2 = new object[rgArgValues.Length]; for (int i = 0; i < rgArgValues.Length; i++) { if (rgArgValues[i] is byte[]) { byte[] array3 = (byte[])rgArgValues[i]; byte[] array4 = new byte[array3.Length]; Array.Copy(array3, array4, array3.Length); array2[i] = array4; } else if (rgArgValues[i] is int) { array2[i] = (int)rgArgValues[i]; } else if (rgArgValues[i] is CipherMode) { array2[i] = (int)rgArgValues[i]; } } this._safeProvHandle = Utils.AcquireProvHandle(new CspParameters(24)); SafeKeyHandle invalidHandle = SafeKeyHandle.InvalidHandle; Utils._ImportBulkKey(this._safeProvHandle, algid, useSalt, this._rgbKey, ref invalidHandle); this._safeKeyHandle = invalidHandle; int j = 0; while (j < cArgs) { int num = rgArgIds[j]; int dwValue; switch (num) { case 1: { this.IVValue = (byte[])array2[j]; byte[] ivvalue = this.IVValue; Utils.SetKeyParamRgb(this._safeKeyHandle, array[j], ivvalue, ivvalue.Length); break; } case 2: case 3: goto IL_1D7; case 4: this.ModeValue = (CipherMode)array2[j]; dwValue = (int)array2[j]; goto IL_1AB; case 5: dwValue = (int)array2[j]; goto IL_1AB; default: if (num != 19) { goto IL_1D7; } dwValue = (int)array2[j]; goto IL_1AB; } IL_1EC: j++; continue; IL_1AB: Utils.SetKeyParamDw(this._safeKeyHandle, array[j], dwValue); goto IL_1EC; IL_1D7: throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeyParameter"), "_rgArgIds[i]"); } }
private ICryptoTransform _NewEncryptor(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int feedbackSize, CryptoAPITransformMode encryptMode) { int cArgs = 0; int[] rgArgIds = new int[10]; object[] rgArgValues = new object[10]; if (mode == CipherMode.OFB) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported")); } if (mode == CipherMode.CFB && feedbackSize != 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported")); } if (rgbKey == null) { rgbKey = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbKey); } if (mode != CipherMode.CBC) { rgArgIds[cArgs] = 4; rgArgValues[cArgs] = (object)mode; ++cArgs; } if (mode != CipherMode.ECB) { if (rgbIV == null) { rgbIV = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbIV); } if (rgbIV.Length < 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize")); } rgArgIds[cArgs] = 1; rgArgValues[cArgs] = (object)rgbIV; ++cArgs; } if (mode == CipherMode.OFB || mode == CipherMode.CFB) { rgArgIds[cArgs] = 5; rgArgValues[cArgs] = (object)feedbackSize; ++cArgs; } return((ICryptoTransform) new CryptoAPITransform(26113, cArgs, rgArgIds, rgArgValues, rgbKey, this.PaddingValue, mode, this.BlockSizeValue, feedbackSize, false, encryptMode)); }
// *********************** CONSTRUCTORS ************************* internal CryptoAPITransform(String strName, int algid, int cArgs, int[] rgArgIds, Object[] rgArgValues, byte[] rgbKey, CspParameters param, PaddingMode padding, CipherMode cipherChainingMode, int blockSize, int feedbackSize, CryptoAPITransformMode encDecMode) { int dwValue; int hr; int i; byte[] rgbValue; State = 0; NameValue = strName; BlockSizeValue = blockSize; FeedbackSizeValue = feedbackSize; ModeValue = cipherChainingMode; PaddingValue = padding; KeySizeValue = rgbKey.Length * 8; _algid = algid; encryptOrDecrypt = encDecMode; // Copy the input args _cArgs = cArgs; _rgArgIds = new int[rgArgIds.Length]; Array.Copy(rgArgIds, _rgArgIds, rgArgIds.Length); _rgbKey = new byte[rgbKey.Length]; Array.Copy(rgbKey, _rgbKey, rgbKey.Length); _rgArgValues = new Object[rgArgValues.Length]; // an element of rgArgValues can only be an int or a byte[] for (int j = 0; j < rgArgValues.Length; j++) { if (rgArgValues[j] is byte[]) { byte[] rgbOrig = (byte[])rgArgValues[j]; byte[] rgbNew = new byte[rgbOrig.Length]; Array.Copy(rgbOrig, rgbNew, rgbOrig.Length); _rgArgValues[j] = rgbNew; continue; } if (rgArgValues[j] is int) { _rgArgValues[j] = (int)rgArgValues[j]; continue; } if (rgArgValues[j] is CipherMode) { _rgArgValues[j] = (int)rgArgValues[j]; continue; } } _hCSP = IntPtr.Zero; //_hMasterKey = IntPtr.Zero; _hKey = IntPtr.Zero; // If we have no passed in CSP parameters, use the default ones if (param == null) { _parameters = new CspParameters(); } else { _parameters = param; } // // Try and open the CSP. // On downlevel crypto platforms, we have to create a key container because we can't // use the exponent-of-one trick on a CRYPT_VERIFYONLY keyset // see URT bug #15957 // if (_runningWin2KOrLaterCrypto) { hr = _AcquireCSP(_parameters, ref _hCSP); } else { hr = _CreateCSP(_parameters, ref _hCSP); } if ((hr != 0) || (_hCSP == IntPtr.Zero)) { #if _DEBUG if (debug) { Console.WriteLine("_CreateCSP failed in CSP_Encryptor, hr = {0:X} hCSP = {1:X}", hr, _hCSP); } #endif throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CouldNotAcquire")); } // Check to see if this alg & key size are supported // Commented out for now until I can fix the DES/3DES 56/64 problem /* { * int hasAlgHR; * Console.WriteLine("Keysizevalue = " + KeySizeValue); * hasAlgHR = _SearchForAlgorithm(_hCSP, algid, KeySizeValue); * if (hasAlgHR != 0) { * throw new CryptographicException(String.Format(Environment.GetResourceString("Cryptography_CSP_AlgKeySizeNotAvailable"),KeySizeValue)); * } * } */ #if _DEBUG if (debug) { Console.WriteLine("Got back _hCSP = {0}", _hCSP); } #endif #if _DEBUG if (debug) { Console.WriteLine("Calling _ImportBulkKey({0}, {1}, {2})", _hCSP, algid, rgbKey); } #endif _hKey = _ImportBulkKey(_hCSP, algid, _rgbKey); if (_hKey == IntPtr.Zero) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_ImportBulkKey")); } //hr = _DuplicateKey(_hMasterKey, ref _hKey); #if _DEBUG if (debug) { Console.WriteLine("Got back _hKey = {0}", _hKey); } #endif for (i = 0; i < cArgs; i++) { switch (rgArgIds[i]) { case 1: // KP_IV IVValue = (byte[])_rgArgValues[i]; rgbValue = IVValue; SetAsByteArray: _SetKeyParamRgb(_hKey, _rgArgIds[i], rgbValue); break; case 4: // KP_MODE ModeValue = (CipherMode)_rgArgValues[i]; dwValue = (Int32)_rgArgValues[i]; SetAsDWord: #if _DEBUG if (debug) { Console.WriteLine("Calling _SetKeyParamDw({0}, {1}, {2})", _hKey, _rgArgIds[i], dwValue); } #endif _SetKeyParamDw(_hKey, _rgArgIds[i], dwValue); break; case 5: // KP_MODE_BITS FeedbackSizeValue = (Int32)_rgArgValues[i]; dwValue = FeedbackSizeValue; goto SetAsDWord; case 19: // KP_EFFECTIVE_KEYLEN EffectiveKeySizeValue = (Int32)_rgArgValues[i]; dwValue = EffectiveKeySizeValue; goto SetAsDWord; default: throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeyParameter"), "_rgArgIds[i]"); } } _KeyHandleProtector = new __KeyHandleProtector(_hKey); }
internal CryptoAPITransform(int algid, int cArgs, int[] rgArgIds, object[] rgArgValues, byte[] rgbKey, PaddingMode padding, CipherMode cipherChainingMode, int blockSize, int feedbackSize, bool useSalt, CryptoAPITransformMode encDecMode) { this.BlockSizeValue = blockSize; this.ModeValue = cipherChainingMode; this.PaddingValue = padding; this.encryptOrDecrypt = encDecMode; int[] destinationArray = new int[rgArgIds.Length]; Array.Copy(rgArgIds, destinationArray, rgArgIds.Length); this._rgbKey = new byte[rgbKey.Length]; Array.Copy(rgbKey, this._rgbKey, rgbKey.Length); object[] objArray = new object[rgArgValues.Length]; for (int i = 0; i < rgArgValues.Length; i++) { if (rgArgValues[i] is byte[]) { byte[] sourceArray = (byte[])rgArgValues[i]; byte[] buffer3 = new byte[sourceArray.Length]; Array.Copy(sourceArray, buffer3, sourceArray.Length); objArray[i] = buffer3; } else if (rgArgValues[i] is int) { objArray[i] = (int)rgArgValues[i]; } else if (rgArgValues[i] is CipherMode) { objArray[i] = (int)rgArgValues[i]; } } this._safeProvHandle = Utils.AcquireProvHandle(new CspParameters(Utils.DefaultRsaProviderType)); SafeKeyHandle invalidHandle = SafeKeyHandle.InvalidHandle; Utils._ImportBulkKey(this._safeProvHandle, algid, useSalt, this._rgbKey, ref invalidHandle); this._safeKeyHandle = invalidHandle; for (int j = 0; j < cArgs; j++) { int num; switch (rgArgIds[j]) { case 1: { this.IVValue = (byte[])objArray[j]; byte[] iVValue = this.IVValue; Utils.SetKeyParamRgb(this._safeKeyHandle, destinationArray[j], iVValue, iVValue.Length); continue; } case 4: this.ModeValue = (CipherMode)objArray[j]; num = (int)objArray[j]; break; case 5: num = (int)objArray[j]; break; case 0x13: num = (int)objArray[j]; break; default: throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeyParameter"), "_rgArgIds[i]"); } Utils.SetKeyParamDw(this._safeKeyHandle, destinationArray[j], num); } }
internal CryptoAPITransform(int algid, int cArgs, int[] rgArgIds, object[] rgArgValues, byte[] rgbKey, PaddingMode padding, CipherMode cipherChainingMode, int blockSize, int feedbackSize, bool useSalt, CryptoAPITransformMode encDecMode) { this.BlockSizeValue = blockSize; this.ModeValue = cipherChainingMode; this.PaddingValue = padding; this.encryptOrDecrypt = encDecMode; int[] destinationArray = new int[rgArgIds.Length]; Array.Copy(rgArgIds, destinationArray, rgArgIds.Length); this._rgbKey = new byte[rgbKey.Length]; Array.Copy(rgbKey, this._rgbKey, rgbKey.Length); object[] objArray = new object[rgArgValues.Length]; for (int i = 0; i < rgArgValues.Length; i++) { if (rgArgValues[i] is byte[]) { byte[] sourceArray = (byte[]) rgArgValues[i]; byte[] buffer3 = new byte[sourceArray.Length]; Array.Copy(sourceArray, buffer3, sourceArray.Length); objArray[i] = buffer3; } else if (rgArgValues[i] is int) { objArray[i] = (int) rgArgValues[i]; } else if (rgArgValues[i] is CipherMode) { objArray[i] = (int) rgArgValues[i]; } } this._safeProvHandle = Utils.AcquireProvHandle(new CspParameters(Utils.DefaultRsaProviderType)); SafeKeyHandle invalidHandle = SafeKeyHandle.InvalidHandle; Utils._ImportBulkKey(this._safeProvHandle, algid, useSalt, this._rgbKey, ref invalidHandle); this._safeKeyHandle = invalidHandle; for (int j = 0; j < cArgs; j++) { int num; switch (rgArgIds[j]) { case 1: { this.IVValue = (byte[]) objArray[j]; byte[] iVValue = this.IVValue; Utils.SetKeyParamRgb(this._safeKeyHandle, destinationArray[j], iVValue, iVValue.Length); continue; } case 4: this.ModeValue = (CipherMode) objArray[j]; num = (int) objArray[j]; break; case 5: num = (int) objArray[j]; break; case 0x13: num = (int) objArray[j]; break; default: throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeyParameter"), "_rgArgIds[i]"); } Utils.SetKeyParamDw(this._safeKeyHandle, destinationArray[j], num); } }
private ICryptoTransform _NewEncryptor(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int effectiveKeySize, int feedbackSize, CryptoAPITransformMode encryptMode) { int index = 0; int[] rgArgIds = new int[10]; object[] rgArgValues = new object[10]; if (mode == CipherMode.OFB) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported")); } if (mode == CipherMode.CFB && feedbackSize != 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported")); } if (rgbKey == null) { rgbKey = new byte[this.KeySizeValue / 8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbKey); } int num = rgbKey.Length * 8; if (!this.ValidKeySize(num)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize")); } rgArgIds[index] = 19; rgArgValues[index] = this.EffectiveKeySizeValue != 0 ? (object)effectiveKeySize : (object)num; int cArgs = index + 1; if (mode != CipherMode.CBC) { rgArgIds[cArgs] = 4; rgArgValues[cArgs] = (object)mode; ++cArgs; } if (mode != CipherMode.ECB) { if (rgbIV == null) { rgbIV = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbIV); } if (rgbIV.Length < 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize")); } rgArgIds[cArgs] = 1; rgArgValues[cArgs] = (object)rgbIV; ++cArgs; } if (mode == CipherMode.OFB || mode == CipherMode.CFB) { rgArgIds[cArgs] = 5; rgArgValues[cArgs] = (object)feedbackSize; ++cArgs; } if (!Utils.HasAlgorithm(26114, num)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_AlgKeySizeNotAvailable", (object)num)); } return((ICryptoTransform) new CryptoAPITransform(26114, cArgs, rgArgIds, rgArgValues, rgbKey, this.PaddingValue, mode, this.BlockSizeValue, feedbackSize, this.m_use40bitSalt, encryptMode)); }
private ICryptoTransform _NewEncryptor(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int feedbackSize, CryptoAPITransformMode encryptMode) { int index = 0; int[] rgArgIds = new int[10]; object[] rgArgValues = new object[10]; if (mode == CipherMode.OFB) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported")); } if ((mode == CipherMode.CFB) && (feedbackSize != 8)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported")); } if (rgbKey == null) { rgbKey = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbKey); } if (mode != CipherMode.CBC) { rgArgIds[index] = 4; rgArgValues[index] = mode; index++; } if (mode != CipherMode.ECB) { if (rgbIV == null) { rgbIV = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbIV); } if (rgbIV.Length < 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize")); } rgArgIds[index] = 1; rgArgValues[index] = rgbIV; index++; } if ((mode == CipherMode.OFB) || (mode == CipherMode.CFB)) { rgArgIds[index] = 5; rgArgValues[index] = feedbackSize; index++; } return(new CryptoAPITransform(0x6601, index, rgArgIds, rgArgValues, rgbKey, base.PaddingValue, mode, base.BlockSizeValue, feedbackSize, false, encryptMode)); }
[System.Security.SecurityCritical] // auto-generated private ICryptoTransform _NewEncryptor(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int effectiveKeySize, int feedbackSize, CryptoAPITransformMode encryptMode) { int cArgs = 0; int[] rgArgIds = new int[10]; Object[] rgArgValues = new Object[10]; // Check for bad values // 1) we don't support OFB mode in RC2_CSP if (mode == CipherMode.OFB) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported")); } // 2) we only support CFB with a feedback size of 8 bits if ((mode == CipherMode.CFB) && (feedbackSize != 8)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported")); } if (rgbKey == null) { rgbKey = new byte[KeySizeValue / 8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbKey); } // Check the rgbKey size int keySizeValue = rgbKey.Length * 8; if (!ValidKeySize(keySizeValue)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize")); } // Deal with effective key length questions rgArgIds[cArgs] = Constants.KP_EFFECTIVE_KEYLEN; if (EffectiveKeySizeValue == 0) { rgArgValues[cArgs] = keySizeValue; } else { rgArgValues[cArgs] = effectiveKeySize; } cArgs += 1; // Set the mode for the encryptor (defaults to CBC) if (mode != CipherMode.CBC) { rgArgIds[cArgs] = Constants.KP_MODE; rgArgValues[cArgs] = mode; cArgs += 1; } // If not ECB mode -- pass in an IV if (mode != CipherMode.ECB) { if (rgbIV == null) { rgbIV = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbIV); } // // We truncate IV's that are longer than the block size to 8 bytes : this is // done to maintain backward compatibility with the behavior shipped in V1.x. // The call to set the IV in CryptoAPI will ignore any bytes after the first 8 // bytes. We'll still reject IV's that are shorter than the block size though. // if (rgbIV.Length < 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize")); } rgArgIds[cArgs] = Constants.KP_IV; rgArgValues[cArgs] = rgbIV; cArgs += 1; } // If doing OFB or CFB, then we need to set the feed back loop size if ((mode == CipherMode.OFB) || (mode == CipherMode.CFB)) { rgArgIds[cArgs] = Constants.KP_MODE_BITS; rgArgValues[cArgs] = feedbackSize; cArgs += 1; } if (!Utils.HasAlgorithm(Constants.CALG_RC2, keySizeValue)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_AlgKeySizeNotAvailable", keySizeValue)); } // Create the encryptor/decryptor object return(new CryptoAPITransform(Constants.CALG_RC2, cArgs, rgArgIds, rgArgValues, rgbKey, PaddingValue, mode, BlockSizeValue, feedbackSize, m_use40bitSalt, encryptMode)); }
private ICryptoTransform _NewEncryptor(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int feedbackSize, CryptoAPITransformMode encryptMode) { int num = 0; int[] array = new int[10]; object[] array2 = new object[10]; int algid = 26115; if (mode == CipherMode.OFB) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported")); } if (mode == CipherMode.CFB && feedbackSize != 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported")); } if (rgbKey == null) { rgbKey = new byte[this.KeySizeValue / 8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbKey); } if (mode != CipherMode.CBC) { array[num] = 4; array2[num] = mode; num++; } if (mode != CipherMode.ECB) { if (rgbIV == null) { rgbIV = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbIV); } if (rgbIV.Length < 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize")); } array[num] = 1; array2[num] = rgbIV; num++; } if (mode == CipherMode.OFB || mode == CipherMode.CFB) { array[num] = 5; array2[num] = feedbackSize; num++; } if (rgbKey.Length == 16) { algid = 26121; } return(new CryptoAPITransform(algid, num, array, array2, rgbKey, this.PaddingValue, mode, this.BlockSizeValue, feedbackSize, false, encryptMode)); }
[System.Security.SecurityCritical] // auto-generated internal CryptoAPITransform(int algid, int cArgs, int[] rgArgIds, Object[] rgArgValues, byte[] rgbKey, PaddingMode padding, CipherMode cipherChainingMode, int blockSize, int feedbackSize, bool useSalt, CryptoAPITransformMode encDecMode) { int dwValue; byte[] rgbValue; BlockSizeValue = blockSize; ModeValue = cipherChainingMode; PaddingValue = padding; encryptOrDecrypt = encDecMode; // Copy the input args int _cArgs = cArgs; int[] _rgArgIds = new int[rgArgIds.Length]; Array.Copy(rgArgIds, _rgArgIds, rgArgIds.Length); _rgbKey = new byte[rgbKey.Length]; Array.Copy(rgbKey, _rgbKey, rgbKey.Length); Object[] _rgArgValues = new Object[rgArgValues.Length]; // an element of rgArgValues can only be an int or a byte[] for (int j = 0; j < rgArgValues.Length; j++) { if (rgArgValues[j] is byte[]) { byte[] rgbOrig = (byte[]) rgArgValues[j]; byte[] rgbNew = new byte[rgbOrig.Length]; Array.Copy(rgbOrig, rgbNew, rgbOrig.Length); _rgArgValues[j] = rgbNew; continue; } if (rgArgValues[j] is int) { _rgArgValues[j] = (int) rgArgValues[j]; continue; } if (rgArgValues[j] is CipherMode) { _rgArgValues[j] = (int) rgArgValues[j]; continue; } } _safeProvHandle = Utils.AcquireProvHandle(new CspParameters(Utils.DefaultRsaProviderType)); SafeKeyHandle safeKeyHandle = SafeKeyHandle.InvalidHandle; // _ImportBulkKey will check for failures and throw an exception Utils._ImportBulkKey(_safeProvHandle, algid, useSalt, _rgbKey, ref safeKeyHandle); _safeKeyHandle = safeKeyHandle; for (int i=0; i<cArgs; i++) { switch (rgArgIds[i]) { case Constants.KP_IV: IVValue = (byte[]) _rgArgValues[i]; rgbValue = IVValue; Utils.SetKeyParamRgb(_safeKeyHandle, _rgArgIds[i], rgbValue, rgbValue.Length); break; case Constants.KP_MODE: ModeValue = (CipherMode) _rgArgValues[i]; dwValue = (Int32) _rgArgValues[i]; SetAsDWord: Utils.SetKeyParamDw(_safeKeyHandle, _rgArgIds[i], dwValue); break; case Constants.KP_MODE_BITS: dwValue = (Int32) _rgArgValues[i]; goto SetAsDWord; case Constants.KP_EFFECTIVE_KEYLEN: dwValue = (Int32) _rgArgValues[i]; goto SetAsDWord; default: throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeyParameter"), "_rgArgIds[i]"); } } }
private ICryptoTransform _NewEncryptor(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int effectiveKeySize, int feedbackSize, CryptoAPITransformMode encryptMode) { int num = 0; int[] array = new int[10]; object[] array2 = new object[10]; if (mode == CipherMode.OFB) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported")); } if (mode == CipherMode.CFB && feedbackSize != 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported")); } if (rgbKey == null) { rgbKey = new byte[this.KeySizeValue / 8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbKey); } int num2 = rgbKey.Length * 8; if (!base.ValidKeySize(num2)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize")); } array[num] = 19; if (this.EffectiveKeySizeValue == 0) { array2[num] = num2; } else { array2[num] = effectiveKeySize; } num++; if (mode != CipherMode.CBC) { array[num] = 4; array2[num] = mode; num++; } if (mode != CipherMode.ECB) { if (rgbIV == null) { rgbIV = new byte[8]; Utils.StaticRandomNumberGenerator.GetBytes(rgbIV); } if (rgbIV.Length < 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize")); } array[num] = 1; array2[num] = rgbIV; num++; } if (mode == CipherMode.OFB || mode == CipherMode.CFB) { array[num] = 5; array2[num] = feedbackSize; num++; } if (!Utils.HasAlgorithm(26114, num2)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_AlgKeySizeNotAvailable", new object[] { num2 })); } return(new CryptoAPITransform(26114, num, array, array2, rgbKey, this.PaddingValue, mode, this.BlockSizeValue, feedbackSize, this.m_use40bitSalt, encryptMode)); }