Skip to content

Instantly share code, notes, and snippets.

@inancsevinc
Created July 10, 2012 09:30
Show Gist options
  • Save inancsevinc/3082293 to your computer and use it in GitHub Desktop.
Save inancsevinc/3082293 to your computer and use it in GitHub Desktop.
Junit for RSA encryption/decryption
<!-- ..... -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
<!-- ..... -->
import static org.junit.Assert.*;
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
public class TestRSA {
private static final String STRING_TO_ENCRYPT = "test string";
private static final String CHAR_ENCODING = "UTF-8";
private static final String PUBLIC_KEY_MODULUS = "114148583453528745752158573299299462974849455803660656482072552191881163973185343560593952047234554690302302264823153536655488960387116585505622616432041543078622970397250697451778064891543319594178153797228157167683013081933196126650429380296544727748466019924362570851520110617956047194419951742367399790853";
private static final String PUBLIC_KEY_EXPONENT = "65537";
private static final String PRIVATE_KEY_MODULUS = "114148583453528745752158573299299462974849455803660656482072552191881163973185343560593952047234554690302302264823153536655488960387116585505622616432041543078622970397250697451778064891543319594178153797228157167683013081933196126650429380296544727748466019924362570851520110617956047194419951742367399790853";
private static final String PRIVATE_KEY_EXPONENT = "33298634640959497097666472440545144470347618842095067376051132227663519734491316328050340650915211507533140282571509976254324090890350410938805456169000256520741194286946780602454621083327057643032729590642843725780625377381384749342388096938198107146927109759526548861172195154004923472826081212789832947265";
@Test
public void testEncryptDecrypt() throws Throwable {
KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(new BigInteger(
PUBLIC_KEY_MODULUS), new BigInteger(PUBLIC_KEY_EXPONENT));
PublicKey pubKey = fact.generatePublic(publicKeySpec);
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(
new BigInteger(PRIVATE_KEY_MODULUS), new BigInteger(
PRIVATE_KEY_EXPONENT));
PrivateKey privKey = fact.generatePrivate(privateKeySpec);
byte[] cipherData = null;
Cipher cipher = null;
try {
System.out.println("String to encrypt: " + STRING_TO_ENCRYPT);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
cipherData = cipher.doFinal(STRING_TO_ENCRYPT.getBytes(CHAR_ENCODING));
String encryptedString = Base64
.encodeBase64URLSafeString(cipherData);
System.out.println("String after encryption: " + encryptedString);
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] decryptedData = cipher.doFinal(Base64
.decodeBase64(encryptedString));
String decryptedString = new String(decryptedData, CHAR_ENCODING);
System.out.println("String after decryption: " + decryptedString);
assertEquals("decrypted stinrg is wrong!!",STRING_TO_ENCRYPT,decryptedString);
} catch (Exception e1) {
System.out.println("error: " + e1.getMessage());
}
}
@Test
public void generateKeys() throws Exception {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair kp = kpg.genKeyPair();
Key publicKey = kp.getPublic();
Key privateKey = kp.getPrivate();
KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pub = (RSAPublicKeySpec) fact.getKeySpec(publicKey,
RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = (RSAPrivateKeySpec) fact.getKeySpec(
privateKey, RSAPrivateKeySpec.class);
System.out.println("Public key Modulus: " + pub.getModulus());
System.out.println("Public key Exponent: " + pub.getPublicExponent());
System.out.println("Private key Modulus: " + priv.getModulus());
System.out
.println("Private key Exponent: " + priv.getPrivateExponent());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment