Eight, MD5 encryption and encapsulation, and encapsulation method call

A, MD5 encryption

Package Md5

public  class the Md5 { 

    // hexadecimal character array digital mapping of 
    Private  static  Final  char hexdigits [] = { '0', '. 1', '2', '. 3', '. 4', '. 5', '. 6', '. 7', '. 8', '. 9' ,
             'A', 'B', 'C', 'D', 'E', 'F.' }; 

    / ** the encryption inputString * / 
    public  static String md5Str (String inputstr) {
         return the encrypt (inputstr); 
    } 

    public  static String the encrypt (String inputstr) {
         byte [] = inStrBytes inputStr.getBytes ();
        the try {
             // create a message digest with the specified algorithm name
            The MD = MessageDigest.getInstance the MessageDigest ( "the MD5" );
             // with the specified byte array to digest last updated, and then completes the digest calculated 
            MD.update (inStrBytes);
             byte [] = mdByte MD.digest ();
             char [ ] = STR new new  char [mdByte.length * 2 ];
             int K = 0 ;
             for ( int I = 0; I <mdByte.length; I ++ ) {
                 byte TEMP = mdByte [I]; 
                STR [K ++] = hexdigits [>>> TEMP & 0xF. 4 ]; 
                STR [K ++] = hexdigits [TEMP & 0xF ];
            }
            return new String(str);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

}

Second, the general request parameters + secret encrypted, so the re-encapsulated, and returns the encrypted MD5 string (32) Request Parameter + secret]

Import java.io.UnsupportedEncodingException;
 Import java.security.InvalidKeyException;
 Import java.security.NoSuchAlgorithmException;
 Import java.util.TreeMap; 

public  class SJS { 

    // Entrance 
    / ** 
     * encrypting request parameters 
     * @param requestParams request parameters 
     * @param secret root keys 
     * @return 
     * @throws Exception
      * / 
    public String getSJSInfo (the TreeMap <String, Object> requestParams, String secret) throws Exception {
         returnsjsSi (buildParamStr (requestParams), Secret); 
    } 

    // call the MD5 method for packaging into encrypted [parameter] + secret 
    Private String sjsSi (signStr String, String Secret)
         throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
         return Md5.md5Str (signStr + Secret) ; 
    } 

    // the parameters into (format key = value) & spliced with, and returns 
    public String buildParamStr (the TreeMap <String, Object> requestParams) { 
        the StringBuilder retstr = new new the StringBuilder ();
         for (String Key: requestParams.keySet ( )) {
             IF (retStr.length () == 0 ) {
                retStr.append(key+"="+String.valueOf(requestParams.get(key)));
            } else {
                retStr.append("&"+key+"="+String.valueOf(requestParams.get(key)));
            }
        }
        return retStr.toString();
    }

}

 

Guess you like

Origin www.cnblogs.com/chushujin/p/11387236.html