Java
Chilkat2 Java
Duplicate openssl smime -decrypt -in some_file.dat.enc -binary -inform DER -inkey private.key -out some_file.dat
See more OpenSSL Examples
Demonstrates how to decrypt binary DER that was encrypted using the following openssl command:openssl smime -encrypt -binary -aes-256-cbc -in some_file.dat -out some_file.dat.enc -outform DER cert.crt
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.PrivateKey;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Duplicates the following openssl command:
// openssl smime -decrypt -in hello.txt.enc -binary -inform DER -inkey private.key -out hello.txt
Cert cert = new Cert();
try {
cert.loadFromFile("qa_data/openssl/EE.cer");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
BinData bd = new BinData();
bd.loadFile("qa_data/openssl/EE.key");
// Assuming success..
PrivateKey privKey = new PrivateKey();
try {
privKey.loadAnyFormat(bd, "");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Crypt2 crypt = new Crypt2();
try {
crypt.setDecryptCert2(cert, privKey);
crypt.setCryptAlgorithm("PKI");
crypt.ckDecryptFile("qa_data/openssl/hello.txt.enc", "qa_output/hello.txt");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Success.");
}
}