Προς το περιεχόμενο

Key genarator


NiKoS WSN

Προτεινόμενες αναρτήσεις

Δημοσ.

Υπάρχει εύκολος τρόπος για να δημιουργήσω ένα προγραμματάκι για προσωπική χρήση το οποίο να μου δημιουργεί διάφορους κωδικούς;

 

Δεν θέλω κάτι πολύπλοκο:

 

απλά να δημιουργεί 10ψήφιο κωδικό από νούμερα και γράμματα

 

Επειδή δεν ασχολούμαι με προγραμματισμό έχετε κάποιο έτοιμο κώδικα η καμία παραπομπή; Αν δεν έχετε πείτε μου έναν βασικό κώδικα αν υπάρχει και θα πειραματιστώ (δεν είμαι τελείως εκτός).

Δημοσ.
>sub randomPassword {
my $password;
my $_rand;

my $password_length = 10;

my @chars = split(" ",
   "a b c d e f g h i j k l m n o
   p q r s t u v w x y z - _ % # |
   0 1 2 3 4 5 6 7 8 9");

srand;

for (my $i=0; $i <= $password_length ;$i++) {
   $_rand = int(rand 41);
   $password .= $chars[$_rand];
}
return $password;
}

Δημοσ.

>sub randomPassword {
my $password;
my $_rand;

my $password_length = 10;

my @chars = split(" ",
   "a b c d e f g h i j k l m n o
   p q r s t u v w x y z - _ % # |
   0 1 2 3 4 5 6 7 8 9");

srand;

for (my $i=0; $i <= $password_length ;$i++) {
   $_rand = int(rand 41);
   $password .= $chars[$_rand];
}
return $password;
}

 

και σε τι μορφή;

 

edit: να υποθέσω πως τρέχει με cmd; για νέο κωδικό πρέπει να το ξ ανατρέξω ή έχουμε κουμπάκι;

Δημοσ.

Αν και το ζητούμενο καλύφθηκε, παρόλα αυτά να μια ακόμη μικρή (απλή και ενδεχομένως όχι ιδιαίτερα ασφαλής) υλοποίηση σε C++:

>
// Very simple and not so secure password generator, xdir.
#include <cstdlib.h>
#include <iostream>
#include <iomanip>
#include <string>

#define MAX_LEN 10
#define LOOP_BY 15

using namespace std;

int main(void)
{
string strPswrd;
int Loop = LOOP_BY;

// Rand randomization.
srand(time(NULL));

// Generate up to LOOP_BY passwords.
while(Loop--)
{
	// Build a very random string..
	for(int MaxLen = MAX_LEN; MaxLen > 0; MaxLen--)
		strPswrd += (char)(32 + (rand() % 95)); // 127 - 32 = 95 [7BIT ASCII]..
	// Print very random string on STDOUT
	cout <<setw(2)<<setfill('0')<< LOOP_BY - Loop<<")"<<strPswrd<<endl;
	// Reset variables and continue until LOOP_BY is exhausted!
	strPswrd = "";
}

return 0;
}

ΕΞΟΔΟΣ:

>
01)-1Nc]ti#T}
02)9fg[,;)O)~
03)Jg7DyK_zi~
04)H\UWlP{.K,
05)rB9qViUi't
06)T;Y_ymQ9:B
07):7xN6|!p~<
08)Uoo&&'G,R'
09)@tK2f%>4H}
10) '0epKM@dB
11)CYxxqk]vsX
12)w[A.~u5!D%
13)~n0YLJ{u)q
14)p{EhyB%{K1
15)Q&PG1lc HO

Δημοσ.

Αν και το ζητούμενο καλύφθηκε, παρόλα αυτά να μια ακόμη μικρή (απλή και ενδεχομένως όχι ιδιαίτερα ασφαλής) υλοποίηση σε C++:

>
// Very simple and not so secure password generator, xdir.
#include <cstdlib.h>
#include <iostream>
#include <iomanip>
#include <string>

#define MAX_LEN 10
#define LOOP_BY 15

using namespace std;

int main(void)
{
string strPswrd;
int Loop = LOOP_BY;

// Rand randomization.
srand(time(NULL));

// Generate up to LOOP_BY passwords.
while(Loop--)
{
	// Build a very random string..
	for(int MaxLen = MAX_LEN; MaxLen > 0; MaxLen--)
		strPswrd += (char)(32 + (rand() % 95)); // 127 - 32 = 95 [7BIT ASCII]..
	// Print very random string on STDOUT
	cout <<setw(2)<<setfill('0')<< LOOP_BY - Loop<<")"<<strPswrd<<endl;
	// Reset variables and continue until LOOP_BY is exhausted!
	strPswrd = "";
}

return 0;
}

ΕΞΟΔΟΣ:

>
01)-1Nc]ti#T}
02)9fg[,;)O)~
03)Jg7DyK_zi~
04)H\UWlP{.K,
05)rB9qViUi't
06)T;Y_ymQ9:B
07):7xN6|!p~<
08)Uoo&&'G,R'
09)@tK2f%>4H}
10) '0epKM@dB
11)CYxxqk]vsX
12)w[A.~u5!D%
13)~n0YLJ{u)q
14)p{EhyB%{K1
15)Q&PG1lc HO

 

Επειδή προτιμώ τέτοιο τρόπο θέλω να μου εξηγήσεις τι εννοείς όχι ασφαλή τρόπο;

 

 

edit: άκυρο βρήκα τρόπο

Δημοσ.

Επειδή προτιμώ τέτοιο τρόπο θέλω να μου εξηγήσεις τι εννοείς όχι ασφαλή τρόπο;

 

 

edit: άκυρο βρήκα τρόπο

Καταρχήν (για την ιστορία αφού το έλυσες το ζήτημα) το σύστημα χρησιμοποιεί την βιβλιοθήκη τυχαίων αριθμών που παρέχει η C πράγμα που δεν θεωρείται ιδιαίτερα ασφαλές καθώς η τυχαιότητα που μας εξασφαλίζει είναι χαμηλή.

Ύστερα το πρόγραμμα δεν ελέγχει αν ένας κωδικός έχει ήδη χρησιμοποιηθεί οπότε δεν υπάρχει εγγύηση μοναδικότητας (καθίσταται εμφανέστερο όσο αυξάνουμε τον αριθμό των passwords που ζητάμε ενώ ταυτόχρονα κρατάμε το μήκος σταθερό).

Επίσης δεν υπάρχει εγγύηση ότι το κείμενο μας θα αποτελείται πάντα από τουλάχιστον ένα μικρό, έναν κεφαλαίο, έναν αριθμητικό και έναν ειδικό χαρακτήρα.

 

Και μερικά ακόμη (επαναλήψεις χαρακτήρων κλπ).

 

Ακολουθεί ένα τροποποιημένο πρόγραμμα που δοκιμάζει να αντιμετωπίσει το δεύτερο & τρίτο ζήτημα (από ενδιαφέρον, έχω να γράψω καιρό σε C++ :D):

>
// Very simple and not so secure password generator, xdir (v2).
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
#include <stdexcept>
#include <algorithm>

#define MAX_LEN 10
#define LOOP_BY 15

using namespace std;

vector<int> _apply_rnd_chr(string &In, const int chrFrom, const int chrTo,
vector<int> ExceptP);

int main(void)
{
string strPswrd;
int Loop = LOOP_BY;

cout << "Unique Str (c) xdir."<<endl<<endl;
// Rand randomization.
srand(time(NULL));
// rb-tree container (for speeding up duplicates searching)
map<string, bool> Unique;
// container to keep track of used strPswrd possition during _apply_rnd_chr
vector<int> exceptP;

// Generate up to LOOP_BY passwords.
while(Loop--)
{
	for( ;; )
	{
		// Build a very random string..
		for(int MaxLen = MAX_LEN; MaxLen > 0; MaxLen--)
			strPswrd += (char)(32 + (rand() % 95)); // 127 - 32 = 95 [7BIT ASCII]..
		// Assert strPswrd to contain nums, special chrs (lim.set), Caps, Small chrs.
		try
		{
               exceptP.clear();
			_apply_rnd_chr(strPswrd, 65, 91,
				_apply_rnd_chr(strPswrd, 97, 123,
					_apply_rnd_chr(strPswrd, 32, 48,
						_apply_rnd_chr(strPswrd, 48, 58, exceptP))));
		}
		catch(out_of_range oor)
		{
			cerr << "Out of range!"<<endl;
			return 1;
		}
		// Assert strPswrd to be unique..
		if(Unique.find(strPswrd) == Unique.end())
			break;

		strPswrd = "";
	}
	Unique[strPswrd] = true;

	// Reset variables and continue until LOOP_BY is exhausted!
	strPswrd = "";
}

// Print unique random strings on STDOUT
for(map<string, bool>::iterator itS = Unique.begin();
	itS != Unique.end(); itS++)
		cout <<setw(2)<<setfill('0')<< ++Loop+1<<")"<<itS->first<<endl;

return 0;
}

vector<int> _apply_rnd_chr(string &In, const int chrFrom, const int chrTo,
vector<int> ExceptP)
{
/*
 * Place one random character in range chrFrom-chrTo at a random In position
 * while position is not in ExceptP.
 */
if(chrFrom < 0 || chrFrom > 0xFF)
	throw out_of_range("");;

int P;
do {
	P = rand() % In.length();
}while(find(ExceptP.begin(), ExceptP.end(), P) != ExceptP.end());
In.at(P) = chrFrom + (rand() % (chrTo - chrFrom));

ExceptP.push_back(P);
return ExceptP;
}

ΕΞΟΔΟΣ:

>
01)+.7MT27i?W
02)/GL^i5ln!
03)7Zbo,Ru/q^
04)9VBz#aKhv1
05)>a}1wsS32!
06)@ZKAJ+_5pp
07)MvV5MnD!?R
08)O4\U[u"Gut
09)O6s.lptQsg
10)Xu= i6gr:J
11)_).X({a<k6
12)a)i,&0SaPx
13)aQ\"kz7 =S
14)d1.2&Dxr7!
15)ocf'`S.}A1

 

