Java Encryption Algorithm-MD5 Encryption et hachage de hachage avec le code source de l'algorithme de cryption de clé secrète
J'ai récemment appris la connaissance des algorithmes de chiffrement et utilisé le cryptage MD5. Baidu a beaucoup d'informations en ligne, mais elle n'est pas très détaillée. Ici, je trierai comment implémenter l'algorithme de chiffrement avec le cryptage MD5 et le hachage de hachage avec des clés secrètes. Vous pouvez jeter un œil.
Code d'implémentation:
Package com.ompa.common.utils; import java.security.messagedigest; import java.security.nosuchalgorithmexception; import javax.crypto.mac; import javax.crypto.secretkey; importer javax.crypto.spe.secretkeypec; / *** utilise javax.crypto.spe.secretkeypec; / *** utilisation de javax.cryption * * @aroror zhangcd * @date 2016-4-29 * / classe publique EncrypTutil {chaîne finale statique privée mac_name = "hmacsha1"; Encoding de chaîne finale statique privée = "UTF-8"; Key de chaîne finale statique privée = "Iloveyou"; / *** * md5 ajoute du code pour générer le code MD5 32 bits * / public static string string2md5 (string instr) {messagediGest md5 = null; essayez {md5 = messagediGest.getInstance ("md5"); } catch (exception e) {System.out.println (e.toString ()); e.printStackTrace (); retour ""; } char [] chararray = instlit.tocharArray (); octet [] bytearray = new byte [chararray.length]; for (int i = 0; i <chararray.length; i ++) bytearray [i] = (byte) chararray [i]; octet [] md5bytes = md5.digest (bytearray); StringBuffer hexValue = new StringBuffer (); for (int i = 0; i <md5bytes.length; i ++) {int val = ((int) md5bytes [i]) & 0xff; if (val <16) hexvalue.append ("0"); hexvalue.append (Integer.tohexstring (val)); } return hexvalue.toString (); } / *** * MD5 Encryption génère un code MD5 32 bits * / public static string stringmd5 (string instr) {return String2md5 (string2md5 (instr)); } / ** * Algorithme de cryptage et de décryptage * / chaîne statique publique convertmd5 (chaîne instr) {char [] a = instlit.tocharArray (); for (int i = 0; i <a.length; i ++) {a [i] = (char) (a [i] ^ 't'); } String s = new String (a); retour s; } / ** * HMAC-Sha1 * @param EncryptText * @param Encryptkey * @return * @throws Exception * / public static String hmacsha1encrypt (String EncryptText, String EncryptKey) lève exception {byte [] data = Encryptkey.getBytes (Encoding); SecretKey SecretKey = new SecretKeyspec (Data, Mac_Name); Mac mac = mac.getInstance (mac_name); mac.init (Secretkey); Byte [] text = encryptText.getBytes (codage); BYTE [] str = mac.dofinal (texte); // Créer Hex StringStringBuffer hexstring = new StringBuffer (); // Convertir le tableau d'octet en numéro hexadecimal pour (int i = 0; i <str.length; i ++) {String shahex = Integer.toHexString (Str [i] & 0xff); if (shahex.length () <2) {hexstring.append (0);} hexstring.append (shahex);} return hexstring.toString ();} public static string convertsha1 Blocke.printStackTrace (); return "";}} // Testez la fonction principale publique statique void main (String args []) lève une exception {// hachage de hachage avec une chaîne de cryptage de clé secrète tt = convertsha1 ("123456"); System.out.println (tt); // md5 chaîne d'encryption S = new String ("123456"); System.out.println ("Origin:" + S); System.out.println ("après MD5:" + String2MD5 (S)); System.out.println ("après MD5:" + StringMd5 (S));}}Merci d'avoir lu, j'espère que cela peut vous aider. Merci pour votre soutien à ce site!