mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-10 13:39:08 +08:00
Add unit test for RSA cipher (#3664)
This commit is contained in:

committed by
GitHub

parent
f8897f166d
commit
8c6ed9c240
@ -2,32 +2,15 @@ package com.thealgorithms.ciphers;
|
|||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import javax.swing.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Nguyen Duy Tiep on 23-Oct-17.
|
* @author Nguyen Duy Tiep on 23-Oct-17.
|
||||||
*/
|
*/
|
||||||
public final class RSA {
|
public class RSA {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
private BigInteger modulus;
|
||||||
RSA rsa = new RSA(1024);
|
private BigInteger privateKey;
|
||||||
String text1 = JOptionPane.showInputDialog(
|
private BigInteger publicKey;
|
||||||
"Enter a message to encrypt :"
|
|
||||||
);
|
|
||||||
|
|
||||||
String ciphertext = rsa.encrypt(text1);
|
|
||||||
JOptionPane.showMessageDialog(
|
|
||||||
null,
|
|
||||||
"Your encrypted message : " + ciphertext
|
|
||||||
);
|
|
||||||
|
|
||||||
JOptionPane.showMessageDialog(
|
|
||||||
null,
|
|
||||||
"Your message after decrypt : " + rsa.decrypt(ciphertext)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private BigInteger modulus, privateKey, publicKey;
|
|
||||||
|
|
||||||
public RSA(int bits) {
|
public RSA(int bits) {
|
||||||
generateKeys(bits);
|
generateKeys(bits);
|
||||||
@ -77,10 +60,10 @@ public final class RSA {
|
|||||||
BigInteger m =
|
BigInteger m =
|
||||||
(p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
|
(p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
|
||||||
|
|
||||||
publicKey = new BigInteger("3");
|
publicKey = BigInteger.valueOf(3L);
|
||||||
|
|
||||||
while (m.gcd(publicKey).intValue() > 1) {
|
while (m.gcd(publicKey).intValue() > 1) {
|
||||||
publicKey = publicKey.add(new BigInteger("2"));
|
publicKey = publicKey.add(BigInteger.valueOf(2L));
|
||||||
}
|
}
|
||||||
|
|
||||||
privateKey = publicKey.modInverse(m);
|
privateKey = publicKey.modInverse(m);
|
||||||
|
24
src/test/java/com/thealgorithms/ciphers/RSATest.java
Normal file
24
src/test/java/com/thealgorithms/ciphers/RSATest.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package com.thealgorithms.ciphers;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
class RSATest {
|
||||||
|
|
||||||
|
RSA rsa = new RSA(1024);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRSA() {
|
||||||
|
// given
|
||||||
|
String textToEncrypt = "Such secure";
|
||||||
|
|
||||||
|
// when
|
||||||
|
String cipherText = rsa.encrypt(textToEncrypt);
|
||||||
|
String decryptedText = rsa.decrypt(cipherText);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals("Such secure", decryptedText);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user