15 September 2016

Encode Code Md5 - Mã hóa Code Md5 Java Jsp

test-md5.jsp
Java web 2016
<%@page import="java.security.MessageDigest"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        
        <%

        // Input password
            String passwd = "password";
            out.println("String: " + passwd + ".<br>");

        // Create a new instance of MessageDigest, using MD5. SHA and other
        // digest algorithms are also available.
            MessageDigest alg = MessageDigest.getInstance("MD5");

        // Reset the digest, in case it's been used already during this section of code
        // This probably isn't needed for pages of 210 simplicity
            alg.reset();

        // Calculate the md5 hash for the password. md5 operates on bytes, so give
        // MessageDigest the byte verison of the string
            alg.update(passwd.getBytes());

        // Create a byte array from the string digest
            byte[] digest = alg.digest();

        // Convert the hash from whatever format it's in, to hex format
        // which is the normal way to display and report md5 sums
        // This is done byte by byte, and put into a StringBuffer
            StringBuffer hashedpasswd = new StringBuffer();
            String hx;
            for (int i = 0; i < digest.length; i++) {
                hx = Integer.toHexString(0xFF & digest[i]);
                //0x03 is equal to 0x3, but we need 0x03 for our md5sum
                if (hx.length() == 1) {
                    hx = "0" + hx;
                }
                hashedpasswd.append(hx);
            }

        // Output hashedpasswd.toString()
            out.println("MD5: " + hashedpasswd.toString() + "<br>");
        %>

    </body>
</html>
Input String: password
Ouput md5: 5f4dcc3b5aa765d61d8327deb882cf99

Test md5: md5-decrypter
 Encrypt Md5 in Java
Java core  2016
/*
 * MD5 Cấp độ khó giải mã nhất
 */
package javaapplication1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class Ex01 {

    private static String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 6) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9)) {
                    buf.append((char) ('0' + halfbyte));
                } else {
                    buf.append((char) ('a' + (halfbyte - 10)));
                }
                halfbyte = data[i] & 0x0F;
            } while (two_halfs++ < 1);
        }
        return buf.toString();
    }

    public static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] md5hash = new byte[32];
        md.update(text.getBytes("iso-8859-1"), 0, text.length());
        md5hash = md.digest();
        return convertToHex(md5hash);
    }

    public static void main(String[] args) throws IOException {
        BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter string:");
        String rawString = userInput.readLine();
        try {
            System.out.println("MD5 hash of string: " + MD5(rawString));
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang