Pages

Tuesday, 6 December 2016

Load a PKCS#12 with OpenSSL

#include <stdio.h>
#include <stdlib.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/pkcs12.h>

/* Simple PKCS#12 file reader */

int main(int argc, char **argv)
{
    FILE *fp;
    EVP_PKEY *pkey;
    X509 *cert;
    STACK_OF(X509) *ca = NULL;
    PKCS12 *p12;
    int i;
    if (argc != 4) {
        fprintf(stderr, "Usage: pkread p12file password opfile\n");
        exit (1);
    }
    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();
    if (!(fp = fopen(argv[1], "rb"))) {
        fprintf(stderr, "Error opening file %s\n", argv[1]);
        exit(1);
    }
    p12 = d2i_PKCS12_fp(fp, NULL);
    fclose (fp);
    if (!p12) {
        fprintf(stderr, "Error reading PKCS#12 file\n");
        ERR_print_errors_fp(stderr);
        exit (1);
    }
    if (!PKCS12_parse(p12, argv[2], &pkey, &cert, &ca)) {
        fprintf(stderr, "Error parsing PKCS#12 file\n");
        ERR_print_errors_fp(stderr);
        exit (1);
    }
    PKCS12_free(p12);
    if (!(fp = fopen(argv[3], "w"))) {
        fprintf(stderr, "Error opening file %s\n", argv[1]);
        exit(1);
    }
    if (pkey) {
        fprintf(fp, "***Private Key***\n");
        PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
    }
    if (cert) {
        fprintf(fp, "***User Certificate***\n");
        PEM_write_X509_AUX(fp, cert);
    }
    if (ca && sk_X509_num(ca)) {
        fprintf(fp, "***Other Certificates***\n");
        for (i = 0; i < sk_X509_num(ca); i++) 
            PEM_write_X509_AUX(fp, sk_X509_value(ca, i));
    }

    sk_X509_pop_free(ca, X509_free);
    X509_free(cert);
    EVP_PKEY_free(pkey);

    fclose(fp);
    return 0;
}

Thursday, 29 September 2016

[SOLVED] Exception in thread "main" java.security.NoSuchProviderException: No such provider: BC

If you have problem or error "Exception in thread "main" java.security.NoSuchProviderException: No such provider: BC".  This is Solusion.



Friday, 16 September 2016

Compute Digest Using Md5,SHA1,SHA256,SHA384,SHA512,SHA3-224,SHA3-256,SHA3-512


