java写一个 a^b mod c的算法
时间: 2024-05-14 16:15:43 浏览: 16
可以使用快速幂算法来解决这个问题,具体实现如下:
```java
public static long powerMod(long a, long b, long c) {
long res = 1;
while (b > 0) {
if ((b & 1) == 1) {
res = (res * a) % c;
}
a = (a * a) % c;
b >>= 1;
}
return res;
}
```
其中,变量a、b、c分别表示底数、指数、模数。快速幂算法的基本思路是将底数不断平方,将指数二进制表示中每一位所对应的幂分别计算出来,再将其乘起来,最终就能得到a^b mod c的结果。
相关问题
快速幂取模算法java
快速幂取模算法是一种用于计算幂取模的高效算法,它可以在O(logn)的时间复杂度内完成计算。在Java中,可以使用以下代码实现快速幂取模算法:
```java
public static int fastPowMod(int a, int b, int c) {
int ans = 1;
a %= c;
while (b > 0) {
if ((b & 1) == 1) {
ans = (ans * a) % c;
}
b >>= 1;
a = (a * a) % c;
}
return ans;
}
```
其中,a、b、c分别为底数、指数和模数,返回值为a^b mod c的结果。该算法的基本思想是将指数b转化为二进制形式,然后根据二进制位上的值来计算幂的乘积,最终得到结果。
用Java模拟diffie-hellman算法
好的,下面是用 Java 实现 Diffie-Hellman 算法的代码示例:
```java
import java.math.BigInteger;
import java.security.SecureRandom;
public class DiffieHellman {
private static final BigInteger P = new BigInteger("FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF", 16);
private static final BigInteger G = BigInteger.valueOf(2);
private static final SecureRandom random = new SecureRandom();
public static void main(String[] args) {
// Alice generates a random secret number a
BigInteger a = new BigInteger(P.bitLength(), random).mod(P.subtract(BigInteger.ONE)).add(BigInteger.ONE);
System.out.println("Alice's secret number: " + a);
// Bob generates a random secret number b
BigInteger b = new BigInteger(P.bitLength(), random).mod(P.subtract(BigInteger.ONE)).add(BigInteger.ONE);
System.out.println("Bob's secret number: " + b);
// Alice calculates A = G^a mod P
BigInteger A = G.modPow(a, P);
// Bob calculates B = G^b mod P
BigInteger B = G.modPow(b, P);
// Alice and Bob exchange their public keys A and B
System.out.println("Alice's public key: " + A);
System.out.println("Bob's public key: " + B);
// Alice calculates the shared secret s = B^a mod P
BigInteger sAlice = B.modPow(a, P);
System.out.println("Alice's shared secret: " + sAlice);
// Bob calculates the shared secret s = A^b mod P
BigInteger sBob = A.modPow(b, P);
System.out.println("Bob's shared secret: " + sBob);
}
}
```
在这个示例中,我们使用了 Java 的 `BigInteger` 类来处理大整数运算,使用了 `SecureRandom` 类生成随机数。
首先,Alice 和 Bob 分别生成一个随机的秘密数 `a` 和 `b`。
然后,Alice 计算出公钥 `A = G^a mod P`,Bob 计算出公钥 `B = G^b mod P`,然后他们将公钥交换。
最后,Alice 和 Bob 分别使用自己的秘密数和对方的公钥计算出共享秘密 `s`,并输出结果。
需要注意的是,这个示例中使用了一个非常大的质数 `P`,以确保安全性。实际使用时,需要根据实际情况选择合适的参数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)