【问题标题】:I have written a Java code for RSA encryption and decryption where the decryption key is too large so the decryption process takes forever to execute我为 RSA 加密和解密编写了一个 Java 代码,其中解密密钥太大,因此解密过程需要永远执行
【发布时间】:2019-08-16 19:10:39
【问题描述】:

我必须根据我对程序的要求使用p=78511和q=5657,代码执行没有错误,但我因为dec_key的值太大,它不显示解密的文本,继续运行。我该如何解决这个问题?有没有办法让 dec_key 更小,或者我做的解密方法都错了。在这里,我现在尝试在加密方法中传递一个字符“H”。 附上代码。 请不要阻止我的问题。我是新来的,不太确定如何提问,只是让我知道我错在哪里。谢谢!

package crypto.assgn4;  
import static crypto.assgn4.Problem2.phi;
import java.math.BigInteger;
class Test {

static char[] characters = {' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
static BigInteger p = BigInteger.valueOf(78511);
static BigInteger q = BigInteger.valueOf(5657);
static BigInteger N = p.multiply(q);
static BigInteger phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
static BigInteger e = BigInteger.ZERO, d;

public static void main(String args[]) {

    e = new BigInteger("4");
    while ((gcd(phi, e).intValue()>1)) {
        e = e.add(new BigInteger("1"));
    }

    d = BigInteger.valueOf(mul_inverse(e, phi));
    if (d.equals(e)) {
        d.add(phi);
    }

    System.out.println("Encryption Key : "+e);
    System.out.println("Decryption Key : "+d);

    String c = encrypt("H",e,N);
    String p = decrypt(c,d,N);

    System.out.println("Cipher : "+c);
    System.out.println("Text : " +p);


}

public static BigInteger gcd(BigInteger a, BigInteger b) {
    while (b != BigInteger.ZERO) {
        BigInteger temp = b;
        b = a.mod(b);
        a = temp;
    }
    return a;
}


public static int mul_inverse(BigInteger number, BigInteger sizeOfAlphabet) {
    int a = number.intValue() % sizeOfAlphabet.intValue();
    for (int x = 1; x < sizeOfAlphabet.intValue(); x++) {
        if ((a * x) % sizeOfAlphabet.intValue() == 1) {
            return getMod(x, sizeOfAlphabet.intValue());
        }
    }
    return -1;
}


public static int getMod(int x, int y) {
    int result = x % y;
    if (result < 0) {
        result += y;
    }
    return result;
}

/**
 * ********************************************************************************
 */
static String encrypt(String plainText, BigInteger e, BigInteger N) {
    StringBuilder cipherText = new StringBuilder();

    for (int i = 0; i < plainText.length(); i++) {
        int index = plainText.charAt(i);
        cipherText.append("").append((char) (new BigInteger(index + "").pow(e.intValue()).mod(N).intValue()));
        char c1 = (char) (new BigInteger(index + "").intValue());
    }
    return cipherText.toString();
}

static String decrypt(String cipherText, BigInteger d, BigInteger N) {

    String plainText = "";
    for (int i = 0; i < cipherText.length(); i++) {
        int index = cipherText.charAt(i);
        plainText += "" + (char) (new BigInteger(index + "").pow(d.intValue()).mod(N).intValue());

    }
    return plainText;
}

}

【问题讨论】:

  • 你为什么要使用.intValue()?除了使您的代码不那么正确和难以阅读之外,它对您没有任何作用。
  • (1) 您的mul_inverse 比必要的慢数百万倍;通常乘以 29 位数字也不适合 Java int 但你的 e 总是很小所以这可能不会命中 (2) 你不需要将 int 转换为十进制 String BigInteger 的方法,只需使用 BigInteger.valueOf(int) (3) 特别是对于 d,执行 BigInteger.pow 完成 then mod 比优化组合 @ 花费 much 更长的时间987654331@ (4) 对于提供任何安全性的 RSA 大小,您的代码将永远无法完成 (5) 请参阅回复char 的评论

标签: java encryption rsa


【解决方案1】:

看起来你的加密/解密都错了。

RSA 的要点是获取字符串编码的整个位模式,并将其视为本身就是一个 BigNumber(例如 BigInteger)。 (注意:如果 BigNumber 为 > 模,则字符串必须分块,以便 BigNumber 变为

您在逐个字符的基础上所做的事情是不必要的矫枉过正,也可能是完全错误的,显然是长时间运行的根源。 (加密您的单字符串可能仍然很顺利,因为即使在逐个字符的基础上,您也只会进行一次加密。但它会产生一个长度为 x 的字符串,然后解密将执行 x BigInteger 计算,这将不可避免地需要更长的时间。)

【讨论】:

  • 实际上 RSA 很少用于直接数据,通常用于“混合”模式,仅加密用于对称加密(和解密)数据的随机数密钥。并且 RSA 加密必须使用随机填充来防止对手简单地猜测,或者在这里找到欺骗。这两个在维基百科中都有很好的介绍。但是 OP 实际上会尝试将每个 char(在 int 中)加密为 one char,这对于 29 位 N 根本不起作用。
猜你喜欢
  • 2018-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多