Υ.Γ.

Το πρόγραμμα έχει δοκιμασθεί σε C++ Builder 2010 και μπορεί να περιέχει Bugs ή άλλες αβλεψίες (αλλά σκοπός δεν είναι εδώ να γραφθεί ένας πλήρης Password Generator έτσι και αλλιώς)!

Δημοσ.

Καταρχήν (για την ιστορία αφού το έλυσες το ζήτημα) το σύστημα χρησιμοποιεί την βιβλιοθήκη τυχαίων αριθμών που παρέχει η C πράγμα που δεν θεωρείται ιδιαίτερα ασφαλές καθώς η τυχαιότητα που μας εξασφαλίζει είναι χαμηλή.

Ύστερα το πρόγραμμα δεν ελέγχει αν ένας κωδικός έχει ήδη χρησιμοποιηθεί οπότε δεν υπάρχει εγγύηση μοναδικότητας (καθίσταται εμφανέστερο όσο αυξάνουμε τον αριθμό των passwords που ζητάμε ενώ ταυτόχρονα κρατάμε το μήκος σταθερό).

Επίσης δεν υπάρχει εγγύηση ότι το κείμενο μας θα αποτελείται πάντα από τουλάχιστον ένα μικρό, έναν κεφαλαίο, έναν αριθμητικό και έναν ειδικό χαρακτήρα.

 

