Java基础篇--SecureRandom(安全随机)类

java.security.SecureRandom类是Java中用于生成安全的随机数的一个类。与普通的Random类不同,它提供了一种可信赖的随机数生成器,用于生成具有高度随机性的随机数。

SecureRandom类的实例使用了更加安全的随机数生成算法,这些算法通常经过密码学和安全专家的审查和认证。它们被设计为在保密性、完整性和不可预测性等方面具有更高的安全性。

下面是一些SecureRandom类常用的方法:

  1. nextInt():生成一个伪随机的int值。
    SecureRandom secureRandom = new SecureRandom();
    int randomNumber = secureRandom.nextInt();  // 生成int范围内的随机整数
    
  2. nextInt(int n):生成一个介于0(包括)和指定值n(不包括)之间的伪随机int值。
    SecureRandom secureRandom = new SecureRandom();
    int randomNumberInRange = secureRandom.nextInt(100);  // 生成0到99之间的随机整数
    
  3. nextBytes(byte[] bytes):生成随机字节,并将其填充到指定的byte数组中。
    SecureRandom secureRandom = new SecureRandom();
    byte[] buffer = new byte[10];
    secureRandom.nextBytes(buffer);  // 将随机字节填充到指定的字节数组中
    
  4. generateSeed(int numBytes):生成一个给定长度(以字节为单位)的随机种子,可以用于初始化其他随机算法或密钥生成器。
    SecureRandom secureRandom = new SecureRandom();
    byte[] seed = secureRandom.generateSeed(16);  // 生成16字节长度的随机种子
    

除了上述常用方法外,SecureRandom类还提供了许多其他方法和功能,它适用于需要高安全性和密码学强度的场景,如密码生成、密钥生成等。

值得注意的是,由于SecureRandom类使用的是安全而耗费计算资源的随机数生成算法,因此相对于普通的Random类而言,生成速度可能会较慢。但是它提供了更高的安全性和密码学强度。

下面是使用 SecureRandom 类生成安全随机数的示例代码: 

import java.security.SecureRandom;

public class myclass {
    public static void main(String[] args) {
        // 创建 SecureRandom 对象
        SecureRandom secureRandom = new SecureRandom();

        // 生成随机整数
        int randomInt = secureRandom.nextInt();
        System.out.println("随机整数: " + randomInt);

        // 生成随机字节数组
        byte[] randomBytes = new byte[10];
        secureRandom.nextBytes(randomBytes);
        System.out.print("随机字节数组: ");
        for (byte b : randomBytes) {
            System.out.print(b + " ");
        }
        System.out.println();

        // 生成随机布尔值
        boolean randomBoolean = secureRandom.nextBoolean();
        System.out.println("随机布尔值: " + randomBoolean);

        // 生成随机双精度浮点数
        double randomDouble = secureRandom.nextDouble();
        System.out.println("随机双精度浮点数: " + randomDouble);
    }
}

在上述示例中,我们首先创建了一个 SecureRandom 对象,该对象可以生成安全的随机数。

然后,我们通过调用 nextInt() 方法生成一个随机的整数,并使用 nextBytes() 方法生成指定长度的随机字节数组。最后,通过调用 nextBoolean() 和 nextDouble() 方法分别生成随机布尔值和随机双精度浮点数。

注意:在使用 SecureRandom 生成随机数时,其算法已经经过了安全性增强,并能够提供更高的随机性和预测性保护。

运行示例代码,将得到类似以下输出:

随机整数: -1038453560
随机字节数组: 119 103 114 41 90 -72 -16 76 -25 48 
随机布尔值: false
随机双精度浮点数: 0.7905202451192672
Diffie-Hellman算法是一种密钥交换协议,它可以在不安全的通信信道上协商出一个共享的密钥。下面是用JAVA实现Diffie-Hellman算法的示例代码: ```java import java.math.BigInteger; import java.security.SecureRandom; public class DiffieHellman { private static final BigInteger P = new BigInteger("FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" + "FFFFFFFFFFFFFFFF", 16); private static final BigInteger G = BigInteger.valueOf(2); public static void main(String[] args) { // Alice and Bob generate their private keys BigInteger a = generatePrivateKey(); BigInteger b = generatePrivateKey(); // Alice and Bob calculate their public keys BigInteger A = calculatePublicKey(a); BigInteger B = calculatePublicKey(b); // Alice and Bob exchange their public keys BigInteger secretA = calculateSecretKey(a, B); BigInteger secretB = calculateSecretKey(b, A); // Check if the shared secrets are equal if (secretA.equals(secretB)) { System.out.println("Shared secret: " + secretA); } else { System.out.println("Error: shared secrets do not match"); } } private static BigInteger generatePrivateKey() { SecureRandom random = new SecureRandom(); return new BigInteger(256, random); } private static BigInteger calculatePublicKey(BigInteger privateKey) { return G.modPow(privateKey, P); } private static BigInteger calculateSecretKey(BigInteger privateKey, BigInteger publicKey) { return publicKey.modPow(privateKey, P); } } ``` 该示例代码实现了Diffie-Hellman算法的基本步骤: 1. Alice和Bob生成各自的私钥a和b; 2. Alice和Bob分别使用自己的私钥计算出公钥A和B; 3. Alice和Bob交换公钥; 4. Alice使用自己的私钥和Bob的公钥计算出共享密钥; 5. Bob使用自己的私钥和Alice的公钥计算出共享密钥; 6. Alice和Bob比较计算出的共享密钥是否相同,如果相同则表示密钥交换成功。 在实际应用中,需要注意的是,为了保证安全性,需要选择足够大的素数P和生成元G,以及随机的私钥a和b。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Onlooker﹒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值