Add unit test for RSA cipher (#3664)
This commit is contained in:
parent
f8897f166d
commit
8c6ed9c240
@ -2,32 +2,15 @@ package com.thealgorithms.ciphers;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.SecureRandom;
|
||||
import javax.swing.*;
|
||||
|
||||
/**
|
||||
* @author Nguyen Duy Tiep on 23-Oct-17.
|
||||
*/
|
||||
public final class RSA {
|
||||
public class RSA {
|
||||
|
||||
public static void main(String[] args) {
|
||||
RSA rsa = new RSA(1024);
|
||||
String text1 = JOptionPane.showInputDialog(
|
||||
"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;
|
||||
private BigInteger modulus;
|
||||
private BigInteger privateKey;
|
||||
private BigInteger publicKey;
|
||||
|
||||
public RSA(int bits) {
|
||||
generateKeys(bits);
|
||||
@ -77,10 +60,10 @@ public final class RSA {
|
||||
BigInteger m =
|
||||
(p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
|
||||
|
||||
publicKey = new BigInteger("3");
|
||||
publicKey = BigInteger.valueOf(3L);
|
||||
|
||||
while (m.gcd(publicKey).intValue() > 1) {
|
||||
publicKey = publicKey.add(new BigInteger("2"));
|
||||
publicKey = publicKey.add(BigInteger.valueOf(2L));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user