com.seeq.utilities.encryption.SimpleRSA Maven / Gradle / Ivy
The newest version!
package com.seeq.utilities.encryption;
import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.apache.commons.io.FileUtils;
import lombok.extern.slf4j.Slf4j;
@Slf4j
final class SimpleRSA {
private SimpleRSA() {}
private static final String ALGORITHM = "RSA";
private static final String TRANSFORMATION = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";
/**
* Load a DER encoded PKCS8 private key file
*
* @param privateKeyFile
* a DER encoded PKCS8 private key file
* @return the loaded {@link PrivateKey}
* @throws IOException
* if an error occurs while reading the private key file
* @throws InvalidKeySpecException
* if the private key file does not contain a valid private key in PKCS8 format
*/
static PrivateKey loadPrivateKey(File privateKeyFile) throws IOException, InvalidKeySpecException {
try {
byte[] data = FileUtils.readFileToByteArray(privateKeyFile);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(data);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
return keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
/**
* Load a DER encoded X509 public key file
*
* @param publicKeyFile
* a DER encoded X509 public key file
* @return the loaded {@link PublicKey}
* @throws IOException
* if an error occurs while reading the public key file
* @throws InvalidKeySpecException
* if the public key file does not contain a valid public key in X509 format
*/
static PublicKey loadPublicKey(File publicKeyFile) throws IOException, InvalidKeySpecException {
try {
byte[] data = FileUtils.readFileToByteArray(publicKeyFile);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(data);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
/**
* Encrypt the data using the public key and the RSA algorithm with OAEPWithSHA-256AndMGF1 padding.
*
* @param publicKey
* a 1024 or 2048 bit RSA public key to encrypt the data with
* @param data
* the data to be encrypted
* @return the encrypted data
* @throws InvalidKeyException
* if the public key is not valid
*/
static byte[] encrypt(PublicKey publicKey, byte[] data) throws InvalidKeyException {
try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
} catch (BadPaddingException | IllegalBlockSizeException |
NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new RuntimeException(e);
}
}
/**
* Decrypt a file that was encrypted with {@link SimpleRSA#encrypt(PublicKey, byte[])}
*
* @param privateKey
* the private key corresponding to the public key that was used to encrypt the data
* @param data
* the data to decrypt
* @return the decrypted ata
* @throws BadPaddingException
* if the encrypted data does not have OAEPWithSHA-256AndMGF1 padding
* @throws IllegalBlockSizeException
* if the encrypted data cannot be processed
* @throws InvalidKeyException
* if the private key is not valid
*/
static byte[] decrypt(PrivateKey privateKey, byte[] data)
throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new RuntimeException(e);
}
}
}