Last active
April 26, 2022 07:31
-
-
Save timpamungkas/457f152443e77818d0b18902c317c96f to your computer and use it in GitHub Desktop.
Cryptography service for AES / RSA encryption / decryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.secure; | |
import java.io.UnsupportedEncodingException; | |
import java.nio.charset.StandardCharsets; | |
import java.security.InvalidAlgorithmParameterException; | |
import java.security.InvalidKeyException; | |
import java.security.KeyFactory; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.PrivateKey; | |
import java.security.PublicKey; | |
import java.security.Security; | |
import java.security.spec.PKCS8EncodedKeySpec; | |
import java.security.spec.X509EncodedKeySpec; | |
import java.util.Base64; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import org.bouncycastle.jce.provider.BouncyCastleProvider; | |
import org.springframework.stereotype.Service; | |
@Service | |
public class CryptographyService { | |
private static final String CIPHER_ALGORITHM = "RSA"; | |
private static final String AES_ALGORITHM = "AES/CBC/PKCS7Padding"; | |
private static final IvParameterSpec IV = generateIv(); | |
public static IvParameterSpec generateIv() { | |
// no IV | |
final byte[] ivBytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; | |
return new IvParameterSpec(ivBytes); | |
} | |
private Cipher cipher; | |
public CryptographyService() throws NoSuchAlgorithmException, NoSuchPaddingException { | |
this.cipher = Cipher.getInstance(CIPHER_ALGORITHM); | |
} | |
public String decryptAES(String cipherText, String secretKey) | |
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, | |
InvalidKeyException, BadPaddingException, IllegalBlockSizeException { | |
Security.addProvider(new BouncyCastleProvider()); | |
final var cipher = Cipher.getInstance(AES_ALGORITHM); | |
final var secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "AES"); | |
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, IV); | |
final var plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText)); | |
return new String(plainText); | |
} | |
public String decryptRSA(String msg, PrivateKey key) | |
throws InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException { | |
this.cipher.init(Cipher.DECRYPT_MODE, key); | |
return new String(cipher.doFinal(Base64.getDecoder().decode(msg)), StandardCharsets.UTF_8); | |
} | |
public String encryptAES(String original, String secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, | |
IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { | |
Security.addProvider(new BouncyCastleProvider()); | |
final var cipher = Cipher.getInstance(AES_ALGORITHM); | |
final var secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "AES"); | |
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, IV); | |
return Base64.getEncoder().encodeToString(cipher.doFinal(original.getBytes(StandardCharsets.UTF_8))); | |
} | |
public String encryptRSA(String msg, PublicKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, | |
UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException { | |
this.cipher.init(Cipher.ENCRYPT_MODE, key); | |
return Base64.getEncoder().encodeToString(cipher.doFinal(msg.getBytes(StandardCharsets.UTF_8))); | |
} | |
public PrivateKey getRSAPrivateFromBase64(String rsaPrivateKeyBase64) throws Exception { | |
var keyBytes = Base64.getDecoder().decode(rsaPrivateKeyBase64); | |
var spec = new PKCS8EncodedKeySpec(keyBytes); | |
var kf = KeyFactory.getInstance(CIPHER_ALGORITHM); | |
return kf.generatePrivate(spec); | |
} | |
public PublicKey getRSAPublicFromBase64(String rsaPublicKeyBase64) throws Exception { | |
var keyBytes = Base64.getDecoder().decode(rsaPublicKeyBase64); | |
var spec = new X509EncodedKeySpec(keyBytes); | |
var kf = KeyFactory.getInstance(CIPHER_ALGORITHM); | |
return kf.generatePublic(spec); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment