|   |  | 1 |  | // Copyright (c) Microsoft Corporation. All rights reserved. | 
|   |  | 2 |  | // Licensed under the MIT License. See License.txt in the project root for | 
|   |  | 3 |  | // license information. | 
|   |  | 4 |  |  | 
|   |  | 5 |  | using System; | 
|   |  | 6 |  | using System.Linq; | 
|   |  | 7 |  | using System.Security.Cryptography; | 
|   |  | 8 |  |  | 
|   |  | 9 |  | namespace Microsoft.Azure.KeyVault.Cryptography.Algorithms | 
|   |  | 10 |  | { | 
|   |  | 11 |  |     public class Rsa15 : RsaEncryption | 
|   |  | 12 |  |     { | 
|   |  | 13 |  |         public const string AlgorithmName = "RSA_15"; | 
|   |  | 14 |  |  | 
|   |  | 15 |  |         public Rsa15() | 
|   | 2 | 16 |  |             : base( AlgorithmName ) | 
|   |  | 17 |  |         { | 
|   | 2 | 18 |  |         } | 
|   |  | 19 |  |  | 
|   |  | 20 |  |         public override ICryptoTransform CreateEncryptor( AsymmetricAlgorithm key ) | 
|   |  | 21 |  |         { | 
|   | 4 | 22 |  |             RSA csp = key as RSA; | 
|   |  | 23 |  |  | 
|   | 4 | 24 |  |             if ( csp == null ) | 
|   | 0 | 25 |  |                 throw new ArgumentException( "key must be an instance of RSA", "key" ); | 
|   |  | 26 |  |  | 
|   | 4 | 27 |  |             return new Rsa15Encryptor( csp ); | 
|   |  | 28 |  |         } | 
|   |  | 29 |  |  | 
|   |  | 30 |  |         public override ICryptoTransform CreateDecryptor( AsymmetricAlgorithm key ) | 
|   |  | 31 |  |         { | 
|   | 4 | 32 |  |             RSA csp = key as RSA; | 
|   |  | 33 |  |  | 
|   | 4 | 34 |  |             if ( csp == null ) | 
|   | 0 | 35 |  |                 throw new ArgumentException( "key must be an instance of RSA", "key" ); | 
|   |  | 36 |  |  | 
|   | 4 | 37 |  |             return new Rsa15Decryptor( csp ); | 
|   |  | 38 |  |         } | 
|   |  | 39 |  |  | 
|   |  | 40 |  |         /// <summary> | 
|   |  | 41 |  |         /// RSA 15 Decryptor | 
|   |  | 42 |  |         /// </summary> | 
|   |  | 43 |  |         /// <remarks> | 
|   |  | 44 |  |         /// While this class has a reference to an IDisposable object, | 
|   |  | 45 |  |         /// it is not the owner of the object and will not Dispose it. | 
|   |  | 46 |  |         /// </remarks> | 
|   |  | 47 |  |         class Rsa15Decryptor : ICryptoTransform | 
|   |  | 48 |  |         { | 
|   |  | 49 |  |             private readonly RSA _csp; | 
|   |  | 50 |  |  | 
|   | 4 | 51 |  |             internal Rsa15Decryptor( RSA csp ) | 
|   |  | 52 |  |             { | 
|   | 4 | 53 |  |                 _csp = csp; | 
|   | 4 | 54 |  |             } | 
|   |  | 55 |  |  | 
|   |  | 56 |  |             public bool CanReuseTransform | 
|   |  | 57 |  |             { | 
|   | 0 | 58 |  |                 get { throw new NotImplementedException(); } | 
|   |  | 59 |  |             } | 
|   |  | 60 |  |  | 
|   |  | 61 |  |             public bool CanTransformMultipleBlocks | 
|   |  | 62 |  |             { | 
|   | 0 | 63 |  |                 get { throw new NotImplementedException(); } | 
|   |  | 64 |  |             } | 
|   |  | 65 |  |  | 
|   |  | 66 |  |             public int InputBlockSize | 
|   |  | 67 |  |             { | 
|   | 0 | 68 |  |                 get { throw new NotImplementedException(); } | 
|   |  | 69 |  |             } | 
|   |  | 70 |  |  | 
|   |  | 71 |  |             public int OutputBlockSize | 
|   |  | 72 |  |             { | 
|   | 0 | 73 |  |                 get { throw new NotImplementedException(); } | 
|   |  | 74 |  |             } | 
|   |  | 75 |  |  | 
|   |  | 76 |  |             public int TransformBlock( byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int out | 
|   |  | 77 |  |             { | 
|   | 0 | 78 |  |                 throw new NotImplementedException(); | 
|   |  | 79 |  |             } | 
|   |  | 80 |  |  | 
|   |  | 81 |  |             public byte[] TransformFinalBlock( byte[] inputBuffer, int inputOffset, int inputCount ) | 
|   |  | 82 |  |             { | 
|   | 4 | 83 |  |                 byte[] block = inputBuffer.Skip( inputOffset ).Take( inputCount ).ToArray(); | 
|   |  | 84 |  |  | 
|   |  | 85 |  | #if FullNetFx | 
|   |  | 86 |  |                 if ( _csp is RSACryptoServiceProvider ) | 
|   |  | 87 |  |                 { | 
|   |  | 88 |  |                     return ( ( RSACryptoServiceProvider )_csp ).Decrypt( block, false ); | 
|   |  | 89 |  |                 } | 
|   |  | 90 |  |  | 
|   |  | 91 |  |                 throw new CryptographicException( string.Format( "{0} is not supported", _csp.GetType().FullName ) ); | 
|   |  | 92 |  | #elif NETSTANDARD | 
|   | 4 | 93 |  |                 return _csp.Decrypt( block, RSAEncryptionPadding.Pkcs1 ); | 
|   |  | 94 |  | #else | 
|   |  | 95 |  |                 #error Unknown Framework | 
|   |  | 96 |  | #endif | 
|   |  | 97 |  |             } | 
|   |  | 98 |  |  | 
|   |  | 99 |  |             public void Dispose() | 
|   |  | 100 |  |             { | 
|   |  | 101 |  |                 // Intentionally empty | 
|   | 4 | 102 |  |             } | 
|   |  | 103 |  |         } | 
|   |  | 104 |  |  | 
|   |  | 105 |  |         /// <summary> | 
|   |  | 106 |  |         /// RSA 15 Encryptor | 
|   |  | 107 |  |         /// </summary> | 
|   |  | 108 |  |         /// <remarks> | 
|   |  | 109 |  |         /// While this class has a reference to an IDisposable object, | 
|   |  | 110 |  |         /// it is not the owner of the object and will not Dispose it. | 
|   |  | 111 |  |         /// </remarks> | 
|   |  | 112 |  |         class Rsa15Encryptor : ICryptoTransform | 
|   |  | 113 |  |         { | 
|   |  | 114 |  |             private readonly RSA _csp; | 
|   |  | 115 |  |  | 
|   | 4 | 116 |  |             internal Rsa15Encryptor( RSA csp ) | 
|   |  | 117 |  |             { | 
|   | 4 | 118 |  |                 _csp = csp; | 
|   | 4 | 119 |  |             } | 
|   |  | 120 |  |  | 
|   |  | 121 |  |             public bool CanReuseTransform | 
|   |  | 122 |  |             { | 
|   | 0 | 123 |  |                 get { throw new NotImplementedException(); } | 
|   |  | 124 |  |             } | 
|   |  | 125 |  |  | 
|   |  | 126 |  |             public bool CanTransformMultipleBlocks | 
|   |  | 127 |  |             { | 
|   | 0 | 128 |  |                 get { throw new NotImplementedException(); } | 
|   |  | 129 |  |             } | 
|   |  | 130 |  |  | 
|   |  | 131 |  |             public int InputBlockSize | 
|   |  | 132 |  |             { | 
|   | 0 | 133 |  |                 get { throw new NotImplementedException(); } | 
|   |  | 134 |  |             } | 
|   |  | 135 |  |  | 
|   |  | 136 |  |             public int OutputBlockSize | 
|   |  | 137 |  |             { | 
|   | 0 | 138 |  |                 get { throw new NotImplementedException(); } | 
|   |  | 139 |  |             } | 
|   |  | 140 |  |  | 
|   |  | 141 |  |             public int TransformBlock( byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int out | 
|   |  | 142 |  |             { | 
|   | 0 | 143 |  |                 throw new NotImplementedException(); | 
|   |  | 144 |  |             } | 
|   |  | 145 |  |  | 
|   |  | 146 |  |             public byte[] TransformFinalBlock( byte[] inputBuffer, int inputOffset, int inputCount ) | 
|   |  | 147 |  |             { | 
|   | 4 | 148 |  |                 byte[] block = inputBuffer.Skip( inputOffset ).Take( inputCount ).ToArray(); | 
|   |  | 149 |  |  | 
|   |  | 150 |  | #if FullNetFx | 
|   |  | 151 |  |                 if ( _csp is RSACryptoServiceProvider ) | 
|   |  | 152 |  |                 { | 
|   |  | 153 |  |                     return ( ( RSACryptoServiceProvider )_csp ).Encrypt( block, false ); | 
|   |  | 154 |  |                 } | 
|   |  | 155 |  |  | 
|   |  | 156 |  |                 throw new CryptographicException( string.Format( "{0} is not supported", _csp.GetType().FullName ) ); | 
|   |  | 157 |  | #elif NETSTANDARD | 
|   | 4 | 158 |  |                 return _csp.Encrypt( block, RSAEncryptionPadding.Pkcs1 ); | 
|   |  | 159 |  | #else | 
|   |  | 160 |  |                 #error Unknown Framework | 
|   |  | 161 |  | #endif | 
|   |  | 162 |  |             } | 
|   |  | 163 |  |  | 
|   |  | 164 |  |             public void Dispose() | 
|   |  | 165 |  |             { | 
|   |  | 166 |  |                 // Intentionally empty | 
|   | 4 | 167 |  |             } | 
|   |  | 168 |  |         } | 
|   |  | 169 |  |     } | 
|   |  | 170 |  | } |