OPENSSL AES Encrypt ECB method example


php example
<?php



$iv = "6543210987654321"; /* 必须16位哦 */
$password="1234567812345678";
$data="12345678901234567890012345678909999999999";

#$enc=aes_encode($data,$password)."\n";

$enc="S3Aqx0GUd3vcz5r6CkCkonPzwfaL3z3BgMtP8aEbzKUNeE+8APZVP02sviaSZXyS";

echo "decode:[".aes_decode($enc,$password)."]\n";

/* 采用128位加密,密钥也必须是16位 */
function aes_encode($sourcestr, $key)
{
    global $iv;
    return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $sourcestr, MCRYPT_MODE_CBC, $iv));
}

function aes_decode($crypttext, $key)
{
    global $iv;
    return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($crypttext), MCRYPT_MODE_CBC, $iv), "\0");
}


?>

C++ example



string COpenSSL::aes_encode(const char *sourcestr, char *key = "")
{
    if (strcmp(key, "") == 0)
    {
    	return "";
    }

    int len = strlen(sourcestr);
    unsigned char iv[AES_BLOCK_SIZE+1] = "6543210987654321";  // 注意,iv绝对不能是const的,否则会段错误

    unsigned char * out = (unsigned char *)malloc(1024*1024);
    if (out == NULL) {
        fprintf(stderr, "No Memory!\n");
    }
    AES_KEY aes;
    if(AES_set_encrypt_key((unsigned char*)key, 128, &aes) < 0)
    {
        return NULL;
    }
    /* 计算补0后的长度 */
    int out_len = ((len - 1) / 16 + 1)* 16;
    char * sstr = (char *)malloc(sizeof(char) * out_len + 1);
    /* 补0 */
    memset(sstr, 0, out_len+1);
    strcpy(sstr, sourcestr);
    AES_cbc_encrypt((unsigned char*)sstr, out, out_len, &aes, (unsigned char*)iv, AES_ENCRYPT);
    /* 这里的长度一定要注意,不能用strlen来获取,加密后的字符串中可能会包含\0 */
    string out2 = base64_encode((char *)out, out_len);
    free(out);
    free(sstr);

    ofstream myfile;
    		myfile.open("aes.txt", ios::out);
    		myfile.write((char *) out2.c_str(), out2.size());
    		myfile.close();


    return out2;
}

string COpenSSL::aes_decode(const char *crypttext, char *key = "")
{
    if (strcmp(key, "") == 0)
    {
    	return "";
    }
    int out_len = 0;
    unsigned char iv[AES_BLOCK_SIZE+1] = "6543210987654321";

    string in = base64_decode(crypttext);
    out_len=in.size();
    char *out = (char *) malloc(sizeof(char) * out_len + 1);
    memset(out, 0, out_len + 1);
    AES_KEY aes;
    if(AES_set_decrypt_key((unsigned char*)key, 128, &aes) < 0)
    {
        return "";
    }

    AES_cbc_encrypt((unsigned char*)in.c_str(), (unsigned char*)out, out_len, &aes, (unsigned char*)iv, AES_DECRYPT);
    //free(in);
    string sRet=out;
    free(out);
    return sRet;
}


bool COpenSSL::testAES(std::string p_sKey, std::string p_sData2Encrypt,
		bool p_bEnc, std::string & p_sEncStr) {

	/*
	 * aes BLOCK HAVE TO BE 16 BIT BLOCK
	 * AES key has to be 16 CHAR
	 */
	std::string sBuffer, sOutBuffer;
	//std::string sKey="1234567812345678";
	unsigned char aes_key[16]; //128 bit
	memset(aes_key, 0, 16);

	int nKeyLen = p_sKey.size() <= 16 ? p_sKey.size() : 16;
	memcpy(aes_key, p_sKey.c_str(), nKeyLen);   //Get password inplace
	/* Buffers for Encryption and Decryption */

	unsigned char databuffer[16];
	memset(databuffer, 0, 16);
	unsigned char outbuffer[16];
	memset(outbuffer, 0, 16);

	/* AES-128 bit CBC Encryption */
	AES_KEY enc_key, dec_key;
	if (p_bEnc) {

		if (AES_set_encrypt_key(aes_key, sizeof(aes_key) * 8, &enc_key) < 0) {
			return false;
		}
	} else {
		if (AES_set_decrypt_key(aes_key, sizeof(aes_key) * 8, &dec_key) < 0) {

			return false;
		}

	}

	int32_t nDataLen = 0;

	if (p_bEnc) {
		nDataLen = p_sData2Encrypt.size();
		sBuffer.append((char *) &nDataLen, sizeof(int32_t));
		sBuffer.append(p_sData2Encrypt);
		int nMod = sBuffer.size() % 16;
		if (nMod != 0) {
			sBuffer.append((char *) databuffer, 16 - nMod);

		}

	} else {
		sBuffer = base64_decode(p_sData2Encrypt);
		nDataLen = sBuffer.size();
		if (nDataLen % 16 != 0) {
			TRACE("Wrong Buffer,  Len mod 16 has to be 0\n");
			return false;
		}
	}

	int nDataIndex = 0;
	int nCopySize = 16;
	nDataLen = sBuffer.size();
	//AES_set_decrypt_key(aes_key, sizeof(aes_key)*8, &dec_key); // Size of key is in bits

	while (nDataIndex < nDataLen) {

		memcpy(databuffer, sBuffer.c_str() + nDataIndex, nCopySize);
		if (p_bEnc) {
			AES_ecb_encrypt(databuffer, outbuffer, &enc_key, AES_ENCRYPT);
		} else {
			AES_ecb_encrypt(databuffer, outbuffer, &dec_key, AES_DECRYPT);

		}
		nDataIndex += 16;
		sOutBuffer.append((char *) outbuffer, 16);
	}

	if (p_bEnc) {
		p_sEncStr = base64_encode((unsigned char *) sOutBuffer.c_str(),
				sOutBuffer.size());
	} else {
		int32_t nTotalLen = 0;
		memcpy((char *) &nTotalLen, sOutBuffer.c_str(), sizeof(int32_t));


		p_sEncStr.append(sOutBuffer.c_str() + sizeof(int32_t), nTotalLen);
	}

	return true;

	

}