本文介绍了在Java中使用RSA算法加密String的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试采用RSA算法加密String对象,但是似乎BigInteger - > String和String - > BigInteger转换无法正常工作。这是我的代码:

  public class RSAEncryptor {

private BigInteger n,d,e;

public RSAEncryptor(int bitlen){
SecureRandom r = new SecureRandom();
BigInteger p = new BigInteger(bitlen / 2,100,r);
BigInteger q = new BigInteger(bitlen / 2,100,r);
n = p.multiply(q);
BigInteger m =(p.subtract(BigInteger.ONE))。乘法(q.subtract(BigInteger.ONE));
e = new BigInteger(3);
while(m.gcd(e).intValue()> 1){
e = e.add(new BigInteger(2));
}
d = e.modInverse(m);
}

public String encrypt(String message){
BigInteger plaintext = new BigInteger(message.getBytes());
return new String(plaintext.modPow(e,n).toByteArray());
}

public String decrypt(String message){
BigInteger plaintext = new BigInteger(message.getBytes());
return new String(plaintext.modPow(d,n).toByteArray());
}
}

public class Main {

/ **
* @param args命令行参数
* /
public static void main(String [] args){
RSAEncryptor encryptor = new RSAEncryptor(64);

String source =1;
String crypted = encryptor.encrypt(source);
System.out.println(crypted);
String decryptpted = encryptor.decrypt(crypted);
System.out.println(解密);
}
}

它不打印什么是预期的,一个奇怪的事情是每次输出不同的时候。我做错了吗?谢谢

解决方案

加密的邮件是一个任意的 byte [] 确保它可以正确转换为具有特定字符编码的 String ,因此不需要将其转换为 String



另一个窍门是 plaintext 应该小于 n ,否则算法会产生垃圾。当这个条件满足时,它对我来说可以正常工作。



另一个可能的问题是当消息的第一个字节大于或等于将生成0x80 ,否定的明文。它可以通过在将消息的字节转换为 BigInteger 之前将消息的字节添加一个零字节来解决,并在逆转换期间剥离该字节。


I'm trying to adopt RSA algorithm for encrypting String objects, but seems that BigInteger -> String and String -> BigInteger conversions do not work properly. Here's my code:

public class RSAEncryptor {

    private BigInteger n, d, e;

    public RSAEncryptor(int bitlen) {
        SecureRandom r = new SecureRandom();
        BigInteger p = new BigInteger(bitlen / 2, 100, r);
        BigInteger q = new BigInteger(bitlen / 2, 100, r);
        n = p.multiply(q);
        BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
        e = new BigInteger("3");
        while (m.gcd(e).intValue() > 1) {
            e = e.add(new BigInteger("2"));
        }
        d = e.modInverse(m);
    }

    public String encrypt(String message) {
        BigInteger plaintext = new BigInteger(message.getBytes());
        return new String(plaintext.modPow(e, n).toByteArray());
    }

    public String decrypt(String message) {
        BigInteger plaintext = new BigInteger(message.getBytes());
        return new String(plaintext.modPow(d, n).toByteArray());
    }
}

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        RSAEncryptor encryptor = new RSAEncryptor(64);

        String source = "1";
        String crypted = encryptor.encrypt(source);
        System.out.println(crypted);
        String decrypted = encryptor.decrypt(crypted);
        System.out.println(decrypted);
    }
}

It prints not what is expected, and a strange thing is that every time the output differs. Am I doing something wrong? Thanks

解决方案

Encrypted message is an arbitrary byte[], it's not guaranteed that it can be correctly converted to String with particular character encoding, so there is no need to convert it to String.

Another trick is that plaintext should be less than n, otherwise algorithm would produce garbage. When this condition is met, it works fine for me.

Yet another possible problem is that when the first byte of message in greater or equal than 0x80, a negative plaintext would be produced. It can be solved by prepending a zero byte to the bytes of message before converting it to BigInteger, and stripping that byte during inverse conversion.

这篇关于在Java中使用RSA算法加密String的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 14:30