doutuo7126
doutuo7126
2013-04-03 05:19

这种Java加密方法的等效PHP解密是什么?

已采纳

I have the following Java encryption method, and I'd like to know what the equivalent PHP decryption for it is, if there is one. If there is not an equivalent PHP decryption function for PHP, then what other options do I have? Thanks in advance.

    private String encrypt(String string, String key) {
    StringBuilder enc = new StringBuilder();
    try {
        Mac mac = Mac.getInstance("HMACSHA256");
        SecretKeySpec secret = new SecretKeySpec(key.getBytes(), "HMACSHA256");
        mac.init(secret);
        byte[] digest = mac.doFinal(string.getBytes());
        for (byte b : digest) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1)
                enc.append('0');
            enc.append(hex);
        }
    } catch (Exception e) {
    }
    return enc.toString();
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dqh19413 dqh19413 8年前

    Just take a look at php.net: http://www.php.net/manual/en/function.hash-hmac.php

    An equal method would be

    hash_hmac("sha256", $data, $key, false);
    

    And of course its not an encryption, but an hash function. So its non reversible.

    点赞 评论 复制链接分享
  • doudi5524 doudi5524 8年前

    SHA algorithms are one way hashes, which means you can never decrypt them.

    点赞 2 评论 复制链接分享