Skip to content

Instantly share code, notes, and snippets.

@mobynote
Created March 30, 2018 06:07
Show Gist options
  • Save mobynote/4218ffd8fa5c94263050b73b36958ea2 to your computer and use it in GitHub Desktop.
Save mobynote/4218ffd8fa5c94263050b73b36958ea2 to your computer and use it in GitHub Desktop.
Test RSA encryption and decryption
package com.moby.security;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.time.StopWatch;
import org.junit.Test;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.concurrent.TimeUnit;
public class TokenTransferEncryptorTest {
@Test
public void check_time_for_rsa() throws Exception {
String algorithm_rsa = "RSA";
String source = UuidHelper.uuid32();
KeyPair keyPair = this.getKeyPair(algorithm_rsa);
StopWatch stopWatch = StopWatch.createStarted();
for (int i = 0; i < 1; i++) {
encryptAndDecrypt(keyPair);
}
stopWatch.stop();
System.out.println("Cost Time: " + stopWatch.getTime(TimeUnit.MILLISECONDS));
}
@Test
public void check_time_for_encrypt_and_decrypt() {
}
private KeyPair getKeyPair(String algorithm_rsa) throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm_rsa);
keyPairGenerator.initialize(2048);
return keyPairGenerator.generateKeyPair();
}
private void encryptAndDecrypt(KeyPair keyPair) throws Exception {
String algorithm_rsa = "RSA";
String source = UuidHelper.uuid32();
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyPair.getPrivate().getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance(algorithm_rsa);
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance(algorithm_rsa);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] cipherBytes = cipher.doFinal(source.getBytes());
String cipherText = Base64.encodeBase64String(cipherBytes);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded());
keyFactory = KeyFactory.getInstance(algorithm_rsa);
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
cipher = Cipher.getInstance(algorithm_rsa);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] plainBytes = cipher.doFinal(Base64.decodeBase64(cipherText));
String plainText = new String(plainBytes);
}
private void encrypt(Cipher cipher, String source) throws BadPaddingException, IllegalBlockSizeException {
byte[] cipherBytes = cipher.doFinal(source.getBytes());
String cipherText = Base64.encodeBase64String(cipherBytes);
}
private void decrypt(Cipher cipher, String cipherText) throws BadPaddingException, IllegalBlockSizeException {
byte[] plainBytes = cipher.doFinal(Base64.decodeBase64(cipherText));
String plainText = new String(plainBytes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment