強密碼建議程式


每年5月7日,全球各組織都會提醒使用者強密碼的重要性。根據科技巨頭谷歌的資料;

  • 24% 的終端使用者使用“password”或“Qwerty”作為其帳戶密碼。

  • 而他們所有使用者中只有 34% 定期更改其帳戶密碼。

在當今這個科技發展迅速的世界裡,每次登入嘗試都可能遭到網路犯罪分子的攻擊。但如今仍有許多人繼續使用弱密碼來保護其工作和個人帳戶。

在這裡,我們將討論並檢查建立的密碼是極弱、弱、中等、強還是極強。為此,我們將根據演算法檢查密碼建立標準。在今天的文章中,我們將學習如何使用 C++ 程式碼建立強密碼建議程式。

如何建立強密碼 - 演算法

這是一個強密碼的一般演算法。它可以幫助開發人員在 C++ 環境中開發密碼建議系統並找到其密碼的特定標準。

  • 步驟 1 − 開始

  • 步驟 2 − 理想密碼的長度至少應為八個字元。

  • 步驟 3 − 必須包含至少一個數字字元。

  • 步驟 4 − 必須包含至少一個小寫字元。[例如:a、b、c.....、z]

  • 步驟 5 − 必須包含至少一個大寫字元。[例如:A、B、C......、Z]

  • 步驟 6 − 必須包含至少一個特殊字元。[例如:!@#$%^&*()-+]

  • 步驟 7 − 結束

建立強密碼建議程式 - 語法

switch (k) {
   case 1:
      if ((count_alphabet == 7) && (count_number == 0 || count_ALPHABET == 0 || count_ALPHABET == 7 || count_s_symbol == 0))
         break;
      key = getKey();
      password = password + alphabet[key];
      count_alphabet++;
      count++;
         break;

   case 2:            
      if ((count_ALPHABET == 7) && (count_number == 0 || count_alphabet == 0 || count_alphabet == 5 || count_s_symbol == 0))
            break;
         key = getKey();
         password = password + ALPHABET[key];
         count_ALPHABET++;
         count++;
            break;
   case 3:
      if ((count_number == 7) && (count_alphabet == 0 || count_alphabet == 1 || count_ALPHABET == 3 || count_ALPHABET == 0 || count_s_symbol == 0))
            break;
         key = getKey();           
         key = key % 10;          
         password = password + number[key];
         count_number++;
         count++;
            break;

   case 4:
      if ((count_s_symbol == 1) && (count_alphabet == 0 || count_alphabet == 1 || count_ALPHABET == 0 || count_ALPHABET == 1 || count_number == 0))
            break;
         key = getKey();
         key = key % 6;
         password = password + s_symbol[key];

         count_s_symbol++;
         count++;
            break;
   }
   cout << "\n-----------------------------\n";
   cout << "        Enter The Password             \n";
   cout << "------------------------------\n\n";
   cout << " " << password add here;
   cout << "\n\n Press any key continue \n";
   getchar();
}

在此語法中,條件檢查理想密碼的最低要求。我們將所有可能的條件分成四種情況,分別檢查字母、數字和特殊符號字元是否已根據最低要求滿足。然後檢查剩餘的其他字元,並結束。

方法 – 建立強密碼建議程式

  • 方法 1 − 建立強密碼建議程式環境

  • 方法 2 − 檢查密碼是強、弱還是中等

  • 方法 3 − 根據條件檢查密碼

建立強密碼建議程式環境

在此 C++ 構建程式碼中,系統將檢查輸入資料(密碼)的強度。它將掃描輸入以查詢所有提到的理想密碼標準。如果所有標準都匹配,則它是一個理想密碼。否則,如果輸入中缺少任何字元,它將返回一些隨機生成的密碼。

示例 1

#include <bits/stdc++.h>
using namespace std;
string add_more_char(string str, int need){
   int pos = 0;
   string low_case = "abcdefghijklmno";
   for (int a = 0; a < need; a++) {
      pos = rand() % str.length();
      str.insert(pos, 1, low_case[rand() % 26]);
   }
   return str;
}
string suggester(int m, int o, int a, int d, string str) {
   string num = "0123456789";
   string low_case = "abcdefghijklmno";
   string up_case = "ABCDEFGHIJKLMNO";
   string spl_char = "@#$_()!*&%#+-=.`";
   int pos = 0;
   if (m == 0) {
      pos = rand() % str.length();
      str.insert(pos, 1, low_case[rand() % 26]);
   }
   if (o == 0) {
      pos = rand() % str.length();
      str.insert(pos, 1, up_case[rand() % 26]);
   }
   if (a == 0) {
      pos = rand() % str.length();
      str.insert(pos, 1, num[rand() % 10]);
   }
   if (d == 0) {
      pos = rand() % str.length();
      str.insert(pos, 1, spl_char[rand() % 7]);
   }
   return str;
}
void generate_password(int n, string p){
   int l = 0, u = 0, d = 0, s = 0, need = 0;
   string suggest;
   for (int j = 0; j < n; j++) {
      if (p[j] >= 97 && p[j] <= 122)
         l = 1;
      else if (p[j] >= 65 && p[j] <= 90)
         u = 1;
      else if (p[j] >= 48 && p[j] <= 57)
         d = 1;
      else
         s = 1;
   }
   if ((l + u + d + s) == 4) {
      cout << "Hurra! Your Passcode Is Strong, Go Ahead!" << endl;
      return;
   }
   else
   cout << "Suggested password As Per The Input. Pick One For You! " << endl;
   for (int i = 0; i < 10; i++) {
      suggest = suggester(l, u, d, s, p);
      need = 8 - suggest.length();
      if (need > 0)
         suggest = add_more_char(suggest, need);
      cout << suggest << endl;
   }
}
int main(){
   string input_string = "abonirudra@1607";
   srand(time(NULL));
   generate_password(input_string.length(), input_string);
   return 0;
}

