1- using System . Text ;
1+ using System ;
2+ using System . Text ;
23using Org . BouncyCastle . Crypto ;
34using Org . BouncyCastle . Crypto . Parameters ;
45using Org . BouncyCastle . Math ;
@@ -16,24 +17,45 @@ internal static class Sm2Util
1617 /// <summary>
1718 /// 使用公钥加密字符串数据
1819 /// </summary>
19- /// <param name="publicKeyString">十六进制格式的公钥字符串</param>
20- /// <param name="dataString">待加密的原文字符串</param>
21- /// <returns>加密后的密文字符串</returns>
20+ /// <param name="publicKeyString">十六进制格式的公钥字符串,不能为null或空字符串</param>
21+ /// <param name="dataString">待加密的原文字符串,支持null或空字符串</param>
22+ /// <returns>加密后的密文字符串,如果dataString为空则返回空字符串</returns>
23+ /// <exception cref="ArgumentNullException">当publicKeyString为null时抛出</exception>
24+ /// <exception cref="ArgumentException">当publicKeyString为空字符串时抛出</exception>
25+ /// <exception cref="FormatException">当publicKeyString不是有效的十六进制格式时抛出</exception>
2226 public static string Encrypt ( string publicKeyString , string dataString )
2327 {
28+ if ( publicKeyString == null )
29+ throw new ArgumentNullException ( nameof ( publicKeyString ) , "公钥字符串不能为null" ) ;
30+ if ( string . IsNullOrWhiteSpace ( publicKeyString ) )
31+ throw new ArgumentException ( "公钥字符串不能为空或仅包含空白字符" , nameof ( publicKeyString ) ) ;
32+
2433 var publicKey = Hex . Decode ( publicKeyString ) ;
25- var data = Encoding . UTF8 . GetBytes ( dataString ) ;
34+ var data = Encoding . UTF8 . GetBytes ( dataString ?? string . Empty ) ;
2635 return Encrypt ( publicKey , data ) ;
2736 }
2837
2938 /// <summary>
3039 /// 使用私钥解密字符串数据
3140 /// </summary>
32- /// <param name="privateKeyString">十六进制格式的私钥字符串</param>
33- /// <param name="encryptedDataString">待解密的密文字符串</param>
34- /// <returns>解密后的原文字符串</returns>
41+ /// <param name="privateKeyString">十六进制格式的私钥字符串,不能为null或空字符串</param>
42+ /// <param name="encryptedDataString">待解密的密文字符串,支持null或空字符串</param>
43+ /// <returns>解密后的原文字符串,如果encryptedDataString为空则返回空字符串</returns>
44+ /// <exception cref="ArgumentNullException">当privateKeyString为null时抛出</exception>
45+ /// <exception cref="ArgumentException">当privateKeyString为空字符串时抛出</exception>
46+ /// <exception cref="FormatException">当privateKeyString或encryptedDataString不是有效的十六进制格式时抛出</exception>
3547 public static string Decrypt ( string privateKeyString , string encryptedDataString )
3648 {
49+ if ( privateKeyString == null )
50+ throw new ArgumentNullException ( nameof ( privateKeyString ) , "私钥字符串不能为null" ) ;
51+ if ( string . IsNullOrWhiteSpace ( privateKeyString ) )
52+ throw new ArgumentException ( "私钥字符串不能为空或仅包含空白字符" , nameof ( privateKeyString ) ) ;
53+
54+ if ( encryptedDataString == string . Empty )
55+ {
56+ return string . Empty ;
57+ }
58+
3759 var privateKey = Hex . Decode ( privateKeyString ) ;
3860 var encryptedData = Hex . Decode ( encryptedDataString ) ;
3961 var deStr = Sm2Util . Decrypt ( privateKey , encryptedData ) ;
@@ -71,19 +93,27 @@ public static void GenerateKeyPair(out string publicKeyString, out string privat
7193 /// <summary>
7294 /// 使用公钥加密字节数组数据
7395 /// </summary>
74- /// <param name="publicKey">公钥字节数组</param>
75- /// <param name="data">待加密的原文字节数组</param>
76- /// <returns>加密后的密文字符串,包含C1、C2、C3三部分</returns>
96+ /// <param name="publicKey">公钥字节数组,不能为null或空数组</param>
97+ /// <param name="data">待加密的原文字节数组,支持null或空数组</param>
98+ /// <returns>加密后的密文字符串,包含C1、C2、C3三部分,如果data为空则返回空字符串</returns>
99+ /// <exception cref="ArgumentNullException">当publicKey为null时抛出</exception>
100+ /// <exception cref="ArgumentException">当publicKey为空数组时抛出</exception>
77101 public static string Encrypt ( byte [ ] publicKey , byte [ ] data )
78102 {
79- if ( null == publicKey || publicKey . Length == 0 )
103+ if ( publicKey == null )
104+ throw new ArgumentNullException ( nameof ( publicKey ) , "公钥字节数组不能为null" ) ;
105+ if ( publicKey . Length == 0 )
106+ throw new ArgumentException ( "公钥字节数组不能为空" , nameof ( publicKey ) ) ;
107+
108+ if ( data == null )
80109 {
81- return null ;
110+ data = new byte [ 0 ] ;
82111 }
83-
84- if ( data == null || data . Length == 0 )
112+
113+ // 特殊处理空数据
114+ if ( data . Length == 0 )
85115 {
86- return null ;
116+ return string . Empty ;
87117 }
88118
89119 byte [ ] source = new byte [ data . Length ] ;
@@ -110,19 +140,26 @@ public static string Encrypt(byte[] publicKey, byte[] data)
110140 /// <summary>
111141 /// 使用私钥解密字节数组数据
112142 /// </summary>
113- /// <param name="privateKey">私钥字节数组</param>
114- /// <param name="encryptedData">待解密的密文字节数组</param>
115- /// <returns>解密后的原文字节数组</returns>
143+ /// <param name="privateKey">私钥字节数组,不能为null或空数组</param>
144+ /// <param name="encryptedData">待解密的密文字节数组,支持null或空数组</param>
145+ /// <returns>解密后的原文字节数组,如果encryptedData为空则返回空数组</returns>
146+ /// <exception cref="ArgumentNullException">当privateKey为null时抛出</exception>
147+ /// <exception cref="ArgumentException">当privateKey为空数组时抛出</exception>
116148 public static byte [ ] Decrypt ( byte [ ] privateKey , byte [ ] encryptedData )
117149 {
118- if ( null == privateKey || privateKey . Length == 0 )
150+ if ( privateKey == null )
151+ throw new ArgumentNullException ( nameof ( privateKey ) , "私钥字节数组不能为null" ) ;
152+ if ( privateKey . Length == 0 )
153+ throw new ArgumentException ( "私钥字节数组不能为空" , nameof ( privateKey ) ) ;
154+
155+ if ( encryptedData == null )
119156 {
120- return null ;
157+ return new byte [ 0 ] ;
121158 }
122-
123- if ( encryptedData == null || encryptedData . Length == 0 )
159+
160+ if ( encryptedData . Length == 0 )
124161 {
125- return null ;
162+ return new byte [ 0 ] ;
126163 }
127164
128165 string data = Encoding . ASCII . GetString ( Hex . Encode ( encryptedData ) ) ;
0 commit comments