2013-11-28 34 views
0

我需要3數據庫連接字符串3個唯一的密碼加密的安全密碼,所以我砍死在一起,下面是一個WinForms應用程序的一部分:建立在.NET框架

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); 
void CharCount_TextChanged(object sender, System.EventArgs e) 
{ 
    int noChars; 
    if (int.TryParse(CharCount.Text, out noChars)) 
    { 
     byte[] random = new Byte[noChars-1]; 
     rng.GetBytes(random); 
     OutputTxt.Text = Convert.ToBase64String(random); 
    } 
} 

但是,如果我在進入8字符數量,我得到像「igTJEQptvQ ==」,這是12個字符,並且每個字符串似乎以「==」結尾

有人可以請解釋兩個爲什麼字符數大於字節數組,以及爲什麼字符串似乎總是以==結束。

請原諒業餘愛好者這個問題,並感謝您的解釋。

+0

您可以將隨機的一系列字節轉換爲64位以確保其「可讀」;但它不會轉換爲相同的字節數。您*可以*刪除「==」,因爲它只是一個終結符(當您調用FromBase64String時將其添加回去),但不會導致相同數量的「字節」。 –

回答

2

1)字節數組被訪問0爲基礎,但尺寸的基於1的,即要製作7字節數組不是8字節一個

2) Base64String被設計用於(東西等)發送字符串中的二進制數據在電子郵件中。它只使用'安全'字符子集來表示數據(64個字符= 2^6)。爲了表示7個字節(2 ^(7 * 8)= 2^56個比特),它需要56/6 = 10個字符,其實際上通過跟蹤=== s填充到4個字符的倍數(即12,16等)

見例如

0

http://en.wikipedia.org/wiki/Base64墊料部如果你正好8文本密碼(或其他字符)關於使用這樣的方法我產生相當隨機的用戶密碼寫​​一次後如何:

string GeneratePassword(int numUpper, int numLower, int numNum, int numSym) 
     {    
      char[] genPassword = new char[numUpper+numLower+numNum+numSym]; //array to store our password 
      int gPidx=0; // holds the index of where we are in the genPassword char array 

      Random rng = new Random(); // You could use your CryptoRNG here if you want 

      char[] upperChars = {'A','B','C','D','E','F','G', 
       'H','I','J','K','L','M','N','P','Q', 
       'R','S','T','U','V','W','X','Y','Z'}; // No 'O' as it is easily confused with '0' (but does slightly reduce the keyspace) 
      char[] lowerChars = {'a','b','c','d','e','f','g', 
       'h','i','j','k','m','n','o','p','q', 
       'r','s','t','y','v','w','x','y','z'}; // No 'l' as it is easily confused with '1' (but does slightly reduce the keyspace) 
      char[] numberChars = {'2','3','4','5','6','7','8','9'}; // No '1' or '0' as they are easily confused 
      char[] symbolChars = {'!','£','$','%','^','&', 
       '*','+','=','-','@','#','?'}; // Just the easy ones for a luser to find 

      //get uppers & put into the password array 
      for(int i=0; i<numUpper; i++) { 
       genPassword[gPidx] = upperChars[rng.Next(0,upperChars.Length)]; 
       gPidx++; 
      } 
      //get lowers & put into the password array 
      for(int i=0; i<numLower; i++) { 
       genPassword[gPidx] = lowerChars[rng.Next(0,lowerChars.Length)]; 
       gPidx++; 
      } 
      //get numbers & put into the password array 
      for(int i=0; i<numNum; i++) { 
       genPassword[gPidx] = numberChars[rng.Next(0,numberChars.Length)]; 
       gPidx++; 
      } 
      //get symbols & put into the password array 
      for(int i=0; i<numSym; i++) { 
       genPassword[gPidx] = symbolChars[rng.Next(0,symbolChars.Length)]; 
       gPidx++; 
      } 

      // Shuffle the letters (leave the numbers and symbols) 
        // I like passwords to start with a letter as some 
        // sites don't like non-alpha first chars 
      int endOfAlpha = genPassword.Length-numNum-numSym; 
      for(int i=0; i<endOfAlpha;i++) { 
       // For each character in our password 
       // pick a number between 0 and the end of the password 
       // swap the characters 
       char tempChar; 
       int random = rng.Next(0,endOfAlpha); //don't alter the first letter 
       tempChar = genPassword[i]; //store the current Value 
       genPassword[i] = genPassword[random]; 
       genPassword[random]=tempChar;    
      } 

      // Re-Shuffle leaving the first character intact 
      for(int i=1; i<genPassword.Length;i++) { 
       // For each character in our password 
       // pick a number between 0 and the end of the password 
       // swap the characters 
       char tempChar; 
       int random = rng.Next(1,genPassword.Length); //don't alter the first letter 
       tempChar = genPassword[i]; //store the current Value 
       genPassword[i] = genPassword[random]; 
       genPassword[random]=tempChar;    
      } 
      return new string(genPassword); 
     }