본문 바로가기

Native/C++

openssl padding

http://www.openssl.org/docs/crypto/EVP_EncryptInit.html

int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);


EVP_CipherInit(&ctx, EVP_bf_ecb(), NULL, NULL, 0);
EVP_CIPHER_CTX_set_key_length(&ctx, 16);

EVP_CipherInit(&ctx, NULL, contentKey, NULL, 0);
fin = fopen("C:\\My Project\\Practices\\Crypto\\Debug\\HelloWorld.sum", "rb");
fout = fopen("C:\\My Project\\Practices\\Crypto\\Debug\\HelloWorld.pdf", "wb");

for (;;) {
    inlen = fread(inbuf, 1, 1024, fin);
    if (inlen <= 0) break;
    if (!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen)) {
        printf("error1");
        exit(-1);
    }
    fwrite(outbuf, 1, outlen, fout);
}

if (!EVP_CipherFinal(&ctx, outbuf, &outlen)) {
    printf("error2");
    exit(-1);
}
fwrite(outbuf, 1, outlen, fout);






http://linux.die.net/man/3/evp_cipher_ctx_set_padding

Encrypt a string using blowfish:
int do_crypt(char *outfile)
     {
     unsigned char outbuf[1024];
     int outlen, tmplen;
     /* Bogus key and IV: we'd normally set these from
        * another source.
        */
     unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
     unsigned char iv[] = {1,2,3,4,5,6,7,8};
     char intext[] = "Some Crypto Text";
     EVP_CIPHER_CTX ctx;
     FILE *out;
     EVP_CIPHER_CTX_init(&ctx);
     EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv);

     if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext)))
             {
             /* Error */
             return 0;
             }
     /* Buffer passed to EVP_EncryptFinal() must be after data just
        * encrypted to avoid overwriting it.
        */
     if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen))
             {
             /* Error */
             return 0;
             }
     outlen += tmplen;
     EVP_CIPHER_CTX_cleanup(&ctx);
     /* Need binary mode for fopen because encrypted data is
        * binary data. Also cannot use strlen() on it because
        * it wont be null terminated and may contain embedded
        * nulls.
        */
     out = fopen(outfile, "wb");
     fwrite(outbuf, 1, outlen, out);
     fclose(out);
     return 1;
     }

'Native > C++' 카테고리의 다른 글

pkcs padding  (0) 2013.10.02
Block cipher modes of operation  (0) 2013.10.02
using padding in encryption  (0) 2013.10.02
crypto padding  (0) 2013.10.02
-fno-strict-aliasing  (0) 2013.10.02