การสร้างรหัสผ่านสุ่มใน C++
นี่คือโปรแกรม C++ สำหรับสร้างรหัสผ่าน N ชุด โดยแต่ละชุดมีความยาว M ตัวอักษร
อัลกอริทึม
Begin
รับความยาวรหัสผ่าน
ฟังก์ชัน permutation() สร้างรหัสผ่านสุ่ม:
พารามิเตอร์: อาเรย์ pointer a, จำนวนตัวเลขสุ่ม m, ความยาวรหัสผ่าน s
ถ้า (m == s) วนลูป i = 0 ถึง s-1 พิมพ์ *(a + i)
มิฉะนั้น วนลูป i = m ถึง s-1
tmp = a[m], a[m] = a[i], a[i] = tmp
เรียก permutation(a, m + 1, s)
tmp = a[m], a[m] = a[i], a[i] = tmp
End
ตัวอย่างโค้ด
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
void permutation(int *a, int m, int s) {
if (m == s) {
for (int i = 0; i < s; i++) cout << *(a + i);
cout << endl;
} else {
for (int i = m; i < s; i++) {
int tmp = a[m];
a[m] = a[i];
a[i] = tmp;
permutation(a, m + 1, s);
tmp = a[m];
a[m] = a[i];
a[i] = tmp;
}
}
}
int main(int argc, char **argv) {
cout << "Enter the length of the password: ";
int n; cin >> n;
int a[n];
for (int i = 0; i < n; i++) a[i] = rand() % 10;
cout << "Random Numbers are:" << endl;
for (int i = 0; i < n; i++) cout << a[i] << endl;
cout << "The Passwords are: " << endl;
permutation(a, 0, n);
}
ผลลัพธ์
Enter the length of the password: 4 Random Numbers are: 1 7 4 0 The Passwords are: 1740 1704 1470 1407 1047 1074 7140 7104 7410 7401 7041 7014 4710 4701 4170 4107 4017 4071 0741 0714 0471 0417 0147 0174