Java Criptografia Algoritmo MD5 Criptografia e Hash Hash com Código-fonte de Criptografia de Chave Secreto
Recentemente, aprendi o conhecimento dos algoritmos de criptografia e usei a criptografia MD5. O Baidu tem muitas informações on -line, mas não é muito detalhado. Aqui vou resolver como implementar o algoritmo de criptografia com criptografia MD5 e hash com teclas secretas. Você pode dar uma olhada.
Código de implementação:
pacote com.ompa.common.utils; importar java.security.messagedigest; importar java.security.nosuchalgorithMexception; importar javax.crypto.mac; importectkeys.crypto.secretKey; importação javax.crypto.sectkeyspyspect; zhangcd* @date 2016-4-29*/public class Encryptutil {private estático final string mac_name = "hmacsha1"; String final estática privada Encoding = "UTF-8"; String final estática privada key = "iloveyou";/ ****md5 adiciona código para gerar código md5 de 32 bits*/ public static string string2md5 (string instrum) {Messagedigest md5 = null; tente {md5 = Messagedigest.getInstance ("md5"); } catch (Exceção e) {System.out.println (e.toString ()); E.PrintStackTrace (); retornar ""; } char [] CharArray = Instr.ToCharArray (); byte [] byteArray = new Byte [CharArray.length]; for (int i = 0; i <CharArray.Length; i ++) bytearray [i] = (byte) chararray [i]; byte [] md5bytes = md5.digest (bytearray); StringBuffer hexvalue = new StringBuffer (); para (int i = 0; i <md5bytes.length; i ++) {int val = ((int) md5bytes [i]) e 0xff; if (val <16) hexvalue.append ("0"); hexvalue.append (Integer.toHexString (VAL)); } return hexvalue.toString (); } / ****A criptografia md5 gera código MD5 de 32 bits* / public static string StringMd5 (String Instr) {return string2md5 (String2md5 (Instr)); }/ *** Algoritmo de criptografia e descriptografia*/ public static string convertmd5 (String Instr) {char [] a = Instr.ToCharArray (); for (int i = 0; i <a.Length; i ++) {a [i] = (char) (a [i] ^ 't'); } String s = new string (a); retorno s; } / ** * HMAC-SHA1 * @PARAM EncryptText * @param EncryptKey * @return * @throws Exceção * / public static string hmacsha1encrypt (string EncryptText, string encryptKey) lança exceção {byte [] data = EncryptKey.getBytes (encobrimento); SecretKey SecretKey = New SecretKeyspec (Data, Mac_Name); Mac mac = mac.getInstance (mac_name); Mac.init (SecretKey); byte [] text = EncryptText.getBytes (codificação); byte [] str = Mac.Dofinal (texto); // Crie HEXSTRINGBUFFER HEXSTRING = new StringBuffer (); // Converte a matriz de bytes em número hexadecimal para (int i = 0; i <str.length; i ++) {string shahex = integger.ToHexstring (String i] e 0,n e string; if (shahex.length () <2) {hexstring.append (0);} hexstring.append (shahex);} retorna hexstring.toString ();} public static string convertssha1 (string instr) {{return hmacsha1 {TEXTIMATETMATSSHA1 (String Instr) {Try {return.) blocke.printStackTrace (); return "";}} // teste a função principal public estática void main (string args []) lança exceção {// hash hash com chave de criptografia secreta string tt = 123456; System.out.println ("origem:" + s); System.out.println ("Após o MD5:" + String2md5 (s)); System.out.println ("Após Md5:" + StringMd5 (s));}}Obrigado pela leitura, espero que isso possa ajudá -lo. Obrigado pelo seu apoio a este site!