简体   繁体   中英

Java equivalent for PHP pack() function

I have a PHP function. this code is to encryp a password

Is any one know how can I write this in Java code.

this what I was try but dont give me result.

    byte[] rawSHA = null;
    byte[] base64HexSHA = null;
    MessageDigest md= null;

    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        LOG.error("Unable to load MD5 Message Digest : " + e.getMessage(), e);
        throw new IllegalStateException("MD5 Message Digest Instance Not Found");
    }


    rawSHA = md.digest(rawText.getBytes("UTF-8"));
    base64HexSHA = Base64.encodeBase64(rawSHA);
    System.out.println("result = "+base64HexSHA );

Thanks for help

EDIT : I use this for base64 library

import org.apache.tomcat.util.codec.binary.Base64;
String someString = "qwe";
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
String checksum = new BigInteger(1, messageDigest.digest(someString.getBytes())).toString(16);
System.out.println(checksum);

Hope this helps .. :)

I have Solved this. this may help for other.

    String ps="tes";
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] encryptedPassword = md.digest(ps.getBytes());
    byte[] encodedBytes = Base64.encodeBase64(encryptedPassword);
    String Str2 = new String(encodedBytes);

give me a result :

before =tes
after :KLZi2IO212/Zbk3cXpungA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM