| 48 | } |
| 49 | |
| 50 | bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) |
| 51 | { |
| 52 | if (!fKeySet) |
| 53 | return false; |
| 54 | |
| 55 | // max ciphertext len for a n bytes of plaintext is |
| 56 | // n + AES_BLOCK_SIZE - 1 bytes |
| 57 | int nLen = vchPlaintext.size(); |
| 58 | int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0; |
| 59 | vchCiphertext = std::vector<unsigned char> (nCLen); |
| 60 | |
| 61 | EVP_CIPHER_CTX ctx; |
| 62 | |
| 63 | bool fOk = true; |
| 64 | |
| 65 | EVP_CIPHER_CTX_init(&ctx); |
| 66 | if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0; |
| 67 | if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0; |
| 68 | if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0; |
| 69 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 70 | |
| 71 | if (!fOk) return false; |
| 72 | |
| 73 | vchCiphertext.resize(nCLen + nFLen); |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) |
| 78 | { |
no test coverage detected