java rsa biginteger_使用Java RSA加密和使用BigInteger解密

已解决

密码学Python

使用Java RSA加密和使用BigInteger解密649788f17101bff39e9643602f4d66a1.png10

所以我试图找出如何在java中加密和解密RSA。 使用javas API进行加密,使用biginteger进行解密。 我完成了但是biginteger有时会给我奇怪的输出。 这是我的代码:import java.math.BigInteger;

import java.security.InvalidKeyException;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.NoSuchAlgorithmException;

import java.security.NoSuchProviderException;

import java.security.Security;

import java.security.interfaces.RSAPrivateKey;

import java.security.interfaces.RSAPublicKey;

import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.NoSuchPaddingException;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class cryptoAndBigIntegerFIX {

public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException{

Security.addProvider(new BouncyCastleProvider());

System.out.println("input <> encrypted <> decrypted");

cryptoAndBigIntegerFIX.keygen();

BigInteger encryptbytes;

BigInteger decryptbytes;

//Multiple tests with powers of 3 for some reason :D

for(int i=1;i<1000;i*=3){

encryptbytes = cryptoAndBigIntegerFIX.encrypt(new BigInteger(""+i));

System.out.print(i + " <> " + encryptbytes.intValue() + " <> ");

decryptbytes = cryptoAndBigIntegerFIX.decrypt(encryptbytes);

System.out.println(decryptbytes.intValue());

}

}

public static RSAPrivateKey priv;

public static RSAPublicKey pub;

public static void keygen() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException{

KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");

generator.initialize(512);

KeyPair keyPair = generator.generateKeyPair();

priv = (RSAPrivateKey) keyPair.getPrivate();

pub = (RSAPublicKey) keyPair.getPublic();

}

//Encrypt with javas API

public static BigInteger encrypt(BigInteger bg) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException{

byte[] encoded;

Cipher cipher=Cipher.getInstance("RSA/ECB/NoPadding");

cipher.init(Cipher.ENCRYPT_MODE, pub);

encoded=cipher.doFinal(bg.toByteArray());

return new BigInteger(encoded);

}

//Decrypt manually

public static BigInteger decrypt(BigInteger bg){

BigInteger decoded = bg.modPow(priv.getPrivateExponent(),priv.getModulus());

return decoded;

}

}

这给出的输出是:input <> encrypted <> decrypted

1 <> 1 <> 1

3 <> 1088098617 <> 3

9 <> 1947497039 <> 9

27 <> -1665331145 <> 27

81 <> -1064046970 <> 81

243 <> -599005266 <> 243

729 <> -1534949160 <> 729

这是正确的,因为我试图加密和解密三个权力。 但有时它会给出错误的输出,例如:input <> encrypted <> decrypted

1 <> 1 <> 1

3 <> 1693488667 <> 3

9 <> -924345856 <> 9

27 <> 777525903 <> 144224668

81 <> -1602799071 <> 765474161

243 <> -227258229 <> 243

729 <> 1097077312 <> 296615835

这很奇怪? 知道我的代码有什么问题吗? 这是biginteger的问题还是我生成密钥的方式?

Rooney

2019.05.25

95fa9268de11d0061c77acb40a6f985f.png6408

3037648d6a74d46918a474e105002d34.png收藏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RSA加密解密算法是公钥加密算法的代表,常用于加密信息和数字签名。在Java中,我们可以使用Java内置的BigInteger类和一些基本数学算法来实现RSA加密解密算法,以下是一个简单的实现过程: 1. 生成公钥和私钥 ```java import java.math.BigInteger; import java.util.Random; public class RSAKeyGenerator { private final static BigInteger ONE = new BigInteger("1"); private final static Random random = new Random(); public static void main(String[] args) { BigInteger p = BigInteger.probablePrime(512, random); BigInteger q = BigInteger.probablePrime(512, random); BigInteger n = p.multiply(q); BigInteger phiN = p.subtract(ONE).multiply(q.subtract(ONE)); BigInteger e = new BigInteger("65537"); BigInteger d = e.modInverse(phiN); System.out.println("Public Key: (" + e + ", " + n + ")"); System.out.println("Private Key: (" + d + ", " + n + ")"); } } ``` 2. 加密数据 ```java import java.math.BigInteger; public class RSAEncryptor { public static String encrypt(String message, BigInteger e, BigInteger n) { byte[] bytes = message.getBytes(); BigInteger m = new BigInteger(bytes); BigInteger c = m.modPow(e, n); return c.toString(); } } ``` 3. 解密数据 ```java import java.math.BigInteger; public class RSADecryptor { public static String decrypt(String ciphertext, BigInteger d, BigInteger n) { BigInteger c = new BigInteger(ciphertext); BigInteger m = c.modPow(d, n); byte[] bytes = m.toByteArray(); return new String(bytes); } } ``` 以上是一个简单的Java实现RSA加密解密算法的过程,需要注意的是,RSA算法的安全性高度依赖于大素数的难以分解性,因此在实际应用中需要选取足够大的素数。另外,由于BigInteger类的运算速度较慢,因此在实际应用中需要考虑使用更高效的算法实现RSA加密解密

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值