簡體   English   中英

如何使用RSA方法加密/解密文本?

[英]How to encrypt / decrypt a text by using RSA method?

我有一個簡單的Java代碼,可以使用RSA算法對數字進行加密和解密

如果有人可以幫助我使此代碼從用戶讀取文本(字符串)並解密,而不是僅對數字解密,但方式很簡單,那么我可以在之后為代碼繪制流程圖:)

https://codedost.com/css/java-program-rsa-algorithm/

import java.util.*;
import java.math.*;

public class RSA {

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    int p, q, n, z, d = 0, e, i;
    System.out.println("Enter the number to be encrypted and decrypted");
    int msg = sc.nextInt();
    double c;
    BigInteger msgback;
    System.out.println("Enter 1st prime number p");
    p = sc.nextInt();
    System.out.println("Enter 2nd prime number q");
    q = sc.nextInt();

    n = p * q;
    z = (p - 1) * (q - 1);
    System.out.println("the value of z = " + z);

    for (e = 2; e < z; e++) {
        if (gcd(e, z) == 1) // e is for public key exponent
        {
            break;
        }
    }
    //e should be in the range 1-z
    System.out.println("the value of e = " + e);

    // calculate d
    for (i = 0; i <= 9; i++) {
        int x = 1 + (i * z);
        if (x % e == 0) //d is for private key exponent
        {
            d = x / e;
            break;
        }
    }
    System.out.println("the value of d = " + d);
    c = (Math.pow(msg, e)) % n;
    //Encryptin  C = msg ^e mod n
    System.out.println("Encrypted message is : -");
    System.out.println(c);


    //converting int value of n to BigInteger
    BigInteger N = BigInteger.valueOf(n);
    //converting float value of c to BigInteger
    BigInteger C = BigDecimal.valueOf(c).toBigInteger();

    msgback = (C.pow(d)).mod(N);
    //Decrypt , P = Cˆd mod N , msgback = P
    System.out.println("Derypted message is : -");
    System.out.println(msgback);

}
static int gcd(int e, int z) {
    if (e == 0) {
        return z;
    } else {
        return gcd(z % e, e);
    }
}
}

由於您已經為單個號碼實現了加密和解密,因此您可以輕松擴展它並為更長的消息提供支持。 實際上,您唯一需要做的更改就是對N次執行相同的操作(對於輸入消息中的每個字符)。 看看下面的代碼:



import java.util.*;
import java.math.*;

public class Rsa {

    private static final Scanner sc = new Scanner(System.in);

    private int p, q, n, z, d = 0, e, i;

    public Rsa() {
        System.out.println("Enter 1st prime number p");
        p = sc.nextInt();
        System.out.println("Enter 2nd prime number q");
        q = sc.nextInt();

        n = p * q;
        z = (p - 1) * (q - 1);
        System.out.println("the value of z = " + z);

        for (e = 2; e < z; e++) {
            if (gcd(e, z) == 1) // e is for public key exponent
            {
                break;
            }
        }
        //e should be in the range 1-z
        System.out.println("the value of e = " + e);

        // calculate d
        for (i = 0; i <= 9; i++) {
            int x = 1 + (i * z);
            if (x % e == 0) //d is for private key exponent
            {
                d = x / e;
                break;
            }
        }
        System.out.println("the value of d = " + d);
    }

    private static int gcd(int e, int z) {
        if (e == 0) {
            return z;
        } else {
            return gcd(z % e, e);
        }
    }

    double encrypt(int msg) {
        //Encrypting  C = msg ^e mod n
        return (Math.pow(msg, e)) % n;
    }

    double[] encrypt(String msg) {
        int[] charactersAsNumbers = new int[msg.length()];
        for(int i = 0; i < msg.length(); i++) {
            charactersAsNumbers[i] = msg.codePointAt(i);
        }
        System.out.println("Plain text as sequence of numbers: " + Arrays.toString(charactersAsNumbers));

        double[] encryptedMsg = new double[msg.length()];
        for(int i = 0; i < charactersAsNumbers.length; i++) {
            encryptedMsg[i] = encrypt(charactersAsNumbers[i]);
        }
        return encryptedMsg;
    }

    BigInteger decrypt(double encrypted) {
        //converting int value of n to BigInteger
        BigInteger N = BigInteger.valueOf(n);
        //converting float value of c to BigInteger
        BigInteger C = BigDecimal.valueOf(encrypted).toBigInteger();

        //Decrypt , P = Cˆd mod N , msgback = P
        return (C.pow(d)).mod(N);
    }

    String decrypt(double[] encrypted) {
        StringBuilder builder = new StringBuilder();
        for(double encryptedCharacter: encrypted) {
            BigInteger decryptedCharacter = decrypt(encryptedCharacter);
            builder.append(Character.toChars(decryptedCharacter.intValue()));
        }
        return builder.toString();
    }

    public static void main(String args[]) {
        System.out.println("Enter the text to be encrypted and decrypted");
        String msg = sc.nextLine();
        Rsa rsa = new Rsa();

        double[] c = rsa.encrypt(msg);
        System.out.println("Encrypted message is: " + Arrays.toString(c));

        String msgBack = rsa.decrypt(c);
        System.out.println("Decrypted message is: " + msgBack);
    }
}

我在這里所做的是:

  • 重載的encryptdecrypt方法。 現在它們支持更長的消息; encrypt接受String參數並返回double[]decrypt接受double[]並返回String
  • 邏輯轉移到方法,而無需更改原始數據類型和一般流程

我知道給定的解決方案不是最佳解決方案,但在這種情況下,我認為性能和代碼風格對您而言並不關鍵。

希望它可以幫助您解決問題。

編輯:我稍微改善了日志,這是示例輸出(和輸入):

Enter the text to be encrypted and decrypted
Secret.
Enter 1st prime number p
13
Enter 2nd prime number q
19
the value of z = 216
the value of e = 5
the value of d = 173
Plain text as sequence of numbers: [83, 101, 99, 114, 101, 116, 46]
Encrypted message is: [239.0, 43.0, 112.0, 95.0, 43.0, 51.0, 50.0]
Decrypted message is: Secret.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM