00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "crypt.h"
00021
00022 using namespace YAPET;
00023
00037 Crypt::Crypt(const Key& k) throw(YAPETException) : cipher(NULL),
00038 iv_length(0),
00039 key_length(0),
00040 key(k){
00041 cipher = EVP_bf_cbc();
00042 if (cipher == NULL)
00043 throw YAPETException("Unable to get cipher");
00044
00045
00046 EVP_CIPHER_CTX ctx;
00047 EVP_CIPHER_CTX_init(&ctx);
00048
00049 int retval = EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, NULL, 0);
00050 if (retval == 0) {
00051 EVP_CIPHER_CTX_cleanup(&ctx);
00052 throw YAPETException("Error initializing cipher");
00053 }
00054
00055 retval = EVP_CIPHER_CTX_set_key_length(&ctx, key.size());
00056 if (retval == 0) {
00057 EVP_CIPHER_CTX_cleanup(&ctx);
00058 throw YAPETException("Error setting the key length");
00059 }
00060
00061 iv_length = EVP_CIPHER_CTX_iv_length(&ctx);
00062 key_length = EVP_CIPHER_CTX_key_length(&ctx);
00063
00064 EVP_CIPHER_CTX_cleanup(&ctx);
00065 }
00066
00067 Crypt::Crypt(const Crypt& c) : cipher(c.cipher),
00068 iv_length(c.iv_length),
00069 key_length(c.key_length),
00070 key(c.key) {
00071 }
00072
00073 const Crypt&
00074 Crypt::operator=(const Crypt& c) {
00075 if (this == &c) return *this;
00076
00077 iv_length = c.iv_length;
00078 key_length = c.key_length;
00079 cipher = c.cipher;
00080 key = c.key;
00081
00082 return *this;
00083 }