Commit 5ab50cfc authored by Lakshay Thakur's avatar Lakshay Thakur
Browse files

intital commit

parents
Loading
Loading
Loading
Loading

pom.xml

0 → 100644
+33 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>encryption-decryption-util</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.15</version>
        </dependency>

        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.70</version>
        </dependency>


    </dependencies>

</project>
 No newline at end of file
+8 −0
Original line number Diff line number Diff line
package com.leegality.encryptionDecryptionUtil;

public class EncryptionDecryptionUtil {
    public static void main(String[] args) throws Exception {


    }
}
+35 −0
Original line number Diff line number Diff line
package com.leegality.encryptionDecryptionUtil.crypto;

import com.leegality.encryptionDecryptionUtil.util.EncryptionUtil;
import com.leegality.encryptionDecryptionUtil.util.SignatureUtil;
import com.leegality.encryptionDecryptionUtil.vo.EncryptDecryptResponseVO;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.PrivateKey;
import java.util.Arrays;
import java.util.Base64;

public class Decrypt {

    private final String algorithm = "AES/CBC/PKCS5Padding";

    public EncryptDecryptResponseVO decrypt(String payload, String salt, String pfxFilePath, String password) throws IOException, GeneralSecurityException {

        pfxFilePath = System.getProperty("user.dir") + pfxFilePath;
        PrivateKey privateKey = SignatureUtil.getPrivateKey(pfxFilePath, password);

        String key = EncryptionUtil.decrypt(salt, privateKey);
        byte[] keyByte = Base64.getDecoder().decode(key);
        byte[] actKey = Arrays.copyOfRange(keyByte, 0, 32);
        byte[] iv = Arrays.copyOfRange(keyByte, 32, 48);

        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(actKey, algorithm), new IvParameterSpec(iv));
        byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(payload));
        return new EncryptDecryptResponseVO(new String(plainText), null);
    }
}
+40 −0
Original line number Diff line number Diff line
package com.leegality.encryptionDecryptionUtil.crypto;

import com.leegality.encryptionDecryptionUtil.key.Key;
import com.leegality.encryptionDecryptionUtil.util.EncryptionUtil;
import com.leegality.encryptionDecryptionUtil.util.SignatureUtil;
import com.leegality.encryptionDecryptionUtil.vo.EncryptDecryptResponseVO;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.PublicKey;
import java.util.Base64;

public class Encrypt {

    private final String algorithm = "AES/CBC/PKCS5Padding";

    public EncryptDecryptResponseVO encrypt(String payloadPath, String publicKeyPath) throws Exception {
        if (payloadPath == null || publicKeyPath == null) {
            throw new Exception("Both payload Path and public key are required.");
        }

        byte[] inputBytes = Files.readAllBytes(Path.of(System.getProperty("user.dir") + payloadPath));
        String keyPath = System.getProperty("user.dir") + publicKeyPath;

        Key key = new Key();
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getKey(), key.getKeyAlgo()),
                new IvParameterSpec(key.getIv()));

        byte[] cipherText = cipher.doFinal(inputBytes);

        String salt = Base64.getEncoder().encodeToString(key.getEncoded());

        PublicKey publicKey = SignatureUtil.getPublicKey(keyPath);
        return new EncryptDecryptResponseVO(Base64.getEncoder().encodeToString(cipherText), EncryptionUtil.encrypt(salt, publicKey));
    }
}
+12 −0
Original line number Diff line number Diff line
package com.leegality.encryptionDecryptionUtil.exception;

public class SignatureException extends RuntimeException {

    public SignatureException(String message) {
        super(message);
    }

    public SignatureException(Throwable cause) {
        super(cause);
    }
}
Loading