Comments (3)
/// <summary>
/// Generate a random key
/// </summary>
/// <param name="n">key length,IV is 16,Key is 32</param>
/// <returns>return random value</returns>
private static string GetRandomStr(int length)
{
char[] arrChar = new char[]{
'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
};
StringBuilder num = new StringBuilder();
Random rnd = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < length; i++)
{
num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
}
return num.ToString();
}
This function is wrong. It is very simple to solve this problem. But before we will do this, let's take a look behind the random generator.
The Random Generator uses per default a seed as initial value for generating random numbers. The coder always use the same function:
Random rnd = new Random(DateTime.Now.Millisecond);
And this is the error. The CPU is so fast, that in 1 Millisecond this Generator will be initiated with the same Millisecond of time. This will result in the same random numbes. A quote from documenatation. Random()
The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. This problem can be avoided by using a single Random object to generate all random numbers. You can also work around it by modifying the seed value returned by the system clock and then explicitly providing this new seed value to the Random(Int32) constructor. For more information, see the Random(Int32) constructor.
To solve this security issue, generate a single Random Generator in the class.
public class EncryptProvider
{
#region Common
private static Random random;
/// <summary>
/// Generate a random key
/// </summary>
/// <param name="n">key length,IV is 16,Key is 32</param>
/// <returns>return random value</returns>
private static string GetRandomStr(int length)
{
char[] arrChar = new char[]{
'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
};
StringBuilder num = new StringBuilder();
if (random == null)
{
random = new Random();
}
for (int i = 0; i < length; i++)
{
num.Append(arrChar[random.Next(0, arrChar.Length)].ToString());
}
return num.ToString();
}
#endregion
from netcore.encrypt.
I have changed this part. And forked his code. May he will accept the pull request. 😉 If not, I think all others have to understand, that the NETCore.Encrypt is broken by default. (same nonce)
from netcore.encrypt.
Thanks , I have merged the pull request. @doowruc @Siox0911
from netcore.encrypt.
Related Issues (20)
- 希望增加RSA PKCS#1/8 的支持 HOT 1
- 不支持 AES 128 ? key 必须在此区间 "32"-"32". (Parameter 'key') HOT 1
- Encryprion and whitespace HOT 2
- Cannot figure out how to decrypt RSA :S HOT 1
- x509 certificate and RSA. HOT 2
- .NET Core 3.1 Version HOT 1
- 希望添加 Exponent和Modulus 加密方式的支持
- Exception messages need to be in english HOT 2
- Problems with AES HOT 2
- 命名和注释改进,异常建议用更适合的异常类,例如文件不存在用FileNotFoundException
- 咨询linux系统测试结果 HOT 1
- RSA/ECB/OAEPWithSHA-256AndMGF1Padding 这种方式是怎么个加解密,你这边支持嘛 HOT 1
- MD5 Decryption HOT 1
- EncryptProvider.RSAFromPublicPkcs 方法报错 HOT 5
- Project about contains misspelled word "decrpty" HOT 1
- Is NETCore.Encrypt in a Blazor Webassembly Client .Net 7.0 project working? HOT 1
- Encryption Key Chars HOT 1
- 使用RSA加密后,为何publicKey和privateKey多了个modules如何去掉?
- 请问会支持 sm系列加密解密验签等操作吗 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from netcore.encrypt.