輸出

Suggested password As Per The Input. Pick One For You! 
abonirudGra@1607
abConirudra@1607
abonirudra@1E607
abonirudra@16A07
abonirudra@160L7
abNonirudra@1607
aNbonirudra@1607
abonirDudra@1607
aboniruFdra@1607
abonirNudra@1607

檢查密碼是強、弱還是中等

根據提到的演算法和 C++ 構建程式碼;編碼的字串檢查輸入強度。當所有條件都滿足時,整個方法返回“強”作為輸出。如果僅滿足前三個步驟且密碼長度至少為八個字元,則將返回“中等”。

示例 2

#include <bits/stdc++.h>
using namespace std;
void printStrongNess(string& input) {
   int n = input.length();
   bool hasLower = false, hasUpper = false;
   bool hasDigit = false, specialChar = false;
   string normalChars = "abcdefghijklmnopqrstu"
   "vwxyzABCDEFGHIJKL023456789 ";
   for (int i = 0; i < n; i++) {
      if (islower(input[i]))
         hasLower = true;
      if (isupper(input[i]))
         hasUpper = true;
      if (isdigit(input[i]))
         hasDigit = true;
      size_t special = input.find_first_not_of(normalChars);
      if (special != string::npos)
         specialChar = true;
   }
   cout << "Strength of password:-";
   if (hasLower && hasUpper && hasDigit &&
       specialChar && (n >= 8))
   cout << "Strong" << endl;
      else if ((hasLower || hasUpper) &&
   specialChar && (n >= 6))
       cout << "Moderate" << endl;
   else
      cout << "Weak" << endl;
}
int main(){
   string input = "Abonirudra@22052023";
   printStrongNess(input);
   return 0;
}

輸出

Strength of password:-Strong

根據條件檢查密碼

在此程式碼中,我們將對特定密碼執行驗證操作。以下是具體步驟:

  • 從使用者那裡獲取輸入。

  • 現在將檢查使用者輸入的密碼組合。

  • 密碼被分類為強、中等和弱。

  • 如果所有條件都為真,則它將是一個強密碼。

  • 否則,為中等或弱。

示例 3

#include <bits/stdc++.h>
using namespace std;
void checkpassword(string& password) {
   int n = password.length();
   bool hasLower = false, hasUpper = false, hasDigit = false;
   for (int i = 0; i < n; i++) {
      if (islower(password[i]))
         hasLower = true;
      if (isupper(password[i]))
         hasUpper = true;
      if (isdigit(password[i]))
          hasDigit = true;
   }
   cout << "Strength of password you have entered as per the input :-";
   if ( hasUpper && hasDigit && hasLower && (n >= 6))
      cout << "Strong" << endl;
   else if ((hasLower || hasUpper) && hasDigit && (n >=6))
      cout << "Moderate" << endl;
   else
      cout << "Weak" << endl;
}
int main() {
   cout << "Welcome To The Password Tester! Let's Check Your Password. " << endl;
   cout << "Let's consider the average length of an ideal strong password should be more than 6 character " << endl;
   cout << "Please enter your desired password with :- " << endl;
   cout << " * at least one upper case letter and lower case letter " << endl;
   cout << " * and also need to have at least one digit " << endl; string password;
   cout <<"Enter your monermoto password"<<endl;
   getline(cin,password);
   checkpassword(password);
   return 0;
}

輸出

Welcome To The Password Tester! Let's Check Your Password. 
Let's consider the average length of an ideal strong password should be more than 6 character 
Please enter your desired password with :- 
 * at least one upper case letter and lower case letter 
 * and also need to have at least one digit 
Enter your monermoto password
Strength of password you have entered as per the input :-Weak

結論

從本文中,我們瞭解瞭如何使用 C++ 建立密碼建議程式環境,然後如何在該特定平臺上使用一些標籤(強、中等、弱)來評估密碼質量。

更新於: 2023 年 4 月 5 日

636 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.