This is an example of Java implementation of encryption with RSA encryption. Two patterns are implemented: encryption with private key → decryption with public key, encryption with public key → decryption with private 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;
public class Main {
	public static void main(String[] args) throws Exception {
		//Generate public / private key.
		KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA");
		kg.initialize(1024);
		KeyPair keyPair = kg.generateKeyPair();
		KeyFactory factoty = KeyFactory.getInstance("RSA");
		RSAPublicKeySpec publicKeySpec = factoty.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class);
		RSAPrivateKeySpec privateKeySpec = factoty.getKeySpec(keyPair.getPrivate(), RSAPrivateKeySpec.class);
		PublicKey publicKey = factoty.generatePublic(publicKeySpec);
		PrivateKey privateKey = factoty.generatePrivate(privateKeySpec);
        //Plaintext
		byte[] plain = new byte[] { 0x01, 0x02 };
        //Encrypt with private key and decrypt with public key.
		Cipher encrypterWithPrivateKey = Cipher.getInstance("RSA/ECB/PKCS1Padding");
		encrypterWithPrivateKey.init(Cipher.ENCRYPT_MODE, privateKey);
		byte[] encryptedWithPrivateKey = encrypterWithPrivateKey.doFinal(plain);
	    System.out.println("encryptedWithPrivateKey (" + encryptedWithPrivateKey.length + " bytes):");
		System.out.println(decodeHex(encryptedWithPrivateKey));
		Cipher decrypterWithPublicKey = Cipher.getInstance("RSA/ECB/PKCS1Padding");
		decrypterWithPublicKey.init(Cipher.DECRYPT_MODE, publicKey);
		byte[] decryptedWithPrivateKey = decrypterWithPublicKey.doFinal(encryptedWithPrivateKey);
	    System.out.println("decryptedWithPrivateKey (" + decryptedWithPrivateKey.length + " bytes):");
		System.out.println(decodeHex(decryptedWithPrivateKey));
		//Encrypt with public key and decrypt with private key.
		Cipher encrypterWithPublicKey = Cipher.getInstance("RSA/ECB/PKCS1Padding");
		encrypterWithPublicKey.init(Cipher.ENCRYPT_MODE, publicKey);
		byte[] encryptedWithPublicKey = encrypterWithPublicKey.doFinal(plain);
	    System.out.println("encryptedWithPublicKey (" + encryptedWithPublicKey.length + " bytes):");
		System.out.println(decodeHex(encryptedWithPublicKey));
		
		Cipher decrypterWithPriavteKey = Cipher.getInstance("RSA/ECB/PKCS1Padding");
		decrypterWithPriavteKey.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] decryptedWithPriavteKey = decrypterWithPriavteKey.doFinal(encryptedWithPublicKey);
	    System.out.println("decryptedWithPriavteKey (" + decryptedWithPriavteKey.length + " bytes):");
		System.out.println(decodeHex(decryptedWithPriavteKey));
	}
	/**Convert the byte array to hexadecimal notation.*/
	public static String decodeHex(byte[] bytes) {
		StringBuilder sb = new StringBuilder();
		for (byte b : bytes) {
			sb.append(String.format("%02x", b));
		}
        return sb.toString();
	}
}
Execute with paiza.io.
Recommended Posts