python 实现AES-CMAC算法验证_aescmac算法验证,不服不行


一、AES-CMAC算法原理及用途

什么是基于AES的CMAC算法?

采用AES加密算法,使用密钥K,对明文P进行加密,得到的密文C,作为明文P的认证码,和明文P一起传输给接收方。接收方收到后,再使用自己的密钥,对明文再做一次AES加密,生成新的认证码,与接收到的发送方的认证码进行对比验证。如果相等,说明明文没有被篡改,接收方就可以接收明文并处理;如果不相等,说明明文被篡改,数据不安全,则丢弃!

这就是基于AES的CMAC算法,多用于消息数据的正确性认证,生成的认证码,叫作message authentication code,消息认证码,简称MAC。

二、python CMAC生成验证算法实现

from Crypto.Cipher import AES
from Crypto.Hash import CMAC
from binascii import hexlify, unhexlify
import binascii
import os

key = unhexlify(‘2b7e151628aed2a6abf7158809cf4f3c’)
#print(“key:”, key.decode())

message = unhexlify(‘6bc1bee22e409f96e93d7e117393172a’)
#print(“message:”, message.decode())

mac = CMAC.new(key,message,ciphermod=AES)
print(“AES_CMAC:”,mac.hexdigest())

‘’‘message1 = unhexlify(‘6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411’)
mac1 = CMAC.new(key,message1,ciphermod=AES)
print(“AES_CMAC1:”,mac1.hexdigest())’‘’

三、验证结果

运行python脚本:

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数网络安全工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上网络安全知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注网络安全获取)
img

学习路线:

这个方向初期比较容易入门一些,掌握一些基本技术,拿起各种现成的工具就可以开黑了。不过,要想从脚本小子变成黑客大神,这个方向越往后,需要学习和掌握的东西就会越来越多以下是网络渗透需要学习的内容:
在这里插入图片描述

一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算**

  • 14
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
详细介绍了AES-CMAC算法的原理与实现,附有C语言写的样例程序。 以下是原文的introduction: The National Institute of Standards and Technology (NIST) has recently specified the Cipher-based Message Authentication Code(CMAC). CMAC [NIST-CMAC] is a keyed hash function that is based on a symmetric key block cipher, such as the Advanced Encryption Standard [NIST-AES]. CMAC is equivalent to the One-Key CBC MAC1 (OMAC1) submitted by Iwata and Kurosawa [OMAC1a, OMAC1b]. OMAC1 is an improvement of the eXtended Cipher Block Chaining mode (XCBC) submitted by Black and Rogaway [XCBCa, XCBCb], which itself is an improvement of the basic Cipher Block Chaining-Message Authentication Code (CBC-MAC). XCBC efficiently addresses the security deficiencies of CBC-MAC, and OMAC1 efficiently reduces the key size of XCBC. AES-CMAC provides stronger assurance of data integrity than a checksum or an error-detecting code. The verification of a checksum or an error-detecting code detects only accidental modifications of the data, while CMAC is designed to detect intentional, unauthorized modifications of the data, as well as accidental modifications. AES-CMAC achieves a security goal similar to that of HMAC [RFC-HMAC]. Since AES-CMAC is based on a symmetric key block cipher, AES, and HMAC is based on a hash function, such as SHA-1, AES-CMAC is appropriate for information systems in which AES is more readily available than a hash function. This memo specifies the authentication algorithm based on CMAC with AES-128. This new authentication algorithm is named AES-CMAC.
AES-CMAC是一种使用AES算法实现的消息认证码(MAC算法。以下是一个用C语言实现AES-CMAC算法的示例代码: ```c #include <stdlib.h> #include <stdio.h> #include <string.h> #include <openssl/aes.h> void XOR(unsigned char *a, unsigned char *b, unsigned char *out, int len) { for(int i = 0; i < len; i++) { out[i] = a[i] ^ b[i]; } } void double_128bit(unsigned char *input, unsigned char *output) { unsigned char carry = 0x00; for(int i = 15; i >= 0; i--) { output[i] = input[i] << 1; output[i] |= carry; carry = (input[i] & 0x80) ? 0x01 : 0x00; } if((input[0] & 0x80) != 0) { output[15] ^= 0x87; } } void aes_cmac(unsigned char *key, unsigned char *msg, int msg_len, unsigned char *mac) { AES_KEY aes_key; unsigned char L[16], K1[16], K2[16]; unsigned char subkey[16]; unsigned char padded_msg[16]; memset(L, 0x00, sizeof(L)); memset(K1, 0x00, sizeof(K1)); memset(K2, 0x00, sizeof(K2)); aes_set_encrypt_key(key, 128, &aes_key); aes_encrypt(&aes_key, L, K1); double_128bit(K1, K2); int num_blocks = (msg_len + 15) / 16; int last_block_len = msg_len % 16; if(last_block_len == 0) { memcpy(padded_msg, &msg[(num_blocks - 1) * 16], 16); XOR(padded_msg, K1, subkey, 16); } else { memcpy(padded_msg, &msg[(num_blocks - 1) * 16], last_block_len); padded_msg[last_block_len] = 0x80; memset(&padded_msg[last_block_len + 1], 0x00, 15 - last_block_len); XOR(padded_msg, K2, subkey, 16); } for(int i = 0; i < num_blocks - 1; i++) { XOR(subkey, &msg[i * 16], subkey, 16); aes_encrypt(&aes_key, subkey, subkey); } XOR(subkey, padded_msg, mac, 16); aes_encrypt(&aes_key, mac, mac); } int main() { unsigned char key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}; unsigned char msg[32] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30}; unsigned char mac[16]; aes_cmac(key, msg, sizeof(msg), mac); printf("MAC: "); for(int i = 0; i < 16; i++) { printf("%02x", mac[i]); } printf("\n"); return 0; } ``` 这是一个简单的AES-CMAC算法实现。它使用了OpenSSL库来实现AES算法。```aes_cmac```函数接受一个128位的密钥、要计算MAC的消息和消息的长度作为输入,并输出一个128位的MAC。 你可以自行替换```key```和```msg```变量的值以测试该代码的功能。最终计算得到的MAC将会输出到控制台。 需要注意的是,这只是一个简单示例,用于演示如何实现AES-CMAC算法。在实际使用中,你应该使用经过审计和验证的加密库来保护你的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值