openssl基于ede3的加密和解密
基于ede3的加密和解密当前提供模式有cfb和cbc数据长度非向量整数倍特别注意当数据长度是非向量证书倍的时候该如何处理数据openssl 版本OpenSSL 1.1.1 11 Sep 2018验证结果明文 10000 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63please select :1: EVP_des_ede3_ofb2: EVP_des_ede3_cbc1加密结果长度10058 ec 26 8c 73 f3 b7 99 4f d0 bd 41 43 57 e8 62 41 3c a6 9d 5c 94 e2 82 54 50 29 71 8f b5 bd 64 b2 21 b2 71 30 30 25 01 8b ef 46 df 23 e5 4a 95 0a d7 0f 45 2e 1a 03 16 7f 2e 70 64 80 e7 c6 97 85 14 37 59 7f a9 5c 53 f5 59 9a 15 24 89 dc b9 fe c6 57 5a d3 e0 e1 25 34 96 2b 43 74 43 47 7e c8 5f c5 ef解密结果长度10000 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63test ok!// 编译gcc -g test_1.c -o test_1 -L/usr/lib -lssl -lcrypto#includestring.h#includeopenssl/err.h#includeopenssl/evp.hintmain(){intret,which1;EVP_CIPHER_CTX*ctx;constEVP_CIPHER*cipher;unsignedcharkey[24],iv[8],in[100],out[108],de[100];inti,len,inl,outl,total0;for(i0;i24;i){memset(key[i],i,1);}for(i0;i8;i){memset(iv[i],i,1);}printf(明文 100\n);for(i0;i100;i){memset(in[i],i,1);printf(%02x ,in[i]);}printf(\n);// EVP_CIPHER_CTX_init(ctx);ctxEVP_CIPHER_CTX_new();printf(please select :\n);printf(1: EVP_des_ede3_ofb\n);printf(2: EVP_des_ede3_cbc\n);scanf(%d,which);if(which1)cipherEVP_des_ede3_ofb();elsecipherEVP_des_ede3_cbc();retEVP_EncryptInit_ex(ctx,cipher,NULL,key,iv);if(ret!1){printf(EVP_EncryptInit_ex err1!\n);return-1;}inl50;len0;EVP_EncryptUpdate(ctx,outlen,outl,in,inl);lenoutl;EVP_EncryptUpdate(ctx,outlen,outl,in50,inl);lenoutl;EVP_EncryptFinal_ex(ctx,outlen,outl);lenoutl;printf(加密结果长度%d\n,len);for(i0;ilen;i){printf(%02x ,*(outi));}printf(\n);/* 解密 */EVP_CIPHER_CTX_cleanup(ctx);EVP_CIPHER_CTX_init(ctx);retEVP_DecryptInit_ex(ctx,cipher,NULL,key,iv);if(ret!1){printf(EVP_DecryptInit_ex err1!\n);return-1;}total0;EVP_DecryptUpdate(ctx,detotal,outl,out,44);totaloutl;EVP_DecryptUpdate(ctx,detotal,outl,out44,len-44);totaloutl;retEVP_DecryptFinal_ex(ctx,detotal,outl);totaloutl;printf(解密结果长度%d\n,total);for(i0;itotal;i){printf(%02x ,*(dei));}printf(\n);if(ret!1){EVP_CIPHER_CTX_cleanup(ctx);printf(EVP_DecryptFinal_ex err\n);return-1;}if((total!100)||(memcmp(de,in,100))){printf(err!\n);return-1;}EVP_CIPHER_CTX_cleanup(ctx);printf(test ok!\n);return0;}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2630471.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!