Και μερικά ακόμη (επαναλήψεις χαρακτήρων κλπ).

 

Ακολουθεί ένα τροποποιημένο πρόγραμμα που δοκιμάζει να αντιμετωπίσει το δεύτερο & τρίτο ζήτημα (από ενδιαφέρον, έχω να γράψω καιρό σε C++ :D):

>
// Very simple and not so secure password generator, xdir (v2).
#include <cstdlib.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
#include <stdexcept>
#include <algorithm>

#define MAX_LEN 10
#define LOOP_BY 15

using namespace std;

vector<int> _apply_rnd_chr(string &In, const int chrFrom, const int chrTo,
vector<int> ExceptP);

int main(void)
{
string strPswrd;
int Loop = LOOP_BY;

cout << "Unique Str (c) xdir."<<endl<<endl;
// Rand randomization.
srand(time(NULL));
// rb-tree container (for speeding up duplicates searching)
map<string, bool> Unique;
// container to keep track of used strPswrd possition during _apply_rnd_chr
vector<int> exceptP;

// Generate up to LOOP_BY passwords.
while(Loop--)
{
	for( ;; )
	{
		// Build a very random string..
		for(int MaxLen = MAX_LEN; MaxLen > 0; MaxLen--)
			strPswrd += (char)(32 + (rand() % 95)); // 127 - 32 = 95 [7BIT ASCII]..
		// Assert strPswrd to contain nums, special chrs (lim.set), Caps, Small chrs.
		try
		{
               exceptP.clear();
			_apply_rnd_chr(strPswrd, 65, 91,
				_apply_rnd_chr(strPswrd, 97, 123,
					_apply_rnd_chr(strPswrd, 32, 48,
						_apply_rnd_chr(strPswrd, 48, 58, exceptP))));
		}
		catch(out_of_range oor)
		{
			cerr << "Out of range!"<<endl;
			return 1;
		}
		// Assert strPswrd to be unique..
		if(Unique.find(strPswrd) == Unique.end())
			break;

		strPswrd = "";
	}
	Unique[strPswrd] = true;

	// Reset variables and continue until LOOP_BY is exhausted!
	strPswrd = "";
}

// Print unique random strings on STDOUT
for(map<string, bool>::iterator itS = Unique.begin();
	itS != Unique.end(); itS++)
		cout <<setw(2)<<setfill('0')<< ++Loop+1<<")"<<itS->first<<endl;

return 0;
}

