Skip to content

Commit 91c0415

Browse files
authored
Merge pull request #9 from catcherwong/master
Fix RSA fails in Linux/OS X
2 parents 99e6e1e + d3badeb commit 91c0415

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/NETCore.Encrypt/EncryptProvider.cs

+11-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private static string GetRandomStr(int length)
3030
StringBuilder num = new StringBuilder();
3131

3232
Random rnd = new Random(DateTime.Now.Millisecond);
33-
for (int i = 0;i < length;i++)
33+
for (int i = 0; i < length; i++)
3434
{
3535
num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
3636
}
@@ -235,7 +235,7 @@ public static string AESDecrypt(string data, string key)
235235
{
236236
try
237237
{
238-
byte[] tmp = new byte[encryptedBytes.Length ];
238+
byte[] tmp = new byte[encryptedBytes.Length];
239239
int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length);
240240
byte[] ret = new byte[len];
241241
Array.Copy(tmp, 0, ret, 0, len);
@@ -361,7 +361,7 @@ public static string RSAEncrypt(string publicKey, string srcString)
361361
using (RSA rsa = RSA.Create())
362362
{
363363
rsa.FromJsonString(publicKey);
364-
byte[] encryptBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(srcString), RSAEncryptionPadding.OaepSHA512);
364+
byte[] encryptBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(srcString), RSAEncryptionPadding.Pkcs1);
365365
return encryptBytes.ToHexString();
366366
}
367367
}
@@ -380,7 +380,7 @@ public static string RSADecrypt(string privateKey, string srcString)
380380
{
381381
rsa.FromJsonString(privateKey);
382382
byte[] srcBytes = srcString.ToBytes();
383-
byte[] decryptBytes = rsa.Decrypt(srcBytes, RSAEncryptionPadding.OaepSHA512);
383+
byte[] decryptBytes = rsa.Decrypt(srcBytes, RSAEncryptionPadding.Pkcs1);
384384
return Encoding.UTF8.GetString(decryptBytes);
385385
}
386386
}
@@ -406,8 +406,10 @@ public static RSA RSAFromString(string rsaKey)
406406
/// <returns></returns>
407407
public static RSAKey CreateRsaKey(RsaSize rsaSize = RsaSize.R2048)
408408
{
409-
using (RSA rsa = RSA.Create((int) rsaSize))
409+
using (RSA rsa = RSA.Create())
410410
{
411+
rsa.KeySize = (int)RsaSize.R2048;
412+
411413
string publicKey = rsa.ToJsonString(false);
412414
string privateKey = rsa.ToJsonString(true);
413415

@@ -440,13 +442,13 @@ public static string Md5(string srcString, MD5Length length = MD5Length.L32)
440442
byte[] bytes_md5_out = md5.ComputeHash(bytes_md5_in);
441443

442444
str_md5_out = length == MD5Length.L32
443-
? BitConverter.ToString(bytes_md5_out)
445+
? BitConverter.ToString(bytes_md5_out)
444446
: BitConverter.ToString(bytes_md5_out, 4, 8);
445-
447+
446448
str_md5_out = str_md5_out.Replace("-", "");
447449
return str_md5_out;
448450
}
449-
}
451+
}
450452
#endregion
451453

452454
#region HMACMD5
@@ -730,7 +732,7 @@ private static string CreateMachineKey(int length)
730732
rng.GetBytes(random);
731733

732734
StringBuilder machineKey = new StringBuilder(length);
733-
for (int i = 0;i < random.Length;i++)
735+
for (int i = 0; i < random.Length; i++)
734736
{
735737
machineKey.Append(string.Format("{0:X2}", random[i]));
736738
}

src/NETCore.Encrypt/NETCore.Encrypt.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
66
<Company>Lvcc</Company>
77
<Product>Lvcc</Product>

0 commit comments

Comments
 (0)