I Want To Show You How to compute digest  Using Md5,SHA1,SHA256,SHA384,SHA512,SHA3-224,SHA3-256,SHA3-512 in QT. This my code,
      #include <QCryptographicHash> 
       -------
       -------
    // MD5
    QCryptographicHash cryptoHashMd5(QCryptographicHash::Md5);

    //SHA1
    QCryptographicHash cryptoHashSHA1(QCryptographicHash::Sha1);

    //SHA224
    QCryptographicHash cryptoHashSHA224(QCryptographicHash::Sha224);

    //SHA256
    QCryptographicHash cryptoHashSHA256(QCryptographicHash::Sha256);

    //SHA 384
    QCryptographicHash cryptoHashSHA384(QCryptographicHash::Sha384);

    //SHA 512
    QCryptographicHash cryptoHashSHA512(QCryptographicHash::Sha512);

    //SHA3-256
    QCryptographicHash cryptoHashSHA3256(QCryptographicHash::Sha3_256);

    //SHA3-224
    QCryptographicHash cryptoHashSHA3224(QCryptographicHash::Sha3_224);

    //SHA3-512
    QCryptographicHash cryptoHashSHA3512(QCryptographicHash::Sha3_512);

    //Compute input digest using Md5
    cryptoHashMd5.addData(ui->textEdit_input->toPlainText().toLatin1());

    //Compute input digest using SHA1
    cryptoHashSHA1.addData(ui->textEdit_input->toPlainText().toLatin1());

    //Compute input digest using SHA224
    cryptoHashSHA224.addData(ui->textEdit_input->toPlainText().toLatin1());

    //Compute input digest using SHA256
    cryptoHashSHA256.addData(ui->textEdit_input->toPlainText().toLatin1());

    //Compute input digest using SHA384
    cryptoHashSHA384.addData(ui->textEdit_input->toPlainText().toLatin1());

    //Compute input digest using SHA512
    cryptoHashSHA512.addData(ui->textEdit_input->toPlainText().toLatin1());

    //Compute input digest using SHA3-256
    cryptoHashSHA3256.addData(ui->textEdit_input->toPlainText().toLatin1());

    //Compute input digest using SHA3-224
    cryptoHashSHA3224.addData(ui->textEdit_input->toPlainText().toLatin1());

    //Compute input digest using SHA3-512
    cryptoHashSHA3512.addData(ui->textEdit_input->toPlainText().toLatin1());

    //set Output Digest MD5
    ui->lineEdit_MD5->setText(cryptoHashMd5.result().toHex().data());
    //set Output Digest SHA1
    ui->lineEdit_SHA1->setText(cryptoHashSHA1.result().toHex().data());
    //set Output Digest SHA224
    ui->lineEdit_SHA224->setText(cryptoHashSHA224.result().toHex().data());
    //set Output Digest SHA256
    ui->lineEdit_SHA256->setText(cryptoHashSHA256.result().toHex().data());
    //Set Output Digest SHA384
    ui->textEdit_OutputSHA384->setText(cryptoHashSHA384.result().toHex().data());
    //Set Output Digest SHA512
    ui->textEdit_OutputSHA512->setText(cryptoHashSHA512.result().toHex().data());
    //Set Output Digest SHA3-256
    ui->textEdit_OutputSHA3256->setText(cryptoHashSHA3256.result().toHex().data());
    //Set Output Digest SHA3-224
    ui->lineEdit_SHA3224->setText(cryptoHashSHA3224.result().toHex().data());
    //Set Ouput Digest SHA3-512
    ui->textEdit_OutputSHA3512->setText(cryptoHashSHA3512.result().toHex().data());

For Detail, Please watch my video. Thanks


Friday, 8 January 2016

Encrypt And Decrypt Using TripleDES in Java

Name Algorithm
TripleDES
KeyLength
192 bit / 24 Character
Block Size
64 bit
Tutorial Encrypt And Decrypt Using TripleDES in Java
https://youtu.be/ih6M79mEzk4

Wednesday, 30 December 2015

[SOLVED] org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$1: Key length not 128/192/256 bits.

SOURCE
 ----- 
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
 byte[] input = "Hello".getBytes();
 byte[] keyByte = "1234567812345678123456781234567".getBytes();
 SecretKeySpec key = new SecretKeySpec(keyByte, "AES");
----
ERROR
Dec 31, 2015 11:57:52 AM aes256.AES256 main
SEVERE: null
org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$1: Key length not 128/192/256 bits.
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(Unknown Source)
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(Unknown Source)
at javax.crypto.Cipher.init(Cipher.java:1246)
at javax.crypto.Cipher.init(Cipher.java:1186)
at aes256.AES256.main(AES256.java:36)
Caused by: java.lang.IllegalArgumentException: Key length not 128/192/256 bits.
at org.bouncycastle.crypto.engines.AESFastEngine.generateWorkingKey(Unknown Source)
at org.bouncycastle.crypto.engines.AESFastEngine.init(Unknown Source)
at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.init(Unknown Source)
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$BufferedGenericBlockCipher.init(Unknown Source)
... 5 more
BUILD SUCCESSFUL (total time: 1 second)
SOLUTION
Key Leng AES must 128 / 192 / 256 bit or 16 / 24 / 32 Character 
 Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
 byte[] input = "Hello".getBytes();
 byte[] keyByte = "12345678123456781234567812345678".getBytes();
 SecretKeySpec key = new SecretKeySpec(keyByte, "AES");



Tuesday, 29 December 2015

Encrpt And Decrypt Using AES 256 in Java

Name Algorithm
AES 256
Type Algorithm
Block Cipher
Key Length
256 bit / 32 Character
Link Video Encrypt and Decrypt Using AES 256 in Java
https://youtu.be/NS0QBDq9PfA