vector<int> _apply_rnd_chr(string &In, const int chrFrom, const int chrTo,
vector<int> ExceptP)
{
/*
 * Place one random character in range chrFrom-chrTo at a random In position
 * while position is not in ExceptP.
 */
if(chrFrom < 0 || chrFrom > 0xFF)
	throw out_of_range("");;

int P;
do {
	P = rand() % In.length();
}while(find(ExceptP.begin(), ExceptP.end(), P) != ExceptP.end());
In.at(P) = chrFrom + (rand() % (chrTo - chrFrom));

ExceptP.push_back(P);
return ExceptP;
}

ΕΞΟΔΟΣ:

>
01)+.7MT27i?W
02)/GL^i5ln!
03)7Zbo,Ru/q^
04)9VBz#aKhv1
05)>a}1wsS32!
06)@ZKAJ+_5pp
07)MvV5MnD!?R
08)O4\U[u"Gut
09)O6s.lptQsg
10)Xu= i6gr:J
11)_).X({a<k6
12)a)i,&0SaPx
13)aQ\"kz7 =S
14)d1.2&Dxr7!
15)ocf'`S.}A1

 

Υ.Γ.

Το πρόγραμμα έχει δοκιμασθεί σε C++ Builder 2010 και μπορεί να περιέχει Bugs ή άλλες αβλεψίες (αλλά σκοπός δεν είναι εδώ να γραφθεί ένας πλήρης Password Generator έτσι και αλλιώς)!

 

