في Java كيفية إنشاء كلمة مرور عشوائية قوية - برنامج تعليمي كامل آمن وعشوائي
نشرت: 2017-07-29 افتراضيًا ، لا تحتوي Java على أي أداة تساعد على إنشاء كلمة مرور عشوائية طويلة وقوية. هنا قمنا بإنشاء برنامج تعليمي مفصل حول كيفية إنشاء كلمة مرور عشوائية قوية باستخدام java.security.SecureRandom
API.
- Java Security - إنشاء كلمة مرور عشوائية آمنة من أجل الخير
- كيف يتم إنشاء سلسلة أبجدية رقمية عشوائية آمنة في جافا؟
- مولد كلمة السر في كود مصدر جافا
- تقوم Java بإنشاء كلمة مرور عشوائية بأحرف خاصة
سنستخدم جدول ASCII للحصول على أحرف خاصة بالقيمة العشرية في جافا. ألق نظرة أدناه على التعيين بين Decimal Value
Characters
.
هذا هو التدفق:
- قم بإنشاء طريقة عامة
CrunchifyRandomPasswordGenerator()
حيث سنحصل على قيم ASCII العشرية في 3 حلقات مختلفة. - سيتم تعبئة ArrayList بقيم ASCII العشرية
-
crunchifyGetRandom()
ستحصل على حرف عشوائي من القائمة أعلاه وترتبط بـ Char. - لدينا حلقتين في الطريقة الرئيسية ()
- الحلقة الخارجية لطباعة عدد كلمات المرور
- حلقة داخلية لطباعة عدد الأحرف لكلمة مرور قوية
- هذا هو. فقط قم بتشغيل البرنامج أدناه وستكون جاهزًا.
برنامج جافا:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
package crunchify . com . tutorial ; import java . security . SecureRandom ; import java . util . ArrayList ; import java . util . Collections ; import java . util . List ; /** * @author Crunchify.com * Best way to generate very secure random Password automatically */ public class CrunchifyRandomPasswordGenerator { // SecureRandom() constructs a secure random number generator (RNG) implementing the default random number algorithm. private final SecureRandom crunchifyRandomNo = new SecureRandom ( ) ; private final List < Character > crunchifyValueObj ; // Just initialize ArrayList crunchifyValueObj and add ASCII Decimal Values public CrunchifyRandomPasswordGenerator ( ) { crunchifyValueObj = new ArrayList < > ( ) ; // Adding ASCII Decimal value between 33 and 53 for ( int i = 33 ; i < 53 ; i ++ ) { crunchifyValueObj . add ( ( char ) i ) ; } // Adding ASCII Decimal value between 54 and 85 for ( int i = 54 ; i < 85 ; i ++ ) { crunchifyValueObj . add ( ( char ) i ) ; } // Adding ASCII Decimal value between 86 and 128 for ( int i = 86 ; i < 127 ; i ++ ) { crunchifyValueObj . add ( ( char ) i ) ; } // crunchifyValueObj.add((char) 64); // rotate() rotates the elements in the specified list by the specified distance. This will create strong password // Totally optional Collections . rotate ( crunchifyValueObj , 5 ) ; } public static void main ( String [ ] args ) { CrunchifyRandomPasswordGenerator passwordGenerator = new CrunchifyRandomPasswordGenerator ( ) ; log ( "Crunchify Password Generator Utility: \n" ) ; StringBuilder crunchifyBuilder = new StringBuilder ( ) ; // Let's print total 8 passwords for ( int loop = 1 ; loop < = 8 ; loop ++ ) { // Password length should be 23 characters for ( int length = 0 ; length < 42 ; length ++ ) { crunchifyBuilder . append ( passwordGenerator . crunchifyGetRandom ( ) ) ; } log ( loop , crunchifyBuilder . toString ( ) ) ; crunchifyBuilder . setLength ( 0 ) ; } } // Simple log util private static void log ( String string ) { System . out . println ( string ) ; } // Simple log util private static void log ( int count , String password ) { System . out . println ( "Password sample " + count + ": " + password ) ; } // Get Char value from above added Decimal values // Enable Logging below if you want to debug public char crunchifyGetRandom ( ) { char crunchifyChar = this . crunchifyValueObj . get ( crunchifyRandomNo . nextInt ( this . crunchifyValueObj . size ( ) ) ) ; // log(String.valueOf(crunchifyChar)); return crunchifyChar ; } } |

إخراج وحدة التحكم Eclipse:
1 2 3 4 5 6 7 8 9 10 11 12 |
Crunchify Password Generator Utility : Password sample 1 : &fASZ`2|; MW7 ^ y % E @ G8eObKMVP @ 09LEGV1Pu ] a7 ( T Password sample 2 : 'd<&W_|_"6==>`wl`PA"0e[_/E>fYleBb\d#;O}%aN Password sample 3: ?@/H6K@dT/~,?B0@<[yceBBS_' Jd9D ` 26l [ XEv ; WRf Password sample 4 : '"$H62TljbX6$n.PZ.N' @ , 68 #`|!XvE_Hf/d~TSluZ Password sample 5 : Zw ; acO ! zlIVV @ H6 - cf9 ? hI | c { @ $ ] C < V #Z=H@<sne9O Password sample 6 : * y9ogOM89m \ 3JGhZ ) - ` b4kl ~ A | Saz &4d [ ^ ~ S } @ / ` tS Password sample 7 : $ F6x ~ < pDBmOOk ? < EaK49wNPn ^ EfL = : E / sX : w4To @ - z Password sample 8 : eZ [ ` gF1 , oKH : zJu + 3p9 " tq* kh &z $ Xf ) &TH1 ^ , 7h / ZQ Process finished with exit code 0 |
حاول فهم كيفية استرداد Char من القيمة العشرية هنا:
1 |
char crunchifyChar = ( char ) this . crunchifyValueObj . get ( crunchifyRandomNo . nextInt ( this . crunchifyValueObj . size ( ) ) ) ; |