"class_name": "javax.crypto.Mac"
时间: 2024-06-16 21:02:20 浏览: 14
"class_name": "javax.crypto.Mac" 是Java Cryptography Extension (JCE)中的一个接口,它代表了消息认证码(Message Authentication Code,简称MAC)生成器。Mac接口用于创建和验证数据的完整性,通常与密码或密钥一起使用,以确保数据在传输过程中不被篡改。Java提供了一系列实现这个接口的类,如Hmac(基于哈希函数的MAC),它们支持不同的哈希算法,如SHA-1、SHA-256等。
相关问题
"class_name": "javax.crypto.Cipher"
"class_name": "javax.crypto.Cipher" 是Java Cryptography Extension (JCE)的一部分,它提供了一种加密和解密数据的标准接口。`Cipher` 类是Java平台中用于执行各种密码算法(如对称加密、非对称加密、哈希和消息认证码)的核心类。
具体来说,`Cipher` 的主要作用是:
1. 加密/解密:它允许你将明文数据加密为密文,或将密文数据解密回明文状态。这通常是通过调用其提供的 `doFinal()` 或 `update()` 方法来完成。
2. 模式和填充:`Cipher` 提供了不同的操作模式(如ENCRYPT_MODE, DECRYPT_MODE, NO_ENCRYPT_NO_DECRYPT等)以及填充方式(如PKCS7填充),以便控制加密过程的细节。
3. 初始化:在使用前,你需要创建`Cipher` 实例,并用特定的密钥和算法对其进行初始化。
签名步骤:byte2HexStr (HMAC_SHA256(post-content +RequestURI, key), RequestURI 以”/” 开头,不包括域名;post-content 是指发送的完整 json 数据;key= AppSecret+Nonce+ timestamp, AppSecret 是和 vendorId 对应的密钥,签名的结果需要放到 name 为”parm-sign” 的 http header 中。 以下代码列出了 java 版本的签名实现: import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.Charset; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.commons.codec.binary.Hex; public class EncryptUtil { /** * * @param plainStr 需要加密的字符串 * @param key key=AppSecret+nonce+ timestamp * @return 加密后的字符串 */ public static String hmacsha256(String plainStr, String key) { SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), "HmacSHA256"); Mac mac=null; try { mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); } catch (NoSuchAlgorithmException | InvalidKeyException e) { e.printStackTrace(); } byte digest[] = mac.doFinal(plainStr.getBytes(Charset.forName("UTF-8"))); return new StringBuilder().append(byte2HexStr(digest)).toString(); } public static String byte2HexStr(byte array[]) { return array != null ? new String(Hex.encodeHex(array)) : null; } } 请以PHP7.4版本实现签名
以下是PHP 7.4版本的实现,使用了相同的依赖库`apache/commons-codec`来进行十六进制编码。
```php
<?php
use function Sodium\crypto_generichash;
use function Sodium\crypto_pwhash_str;
use function Sodium\crypto_pwhash_str_verify;
require_once __DIR__ . '/vendor/autoload.php';
function hmacsha256($plainStr, $key)
{
$secretKey = new \SecretKeySpec(
utf8_encode($key),
'HmacSHA256'
);
$mac = hash_init('sha256', HASH_HMAC, $secretKey);
hash_update($mac, utf8_encode($plainStr));
$digest = hash_final($mac, true);
return byte2HexStr($digest);
}
function byte2HexStr($array)
{
return $array !== null ? \strval(Hex::encode($array)) : null;
}
function generate_password($password, $salt)
{
return crypto_pwhash_str($password, \SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE, \SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE);
}
function verify_password($hash, $password)
{
return crypto_pwhash_str_verify($hash, $password);
}
```
使用方法示例:
```php
$plainStr = '...'; // post-content RequestURI
$key = '...'; // AppSecret Nonce timestamp
$sign = hmacsha256($plainStr, $key);
$headers = [
'parm-sign: ' . $sign,
];
// 发送HTTP请求,携带上述 headers
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)