Ευχαριστώ πολύ. Το μόνο λάθος που βρήκα είναι αυτό #include <cstdlib.h> πρέπει να είναι #include <cstdlib>, αλλά όπως είπα και πριν βρήκα τρόπο!

Δημοσ.

Ναι, ο C++ Builder (το εργαλείο επιλογής μου εδώ και χρόνια για C/C++) ήταν πάντα χαλαρός σε διάφορα θέματα (μεταξύ των οποίων και οι ονομασίες των βιβλιοθηκών -συγκεκριμένα το <cstdlib> ισούται με <cstdlib.h> που οδηγεί στο <stdlib.h>) οπότε κάποια πράματα που περνάν σε αυτόν, δεν περνάν κατ' ανάγκη σε άλλους μεταφραστές εξ' ου και το κλασσικό disclaimer που βάζω πάντα σε κάθε αναρτημένο πρόγραμμα "Το πρόγραμμα έχει δοκιμασθεί σε C++ Builder 2010 και μπορεί να περιέχει Bugs ή άλλες αβλεψίες", είναι μεταξύ άλλων και για αυτές τις περιπτώσεις που ο compiler είναι λιγότερο αυστηρός στην εφαρμογή των Standards.

Δημοσ.

Μπήκα κι εγώ στο... τριπάκι κι έφτιαξα μια απλή υλοποίηση σε C... είναι και περασμένη η ώρα.

 

Μιας και η στάνταρ C δεν παρέχει τις ευκολίες της C++ με τις έτοιμες δομές και τους αλγόριθμους, την έκανα ψιλο-χοντρο-μπακάλικα στα γρήγορα, χωρίς να ασχοληθώ με βελτιστοποιήσεις.

 

Παρέχει όμως στοιχειώδη παραμετροποίηση τόσο για το μήκος και το πλήθος των κλειδιών όσο και για ομάδες χαρακτήρων που θέλουμε να συμμετέχουν στην διαδικασία παραγωγής τους, καθώς και για τυχόν απαίτηση να υπάρχει τουλάχιστον ένας χαρακτήρας από κάθε ομάδα στο κλειδί (η παραμετροποίηση γίνεται κυρίως μέσω απλών macros)

 

Επίσης κάθε παραγόμενο κλειδί είναι μοναδικό.

 

Είναι πολύ πιθανό να περιέχει bugs, αν και σε μερικά τεστ που έκανα δείχνει να είναι οκ...

 

>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>

#define KEYSIZE			10+1		// μέγεθος κλειδιού
#define NKEYS			10		// πλήθος κλειδιών

// don't touch these
#define DEMAND_LOWER		0x01
#define DEMAND_UPPER		0x02
#define DEMAND_DIGITS		0x04
#define DEMAND_SPECIAL		0x08

// you may add or remove chars in any of the following sets of chars
#define POOL_LOWER		"abcdefghijklmnopqrstuvwxyz"
#define POOL_UPPER		"ABCDEGFHIJKLMNOPQRSTUVWXYZ"
#define POOL_DIGITS		"0123456789"
#define POOL_SPECIAL 		"/?.,><|!@#$%^&*()_+=-~;:][}{# \\\""

