怎么用SHA256PRNG或者SecureRandom.getInstanceStrong()方法来创建SecureRandom实例。
时间: 2024-06-11 09:10:47 浏览: 21
使用SHA256PRNG来创建SecureRandom实例:
```
SecureRandom sr = SecureRandom.getInstance("SHA256PRNG");
```
使用SecureRandom.getInstanceStrong()来创建SecureRandom实例:
```
SecureRandom sr = SecureRandom.getInstanceStrong();
```
请注意,使用SHA256PRNG可能会导致性能问题,因为它需要更多的计算。因此,建议在需要更高安全性的情况下使用。而SecureRandom.getInstanceStrong()则是使用系统提供的最安全的随机数生成器。
相关问题
php实现SecureRandom.getInstance("SHA1PRNG
在 PHP 中,可以使用 openssl_random_pseudo_bytes 函数来生成伪随机数。这个函数可以生成高质量的随机数,并且使用了一些安全算法来生成随机数。
下面是一个示例代码,可以通过 SHA1PRNG 算法生成伪随机数:
```
<?php
$random_bytes = openssl_random_pseudo_bytes(32, $strong);
if ($strong == true) {
// 生成成功
} else {
// 生成失败,不要使用生成的随机数
}
```
在这个示例中,openssl_random_pseudo_bytes 函数生成了一个长度为32字节的随机数,并且使用了系统默认的加密算法。如果 $strong 参数为 true,表示生成的随机数是高质量的,可以放心使用;如果 $strong 参数为 false,表示生成的随机数不是高质量的,应该避免使用。
php实现KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(param.getBytes()); kgen.init(256, secureRandom); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
在PHP中,可以通过以下方式实现类似Java中的代码:
```php
// 初始化安全随机数生成器
$secureRandom = openssl_random_pseudo_bytes(32, $cryptoStrong);
if (!$cryptoStrong) {
// 安全随机数生成失败
die("Cannot generate secure random bytes");
}
// 设置随机数生成器的种子
$param = "your_seed_string";
$secureRandom = hash_hmac('sha1', $param, $secureRandom, true);
// 初始化AES密钥生成器
$key = openssl_cipher_iv_length('aes-256-cbc');
$secretKey = openssl_random_pseudo_bytes($key);
// 生成AES密钥
$key = hash_hmac('sha256', $secretKey, $secureRandom, true);
$key = substr($key, 0, $keyLength);
// 将密钥编码为字节数组
$enCodeFormat = $key;
// 构造一个AES密钥的密钥规范对象
$key = new \stdClass();
$key->key = $enCodeFormat;
$key->algorithm = "AES";
$key->format = "RAW";
```
在上述代码中,我们使用了`openssl_random_pseudo_bytes`函数生成安全随机数,并使用`hash_hmac`函数基于种子字符串和安全随机数生成器的结果生成密钥。最后,我们将生成的密钥编码为字节数组,并使用`stdClass`对象构造了一个密钥规范对象,其中包含了密钥、算法和格式等信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)