Skip to content

Commit 7e28b2f

Browse files
committed
2018.4.1.0
1 parent 0c4dce4 commit 7e28b2f

68 files changed

Lines changed: 2895 additions & 797 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

GodSharp.Encryption.sln

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{365F3D50-DBA
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{D94E875E-7D01-432C-B44A-95BC632D3880}"
99
ProjectSection(SolutionItems) = preProject
10+
appveyor.yml = appveyor.yml
1011
build\common.props = build\common.props
1112
build\version.props = build\version.props
1213
EndProjectSection
1314
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GodSharp.Encryption", "src\GodSharp.Encryption\GodSharp.Encryption.csproj", "{EA23E6BF-07FA-4BA1-902D-B507A83458BC}"
15+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GodSharp.Encryption", "src\GodSharp.Encryption\GodSharp.Encryption.csproj", "{EA23E6BF-07FA-4BA1-902D-B507A83458BC}"
1516
EndProject
16-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GodSharp.Encryption.Test", "test\GodSharp.Encryption.Test\GodSharp.Encryption.Test.csproj", "{A552D3A1-2CCD-4B4A-8EE1-E3E721CB0FF9}"
17+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GodSharp.Encryption.Test", "test\GodSharp.Encryption.Test\GodSharp.Encryption.Test.csproj", "{A552D3A1-2CCD-4B4A-8EE1-E3E721CB0FF9}"
1718
EndProject
1819
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{FF20D902-2537-4F14-88BE-7DDAC879E7F8}"
1920
EndProject
21+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "misc", "misc", "{D642BE82-3960-4D0E-BCAB-3D18F4C6181B}"
22+
ProjectSection(SolutionItems) = preProject
23+
.gitignore = .gitignore
24+
LICENSE = LICENSE
25+
README.md = README.md
26+
EndProjectSection
27+
EndProject
2028
Global
2129
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2230
Debug|Any CPU = Debug|Any CPU

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ Common encryption method for .NET.
4545

4646
- Decrypt()
4747

48+
generate openssl key
49+
50+
``` bash
51+
openssl
52+
genrsa -out openssl_rsa_pri_2048.pem 2048
53+
rsa -in openssl_rsa_pri_2048.pem -pubout -out openssl_rsa_pub_2048.pem
54+
```
55+
4856
# Base64
4957

5058
## Method

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 1.0.1.{build}
1+
version: 2018.4.1.{build}
22
branches:
33
only:
44
- master

build/version.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project>
22
<PropertyGroup Condition="'$(AssemblyName)'=='GodSharp.Encryption'">
33
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
4-
<Version>1.0.1</Version>
5-
<FileVersion>1.0.1</FileVersion>
4+
<Version>2018.4.1.0</Version>
5+
<FileVersion>2018.4.1</FileVersion>
66
<DocumentationFile>$(SolutionDir)\output\$(AssemblyName)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
77
</PropertyGroup>
88
</Project>

src/GodSharp.Encryption/Asymmetric/RSA.cs

Lines changed: 32 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,30 @@ namespace GodSharp.Encryption
99
/// <summary>
1010
/// Asymmetric/RSA encryption.
1111
/// </summary>
12-
public class RSA
12+
public static class RSA
1313
{
1414
/// <summary>
15-
/// The string format of public key.
15+
/// Gets or sets the open SSL.
1616
/// </summary>
17-
private static string publicKeyFormat = @"<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>";
18-
19-
/// <summary>
20-
/// The string format of private key.
21-
/// </summary>
22-
private static string privateKeyFormat = @"<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>";
17+
/// <value>
18+
/// The open SSL.
19+
/// </value>
20+
public static RSAOpenSsl OpenSsl => RSAOpenSslSingletonContainer.Instance;
2321

2422
#region Get public and private key of xml format.
2523
/// <summary>
2624
/// Get public and private key of xml format.
2725
/// </summary>
2826
/// <param name="xmlPrivateKey">The private key of xml format.</param>
2927
/// <param name="xmlPublicKey">The public key of xml format.</param>
30-
/// <param name="dwKeySize">The size of the key to use in bits</param>
31-
public static void CreateKey(out string xmlPrivateKey, out string xmlPublicKey, int dwKeySize = 1024)
28+
/// <param name="size">The size of the key to use in bits</param>
29+
public static void CreateXmlKey(out string xmlPrivateKey, out string xmlPublicKey, RSASizeTypes size = RSASizeTypes.L2048)
3230
{
33-
try
31+
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider((int)size))
3432
{
35-
xmlPrivateKey = null;
36-
xmlPublicKey = null;
37-
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(dwKeySize);
3833
xmlPrivateKey = rsa.ToXmlString(true);
3934
xmlPublicKey = rsa.ToXmlString(false);
4035
}
41-
catch ( Exception ex)
42-
{
43-
throw ex;
44-
}
4536
}
4637
#endregion
4738

@@ -54,14 +45,9 @@ public static void CreateKey(out string xmlPrivateKey, out string xmlPublicKey,
5445
/// <returns>String private key of xml format.</returns>
5546
public static string GetPrivateKey(string certFile, string password)
5647
{
57-
if (!File.Exists(certFile))
58-
{
59-
throw new FileNotFoundException(nameof(certFile));
60-
}
48+
X509Certificate2 cert = GetX509Certificate2(certFile);
6149

62-
X509Certificate2 cert = new X509Certificate2(certFile, password, X509KeyStorageFlags.Exportable);
63-
string privateKey = cert.PrivateKey.ToXmlString(true);
64-
return privateKey;
50+
return cert.PrivateKey.ToXmlString(true);
6551
}
6652

6753
/// <summary>
@@ -71,14 +57,22 @@ public static string GetPrivateKey(string certFile, string password)
7157
/// <returns>String public key of xml format.</returns>
7258
public static string GetPublicKey(string certFile)
7359
{
74-
if (!File.Exists(certFile))
75-
{
76-
throw new FileNotFoundException(nameof(certFile));
77-
}
60+
X509Certificate2 cert = GetX509Certificate2(certFile);
7861

79-
X509Certificate2 cert = new X509Certificate2(certFile);
80-
string publicKey = cert.PublicKey.Key.ToXmlString(false);
81-
return publicKey;
62+
return cert.PublicKey.Key.ToXmlString(false);
63+
}
64+
65+
/// <summary>
66+
/// Gets the X509 certificate2.
67+
/// </summary>
68+
/// <param name="certFile">The cert file.</param>
69+
/// <returns></returns>
70+
/// <exception cref="FileNotFoundException">certFile</exception>
71+
private static X509Certificate2 GetX509Certificate2(string certFile)
72+
{
73+
if (!File.Exists(certFile)) throw new FileNotFoundException(nameof(certFile));
74+
75+
return new X509Certificate2(certFile);
8276
}
8377
#endregion
8478

@@ -92,12 +86,9 @@ public static string GetPublicKey(string certFile)
9286
/// <returns>The encrypted data.</returns>
9387
public static string Encrypt(string data, string xmlPublicKey, Encoding encoding = null)
9488
{
95-
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
96-
{
97-
rsa.FromXmlString(xmlPublicKey);
98-
byte[] bytes = rsa.Encrypt(encoding.GetBytes(data), false);
99-
return Convert.ToBase64String(bytes);
100-
}
89+
if (encoding == null) encoding = Encoding.UTF8;
90+
91+
return Encrypt(encoding.GetBytes(data), xmlPublicKey);
10192
}
10293

10394
/// <summary>
@@ -125,13 +116,7 @@ public static string Encrypt(byte[] dataBytes, string xmlPublicKey)
125116
/// <returns>The decrypted data.</returns>
126117
public static string Decrypt(string data, string xmlPrivateKey, Encoding encoding = null)
127118
{
128-
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
129-
{
130-
byte[] dataBytes = Convert.FromBase64String(data);
131-
rsa.FromXmlString(xmlPrivateKey);
132-
byte[] bytes = rsa.Decrypt(dataBytes, false);
133-
return encoding.GetString(bytes);
134-
}
119+
return Decrypt(Convert.FromBase64String(data), xmlPrivateKey, encoding);
135120
}
136121

137122
/// <summary>
@@ -143,6 +128,8 @@ public static string Decrypt(string data, string xmlPrivateKey, Encoding encodin
143128
/// <returns>The decrypted data.</returns>
144129
public static string Decrypt(byte[] dataBytes, string xmlPrivateKey, Encoding encoding = null)
145130
{
131+
if (encoding == null) encoding = Encoding.UTF8;
132+
146133
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
147134
{
148135
rsa.FromXmlString(xmlPrivateKey);
@@ -151,102 +138,5 @@ public static string Decrypt(byte[] dataBytes, string xmlPrivateKey, Encoding en
151138
}
152139
}
153140
#endregion
154-
155-
#region Get hash sign
156-
/// <summary>
157-
/// Get hash sign.
158-
/// </summary>
159-
/// <param name="data"></param>
160-
/// <param name="hash"></param>
161-
/// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
162-
/// <returns></returns>
163-
public bool GetHash(string data, ref byte[] hash,Encoding encoding=null)
164-
{
165-
try
166-
{
167-
if (encoding==null)
168-
{
169-
encoding = Encoding.UTF8;
170-
}
171-
HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
172-
hash = MD5.ComputeHash(encoding.GetBytes(data));
173-
return true;
174-
}
175-
catch (Exception ex)
176-
{
177-
throw ex;
178-
}
179-
}
180-
181-
/// <summary>
182-
/// Get hash sign.
183-
/// </summary>
184-
/// <param name="data"></param>
185-
/// <param name="hash"></param>
186-
/// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
187-
/// <returns></returns>
188-
public bool GetHash(string data, ref string hash, Encoding encoding = null)
189-
{
190-
try
191-
{
192-
if (encoding == null)
193-
{
194-
encoding = Encoding.UTF8;
195-
}
196-
197-
HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
198-
byte[] buffer = MD5.ComputeHash(encoding.GetBytes(data));
199-
hash = Convert.ToBase64String(buffer);
200-
return true;
201-
}
202-
catch (Exception ex)
203-
{
204-
throw ex;
205-
}
206-
}
207-
208-
/// <summary>
209-
/// Get hash sign.
210-
/// </summary>
211-
/// <param name="fs"></param>
212-
/// <param name="hash"></param>
213-
/// <returns></returns>
214-
public bool GetHash(FileStream fs, ref byte[] hash)
215-
{
216-
try
217-
{
218-
HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
219-
hash = MD5.ComputeHash(fs);
220-
fs.Close();
221-
return true;
222-
}
223-
catch (Exception ex)
224-
{
225-
throw ex;
226-
}
227-
}
228-
229-
/// <summary>
230-
/// Get hash sign.
231-
/// </summary>
232-
/// <param name="fs"></param>
233-
/// <param name="hash"></param>
234-
/// <returns></returns>
235-
public bool GetHash(FileStream fs, ref string hash)
236-
{
237-
try
238-
{
239-
HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
240-
byte[] buffer = MD5.ComputeHash(fs);
241-
fs.Close();
242-
hash = Convert.ToBase64String(buffer);
243-
return true;
244-
}
245-
catch (Exception ex)
246-
{
247-
throw ex;
248-
}
249-
}
250-
#endregion
251141
}
252142
}

0 commit comments

Comments
 (0)