// -------------------------------------------------------------------------------------
// Return true if the cstring keys[ istop ] is not already contained between the
// 0th and the (istop-1)th element of the keys[] array of cstrings,
// otherwise return false
//
_Bool unique_key( const int istop, char keys[ NKEYS ][ KEYSIZE ] )
{
register int ikey = 0;
for (; ikey < istop; ikey++) {
	if ( strcmp( keys[ikey], keys[istop] ) == 0 ) {
		return false;
	}
}

return true;
}
// -------------------------------------------------------------------------------------
void apply_demands( const unsigned char demand, char *key, const size_t keylen )
{
int i, c;
unsigned char cmap[256] = { 0 }, imap[keylen];
memset(imap, 0, keylen);

if ( demand & DEMAND_LOWER )		// put a lowercase letter inside the key
{
	c = POOL_LOWER[ rand() % strlen(POOL_LOWER) ];

	key[ i=rand() % keylen ] = c;
	cmap[ c ] = imap[ i ] = 1;
}

if ( demand & DEMAND_UPPER )		// put an uppercase letter inside the key
{
	do {
		c = POOL_UPPER[ rand() % strlen(POOL_UPPER) ];
	} while( cmap[c] == 1 );

	do {
		i = rand() % keylen;
	} while( imap[ i ] == 1 );

	key[ i ] = c;
	cmap[ c ] = imap[ i ] = 1;
}

if ( demand & DEMAND_DIGITS )		// put a digit inside the key
{
	do {
		c = POOL_DIGITS[ rand() % strlen(POOL_DIGITS) ];
	} while( cmap[c] == 1 );
		
	do {
		i = rand() % keylen;
	} while( imap[ i ] == 1 );

	key[ i ] = c;
	cmap[ c ] = imap[ i ] = 1;
}

if ( demand & DEMAND_SPECIAL)		// put a special char inside the key
{
	do {
		c = POOL_SPECIAL[ rand() % strlen(POOL_SPECIAL) ];
	} while( cmap[c] == 1 );

	do {
		i = rand() % keylen;
	} while( imap[ i ] == 1 );

	key[ i ] = c;
	cmap[ c ] = imap[ i ] = 1;
}

return;
}
// -------------------------------------------------------------------------------------
int main( void )
{
/*
 * To remove the demand for the generation of at least 1 char from any of the
 * available sets of chars, delete or comment out the corresponding line below
 */
unsigned char demand =			// bitflags byte for demanded chars
		DEMAND_LOWER		// ... demand at least one lower letter
		+ DEMAND_UPPER		// ... demand at least one upper letter
		+ DEMAND_DIGITS		// ... demand at least one digit
		+ DEMAND_SPECIAL	// ... demand at least one special char
		;
/*
 * To prevent generation of chars from any of the available sets of chars,
 * delete or comment out the corresponding line below.
 * IMPORTANT: 	if you delete a line here, then you MUST also delete the
 * 		corresponding set of chars in the initialization sprintf
 *		(see a few lines down).
 */
const int POOLSIZE =			// size of the pool to select chars from
		strlen(POOL_LOWER)	// ... for lower case letters
		+ strlen(POOL_UPPER)	// ... for upper case letters
		+ strlen(POOL_DIGITS)	// ... for digits
		+ strlen(POOL_SPECIAL)	// ... for special chars
		+ 1;			// ... for '\0'

char pool[ POOLSIZE ];			// cstring: the pool to select chars from
memset( pool, 0, POOLSIZE);		// clear the pool

/*
 * IMPORTANT:
 *	if you deleted any line from the definition of POOLSIZE above
 *	then you MUST adjust accordingly the following sprintf function...
 */
					// initialize the selection pool
sprintf(pool, "%s%s%s%s", POOL_LOWER, POOL_UPPER, POOL_DIGITS, POOL_SPECIAL);

char keys[ NKEYS ][ KEYSIZE ] = { {0, 0} };	// array of cstring keys
int i=0, ikey=0;			// indices: i for chars, ikey for keys

srand( time(NULL) );			// initialize the pseudo-random seed

for (ikey=0; ikey < NKEYS; ikey++ )	// generate & store cstring keys
{					// into the keys[] array of cstrings
	do {				// ... this do-loop makes sure that
					// ... each generated key is unique
		for ( i=0; i < KEYSIZE-1; i++ ) {	// generate chars of key
			keys[ikey][i] = pool[ rand() % (POOLSIZE-1) ];
		}
	} while ( !unique_key(ikey, keys) );

					// apply any char demands
	apply_demands( demand, keys[ikey], KEYSIZE-1 );

	puts( keys[ikey] );		// print the generated key
}

return 0;
}

>

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...