一个完整的类用来生成RSACryptoServiceProvider单例类(.NET)详解编程语言

 1 internal class CcbRsaCryptProvider 
 2 { 
 3         private static RSACryptoServiceProvider _providerForSign; 
 4         private static RSACryptoServiceProvider _providerForVerifySign; 
 5         private static readonly object SignObj = new object(); 
 6         private static readonly object VerifySignObj = new object(); 
 7  
 8         internal static RSACryptoServiceProvider GetInstanceForSign(string privateKeyFile) 
 9         { 
10             if (null != _providerForSign) return _providerForSign; 
11             lock (SignObj) 
12             { 
13                 if (null != _providerForSign) return _providerForSign; 
14                 var provider = new RSACryptoServiceProvider(); 
15                 provider.FromXmlString(GetXmlContent(privateKeyFile)); 
16                 _providerForSign = provider; 
17             } 
18             return _providerForSign; 
19         } 
20  
21         internal static RSACryptoServiceProvider GetInstanceForVerifySign(string publicKeyFile) 
22         { 
23             if (null != _providerForVerifySign) return _providerForVerifySign; 
24             lock (VerifySignObj) 
25             { 
26                 if (null != _providerForVerifySign) return _providerForVerifySign; 
27                 var provider = new RSACryptoServiceProvider(); 
28                 provider.FromXmlString(GetXmlContent(publicKeyFile)); 
29                 _providerForVerifySign = provider; 
30             } 
31             return _providerForVerifySign; 
32         } 
33  
34         private static string GetXmlContent(string filePath) 
35         { 
36             return File.ReadAllText(filePath); 
37         } 
38  
39         private CcbRsaCryptProvider() 
40         { 
41         } 
42     }

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/15850.html

(0)
上一篇 2021年7月19日 18:49
下一篇 2021年7月19日 18:49

相关推荐

发表回复

登录后才能评论