1 using System.Diagnostics.Contracts;
4 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // <OWNER>[....]</OWNER>
10 namespace System.Security.Cryptography {
11 [System.Runtime.InteropServices.ComVisible(true)]
12 public class RSAPKCS1KeyExchangeDeformatter : AsymmetricKeyExchangeDeformatter {
14 bool? _rsaOverridesDecrypt;
15 RandomNumberGenerator RngValue;
19 public RSAPKCS1KeyExchangeDeformatter() {}
21 public RSAPKCS1KeyExchangeDeformatter(AsymmetricAlgorithm key) {
23 throw new ArgumentNullException("key");
24 Contract.EndContractBlock();
32 public RandomNumberGenerator RNG {
33 get { return RngValue; }
34 set { RngValue = value; }
37 public override String Parameters {
46 public override byte[] DecryptKeyExchange(byte[] rgbIn) {
48 throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_MissingKey"));
51 if (OverridesDecrypt) {
52 rgbOut = _rsaKey.Decrypt(rgbIn, RSAEncryptionPadding.Pkcs1);
57 rgb = _rsaKey.DecryptValue(rgbIn);
60 // Expected format is:
61 // 00 || 02 || PS || 00 || D
62 // where PS does not contain any zeros.
65 for (i = 2; i<rgb.Length; i++) {
72 throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_PKCS1Decoding"));
74 i++; // Skip over the zero
76 rgbOut = new byte[rgb.Length - i];
77 Buffer.InternalBlockCopy(rgb, i, rgbOut, 0, rgbOut.Length);
82 public override void SetKey(AsymmetricAlgorithm key) {
84 throw new ArgumentNullException("key");
85 Contract.EndContractBlock();
87 _rsaOverridesDecrypt = default(bool?);
90 private bool OverridesDecrypt {
92 if (!_rsaOverridesDecrypt.HasValue) {
93 _rsaOverridesDecrypt = Utils.DoesRsaKeyOverride(_rsaKey, "Decrypt", new Type[] { typeof(byte[]), typeof(RSAEncryptionPadding) });
95 return _rsaOverridesDecrypt.Value;