OpenSSL学习笔记三:实操

实操

未完待续…

对称加密

https://www.openssl.org/docs/man1.1.1/man3/
可以修改Key、IV、Nonce的最后一位,看加密后的密文是否修改

//man EVP_EncryptInit

//DES in CBC, ECB, CFB and OFB modes
const EVP_CIPHER * EVP_des_cbc(void);
const EVP_CIPHER * EVP_des_ecb(void);
const EVP_CIPHER * EVP_des_cfb(void);
const EVP_CIPHER * EVP_des_ofb(void);

//Two key triple DES in CBC, ECB, CFB and OFB modes
const EVP_CIPHER * EVP_des_ede_cbc(void);
const EVP_CIPHER * EVP_des_ede(void);
const EVP_CIPHER * EVP_des_ede_ofb(void);
const EVP_CIPHER * EVP_des_ede_cfb(void);

//Three key triple DES in CBC, ECB, CFB and OFB modes
const EVP_CIPHER * EVP_des_ede3_cbc(void);
const EVP_CIPHER * EVP_des_ede3(void);
const EVP_CIPHER * EVP_des_ede3_ofb(void);
const EVP_CIPHER * EVP_des_ede3_cfb(void);

//AES with 128 bit key length in CBC, ECB, OFB and CFB modes
const EVP_CIPHER * EVP_aes_128_cbc(void);
const EVP_CIPHER * EVP_aes_128_ecb(void);
const EVP_CIPHER * EVP_aes_128_ofb(void);
const EVP_CIPHER * EVP_aes_128_cfb1(void);
const EVP_CIPHER * EVP_aes_128_cfb8(void);
const EVP_CIPHER * EVP_aes_128_cfb128(void);

//AES with 192 bit key length in CBC, ECB, OFB and CFB modes
const EVP_CIPHER * EVP_aes_192_cbc(void);
const EVP_CIPHER * EVP_aes_192_ecb(void);
const EVP_CIPHER * EVP_aes_192_ofb(void);
const EVP_CIPHER * EVP_aes_192_cfb1(void);
const EVP_CIPHER * EVP_aes_192_cfb8(void);
const EVP_CIPHER * EVP_aes_192_cfb128(void);

//AES with 256 bit key length in CBC, ECB, OFB and CFB modes
const EVP_CIPHER * EVP_aes_256_cbc(void);
const EVP_CIPHER * EVP_aes_256_ecb(void);
const EVP_CIPHER * EVP_aes_256_ofb(void);
const EVP_CIPHER * EVP_aes_256_cfb1(void);
const EVP_CIPHER * EVP_aes_256_cfb8(void);
const EVP_CIPHER * EVP_aes_256_cfb128(void);

//AES Galois Counter Mode (GCM)
const EVP_CIPHER * EVP_aes_128_gcm(void);
const EVP_CIPHER * EVP_aes_192_gcm(void);
const EVP_CIPHER * EVP_aes_256_gcm(void);
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL);//a default IV length is used (96 bits for AES)
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, taglen, tag);//
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, taglen, tag);//


//AES Counter with CBC-MAC Mode (CCM)
const EVP_CIPHER * EVP_aes_128_ccm(void);
const EVP_CIPHER * EVP_aes_192_ccm(void);
const EVP_CIPHER * EVP_aes_256_ccm(void);
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, taglen, tag);
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, ivlen, NULL);
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, ivlen, NULL);


const EVP_CIPHER *EVP_rc4(void);

aes_128_ecb

int encode_aes_128_ecb(unsigned char* plainText,int plainTextLen,\
unsigned char* cipherText,int cipherTextSize,\
unsigned char *key)
{
   
	int cipherTextLen=0,l=0;
	EVP_CIPHER_CTX ctx;
	EVP_CIPHER_CTX_init(&ctx);
	EVP_EncryptInit_ex(&ctx,EVP_aes_128_ecb(),NULL,key,NULL);
	EVP_EncryptUpdate(&ctx,cipherText+cipherTextLen,&l,plainText,plainTextLen);
	cipherTextLen += l;
	EVP_EncryptFinal_ex(&ctx,cipherText+cipherTextLen,&l);
	cipherTextLen += l;
	EVP_CIPHER_CTX_cleanup(&ctx);
	return cipherTextLen;
}

int decode_aes_128_ecb(unsigned char* cipherText,int cipherTextLen,\
unsigned char* plainText,int plainTextSize,\
unsigned char *key)
{
   
	int plainTextLen=0,l=0;
	int ret;
	EVP_CIPHER_CTX ctx;
	EVP_CIPHER_CTX_init(&ctx);
	EVP_DecryptInit_ex(&ctx,EVP_aes_128_ecb(),NULL,key,NULL);
	EVP_DecryptUpdate(&ctx,plainText+plainTextLen,&l,cipherText,cipherTextLen);
	plainTextLen += l;

	ret = EVP_DecryptFinal_ex(&ctx,plainText+plainTextLen,&l);//0 fail, 1 success
	plainTextLen += l;
	EVP_CIPHER_CTX_cleanup(&ctx);
	if(ret <=0)
		return ret;
	else
		return plainTextLen;
}

int main()
{
   
	char plainText[1024] = {
   0},cipherText[1024] = {
   0};
	int plainTextLen,cipherTextLen;
	unsigned char key[16] = "1111111111111111";
	//RAND_bytes(key,16);

	plainTextLen = sprintf(plainText,"hello world");
	memset(cipherText,0,sizeof(cipherText));
	//encrypt
	cipherTextLen = encode_aes_128_ecb(plainText,plainTextLen,cipherText,sizeof(cipherText),key);
	int i;
	for(i=0;i<cipherTextLen;i++){
   
		printf("%02x",cipherText[i]);
	}
	printf("\n");

	memset(plainText,0,sizeof(plainText));
	//decrypt
	plainTextLen =  decode_aes_128_ecb(cipherText,cipherTextLen,plainText,sizeof(plainText),key);
	printf("[%d][%s]\n",plainTextLen,plainText);
}

aes_192_cbc

int encode_aes_192_cbc(unsigned char* plainText,int plainTextLen,\
unsigned char* cipherText,int cipherTextSize,\
unsigned char *key,unsigned char *iv)
{
   
	int cipherTextLen=0,l=0;
	EVP_CIPHER_CTX ctx;
	EVP_CIPHER_CTX_init(&ctx);
	EVP_EncryptInit_ex(&ctx,EVP_aes_192_cbc(),NULL,key
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值