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