多くの場合、秘密の暗号化には永続的な暗号化が必要であり、この時点での暗号化はMD5を使用します。対称暗号化を使用する場合、DESメソッドが使用されます
java.securty.securty.securryandom Import sun.misc.basecoder.misc.baseencoder( / ** key) * @author mingge * / public class keysutil "; private final static string md =" md "; private final static string key =" opeddsaeaddadbcabf "; / ** * md necryption algorithm * @param data * @return * / public st atic string mdencrypt(string data){ String resultString {resultString = new String(MESSAGENGEST.GETINSTANCE(MD); resultString;} private string bytehexstring(byte [] bytes){new stringbuffer(bytes.length *); xff)<x){bf.append( "t");キー値へ * @param data * @paramキー暗号化キーバイト配列 * @return * @throws例外 */ public static string desencrypt(string data、string key)shrows {if(key == null){key = key; } byte [] bt = encrypt(data.getBytes()、key.getBytes()); param data * @paramキー暗号化キーバイト配列 * @return * @throws ioexception * @throws例外 */ public static string desdecrypt(string data、string key)throws ioexcエピソード、例外{if(data == null){return null ;キー== null){key} badecoder = new basecoder() )新しい文字列(BT) ] d ata、byte [] key)スロー例{//信頼できる乱数securerandom sr = new Securerandom()キーファクトリーを作成し、DeskeyspecをSecretKeyFactory = SecretKeyFactory(DES)に変換しますcipher.getInstance(des); PARAM DATA * @PARAMキー暗号化キーバイト配列 * @return * @Throws Exception */ private static byte [] decrypt(byte [] data、byte [] key)スロー例外新しいsec urerandom(); des); securekey.generatecret(dks); )cipher.do final(データ);以下に、JavaでMD5暗号化と復号化クラスを実装するコードを紹介します
Javaは、MD5の暗号化と復号化クラスをテストクラスで実装して、詳細についてはコードを参照してください。
MD5暗号化と復号化クラス-MyMD5util、コードは次のとおりです。
com.zyg.md5静的な文字列hex_nums_str = "0123456789ABCDEF"; =(hex.length() / 2)result = hexchars = hex.tochararray; pos = i * 2 =(byte)(hex_nums_str.indexof(pos] << 4 | hex_nums_str.indexof(pos + 1]);指定されたバイトアレイを16進ストリング * @param b * @return */ public static string bytetohexstring(byte [] b){stringbuffer hexstring = new StringBuffer(); i ++){string hex = integer.tohexstring(b [i]&0xff); :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::づ 火::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::づ 火::::: ::::::::::::::::::); * @param passwordindb * @return * @throws nosuchalgorithmexception * @ @throws unsupportedencodingexception */public static boolean validpassword(string password indb)nosuchalgorithmexception、nosuchalgorithmexception、nosuchalgorithmexception、supportedencodingexception {// hexstringtobyte(パスワードindb); ; //メッセージダイジェストMESSAGES MD = MESSAGEDGEST.GETINSTANCE:::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::づ 火::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::づ 火::::: :ソルトデータは、メッセージダイジェストオブジェクト(塩)に渡されます入力パスワードbyte [] digest = md.digest();データベースSystem.ArrayCopy(PWDINDB、SALT_LENGTH、0、DIGESTINDB.LENGTH.LENGTE)のメッセージダイジェスト。 arrays.equals(digest、digestindb)){//パスワードは正しく返されますhexadecimalパスワード * @paramパスワード * @return * @throws nosuchalgorithm例外 * @throws unsupportedencodingexception * /public static string getEncryptedpwd(String Password)スローNosuchalgorithMexception、supportedencodingexception {//ランダム数ジェネレーションsecureandom random = new Securerandom(); MESSAGES MD = NULL; Object Md.update(passwore.getBytes( "UTF-8"));パスワード、塩のバイトの長さを追加します= new byte [digest.length + salt_length]; 、0、pwd、0、salt_length);パスワードの[バイト]アレイ形式へのパスワードは、bytohexstring(pwd)}}}を返します。テストクラス - クライアント、コードは次のとおりです。
com.zyg.security.io.unsupportedencection.nosuchalgorithmexception; ; string [] string username = "123"; )loginuserid = "zyg"; out println(パスワードエラー、再び!!!」} / TODO自動生成キャッチブロックe.printstacktrace(); try {ecryptedpwd = getEncryptedPwd(username、necrygorithmexcept); / TODO自動生成キャッチブロックe.printstacktrace();ユーザー名、文字列パスワード)nosuchalgorithmexception、unsupportedencodingection {string pwdindb =(string)users.get(username); System.out.println( "このユーザーは存在しません! ! ! "); falseを返します;}}}