简体   繁体   English

我编写了一个用于 RSA 加密和解密的 Java 代码,其中解密密钥太大,因此解密过程需要永远执行

[英]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

I have to use p=78511 and q= 5657 as per my requirement of the program, the code executes without errors but I as the value of dec_key is too large, it does not display the decrypted text, keeps on running.我必须按照我对程序的要求使用 p=78511 和 q=5657,代码执行没有错误,但我因为 dec_key 的值太大,它不显示解密的文本,继续运行。 How do I solve this?我该如何解决这个问题? Is there a way to have smaller dec_key or am I doing the decryption method all wrong.有没有办法让 dec_key 更小,或者我做的解密方法都错了。 Here I am trying to pass a character "H" in the encryption method for now.在这里,我现在尝试在加密方法中传递一个字符“H”。 Attaching the code.附上代码。 Please do not block my question.请不要屏蔽我的问题。 I am new here and not so sure how to ask questions, just let me know where I am wrong.我是新来的,不太确定如何提问,请告诉我我错在哪里。 Thanks!谢谢!

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;
}

}

It looks like you are doing the encryption/decryption all wrong.看起来你做的加密/解密都是错误的。

The point of RSA is to take the entire bit pattern of the encoding of the String, and treat that as if it were a BigNumber (eg BigInteger) in its own right. RSA 的要点是获取字符串编码的整个位模式,并将其视为一个 BigNumber(例如 BigInteger)。 (Note : if that BigNumber is > the modulo then the string has to be chunked in portions such that the BigNumber becomes < the modulo.) (注意:如果 BigNumber > 模数,那么字符串必须分成几部分,以便 BigNumber 变成 < 模数。)

What you are doing on a character by character basis is unnecessary overkill, probably plain wrong as well and obviously the source of long running times.您在逐个字符的基础上所做的事情是不必要的矫枉过正,也可能是完全错误的,并且显然是长时间运行的根源。 (Encrypting your single-character string might still go well because even on a character-by-character basis you'll do only one encryption. But it will yield a string of length x and then the decryption will do x BigInteger computations and that will inevitably take longer.) (加密您的单字符字符串可能仍然很顺利,因为即使在逐个字符的基础上,您也只会进行一次加密。但它会产生一个长度为 x 的字符串,然后解密将执行 x BigInteger 计算,这将不可避免地需要更长的时间。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM