@@ -219,27 +219,9 @@ public static String Encrypt(Object Value, String Thumbprint)
219
219
return null ;
220
220
}
221
221
else
222
- {
223
- X509Store CertStore = new X509Store ( StoreName . My , StoreLocation . CurrentUser ) ;
224
- try
225
- {
226
- CertStore . Open ( OpenFlags . ReadOnly ) ;
227
- }
228
- catch ( Exception ex )
229
- {
230
- throw new Exception ( "Error reading certificate store" , ex ) ;
231
- }
232
-
233
- var CertCollection = CertStore . Certificates ;
234
- var EncryptCert = CertCollection . Find ( X509FindType . FindByThumbprint , Thumbprint , false ) ;
235
- CertStore . Close ( ) ;
236
-
237
- if ( EncryptCert . Count == 0 )
238
- {
239
- throw new Exception ( "Certificate:" + Thumbprint + " does not exist in HKLM\\ Root" ) ;
240
- }
241
-
242
- RSACryptoServiceProvider rsaEncryptor = ( RSACryptoServiceProvider ) EncryptCert [ 0 ] . PublicKey . Key ;
222
+ {
223
+ X509Certificate2 EncryptCert = AutomationSelfSignedCertificate . GetCertificateWithThumbprint ( Thumbprint ) ;
224
+ RSACryptoServiceProvider rsaEncryptor = ( RSACryptoServiceProvider ) EncryptCert . PublicKey . Key ;
243
225
var valueJson = JsonConvert . SerializeObject ( Value ) ;
244
226
var EncryptedBytes = System . Text . Encoding . Default . GetBytes ( valueJson ) ;
245
227
byte [ ] EncryptedData = rsaEncryptor . Encrypt ( EncryptedBytes , true ) ;
@@ -258,28 +240,10 @@ public static Object Decrypt(Object EncryptedValue, String Thumbprint)
258
240
throw new Exception ( "Cannot decrypt value. Value to decrypt was not a string." ) ;
259
241
}
260
242
else
261
- {
262
- X509Store CertStore = new X509Store ( StoreName . My , StoreLocation . CurrentUser ) ;
263
- try
264
- {
265
- CertStore . Open ( OpenFlags . ReadOnly ) ;
266
- }
267
- catch ( Exception ex )
268
- {
269
- throw new Exception ( "Error reading certificate store" , ex ) ;
270
- }
271
-
272
- var CertCollection = CertStore . Certificates ;
273
- var EncryptCert = CertCollection . Find ( X509FindType . FindByThumbprint , Thumbprint , false ) ;
274
- CertStore . Close ( ) ;
275
-
276
- if ( EncryptCert . Count == 0 )
277
- {
278
- throw new Exception ( "Certificate:" + Thumbprint + " does not exist in HKLM\\ My" ) ;
279
- }
280
-
243
+ {
244
+ X509Certificate2 EncryptCert = AutomationSelfSignedCertificate . GetCertificateWithThumbprint ( Thumbprint ) ;
281
245
Byte [ ] EncryptedString = Convert . FromBase64String ( ( string ) EncryptedValue ) ;
282
- RSACryptoServiceProvider rsaEncryptor = ( RSACryptoServiceProvider ) EncryptCert [ 0 ] . PrivateKey ;
246
+ RSACryptoServiceProvider rsaEncryptor = ( RSACryptoServiceProvider ) EncryptCert . PrivateKey ;
283
247
byte [ ] EncryptedData = rsaEncryptor . Decrypt ( EncryptedString , true ) ;
284
248
var valueJson = System . Text . Encoding . Default . GetString ( EncryptedData ) ;
285
249
return JsonConvert . DeserializeObject ( valueJson ) ;
0 commit comments