Giter Club home page Giter Club logo

Comments (3)

Siox0911 avatar Siox0911 commented on May 18, 2024
        /// <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.

Siox0911 avatar Siox0911 commented on May 18, 2024

EncryptProvider.zip

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.

myloveCc avatar myloveCc commented on May 18, 2024

Thanks , I have merged the pull request. @doowruc @Siox0911

from netcore.encrypt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.