java sha1加密与php sha1加密结果不一致

java sha1加密与php sha1加密结果不一致

java sha1加密得出来的结果和php sha1 结果不一致解决办法:
java calss类:

import java.util.logging.Level;
import java.util.logging.Logger;
import java.security.MessageDigest;
/**
 * @version V1.0
 * @desc sha1 加密工具类
 */
public class SHA1Util {

    /**
     * sha1加密操作
     *
     * @param content 待加密内容
     * @return 返回String
     */
    public static String encrypt(String content) {
        try {
        // Create MD5 Hash
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.update(content.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++)
            hexString.append(String.format("%02X", 0xFF & messageDigest[i]));

        return   hexString.toString().toLowerCase();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void main(String[] args) {
        String content = "foobar123dsadn23u1wdqqwdyxdasd";

        System.out.println("加密前:" + content);

        String encrypt = SHA1Util.encrypt(content);
        System.out.println("加密后:" + encrypt);

    }

}

php:

echo sha1('foobar123dsadn23u1wdqqwdyxdasd');

结果:

加密前:foobar123dsadn23u1wdqqwdyxdasd
加密后:d8033103e9aaf67af13a4b45534b2d0f6d8dfded

你可能感兴趣的:(php)