LoginSignup
0
0

More than 5 years have passed since last update.

javaでPHPのMD5の結果を同じにする方法

Posted at

概要

パラメーターの改ざん対策にMD5を使ってPHPとJavaで同じ結果にしたい

やってみよう

1. PHPでは

      // MD5(固定文字列+12345)
        $key = md5(固定文字列.12345);

2. Javaでは

String key = getHash(固定文字列+12345);

private String getHash(String param) throws BusinessException {
    String md5_str = "";

    try {
         byte[] str_bytes = param.getBytes("UTF-8");
         MessageDigest md = MessageDigest.getInstance("MD5");
         byte[] md5_bytes = md.digest(str_bytes);
         BigInteger big_int = new BigInteger(1, md5_bytes);
         md5_str = big_int.toString(16);
        } catch (Exception e) {
          throw new Exception("MD5 fail");
        }
        return md5_str;
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0