当前位置: 首页 > article >正文

生成S19(SRecord)的数据段Hash/AES/RSA

前言

随着网络安全越来越重要,车辆的控制器的代码需要更安全的防护;以下是对S19的Hash和加密操作示例,用户可以将生成的签名或加密值生成新的S19,可实现控制器的安全启动,安全访问等相关子功能。

一、准备测试的S19文件

示例S19内容文件名:HashTestS3_oneline.s19(可以使用S-Record工具先填充地址练习):本程序仅识别S1/S2/S3有效的数据段,并校验checksum和地址的连续性。

S224FAC400780000477060E0007064C4007080E0007080C05470BFE40370A0C00070DFE40326
S604012000DA
S70501040400F1

二、python处理代码

以下为AI生成后修改成需要的代码:

# pip install rsa/'File'->'Setting'
import rsa

# pip install pycryptodome
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import binascii

# SHA1、SHA224、SHA256、SHA384、SHA512
import hashlib

# check len/addr/data
def verify_checksum(data, checksum):
    rst = False
    sum = 0
    for i in range(0,len(data)):
        sum = sum + data[i]
    sum = sum & 0xFF
    sum = 0xFF - sum
    if sum == checksum:
        rst = True
    return rst

# records: error data(checksum, address continous)
# rawData: raw data(download to memeory without address)
def parse_srecord(filename):
    records = []
    rawData = []
    offset = 0
    firstAddr = False
    lastAddr = 0
    address = 0
    dataSize = 0
    with open(filename, 'r') as file:
        for line in file:
            record_type = line[0:2]
            if record_type == 'S1' or record_type == 'S2' or record_type == 'S3':
                byte_count = int(line[2:4], 16)
                data_end = 4 + 2 * byte_count - 2
                if record_type == 'S1':
                    offset = 8
                    address = int(line[4:offset], 16)    # 16bits

                elif record_type == 'S2':
                    offset = 10
                    address = int(line[4:offset], 16)   # 24bits

                elif record_type == 'S3':
                    offset = 12
                    address = int(line[4:offset], 16)    # 32bits

                for i in range(offset, data_end, 2):
                    byte_value = int(line[i:i + 2], 16)
                    #hex_value  = byte_value.to_bytes(1,byteorder='little')
                    rawData.append(byte_value)
                dataSize = (data_end - offset) / 2

                data = [int(line[i:i + 2], 16) for i in range(2, data_end, 2)]  # for checksum(check len/addr/data)
                checksum = int(line[data_end: data_end + 2], 16)

                # address continous check
                if firstAddr == False:
                    lastAddr = address
                    firstAddr = True
                else:
                    if lastAddr + dataSize != address:
                        records.append({
                            'record_type': record_type,
                            'byte_count': byte_count,
                            'address': address,
                            'data': data,
                            'checksum': checksum
                        })
                    lastAddr = address

                if verify_checksum(data, checksum) == False:
                    records.append({
                    'record_type': record_type,
                    'byte_count': byte_count,
                    'address': address,
                    'data': data,
                    'checksum': checksum
                    })
    return [records,rawData]

# sha1 output = 160(bits)
# input=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
# output=9e5175008751d08f361488c9927086b276b965fa
def calculate_sha1(data):
    hashObj = hashlib.sha1()
    hashObj.update(data)
    return hashObj.hexdigest()

# SHA512 output = 512(bits)
# input=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
# output=5a944f3eaf5e8412fcffc48e01a313848cf95f511d0b0667332a4adc5803793a58fe6646d04fc2d9160a43c57ab68545e3298b3cb3ba2284e605edffda603287
def calculate_sha512(data):
    hashObj = hashlib.sha3_512()
    hashObj.update(data)
    return hashObj.hexdigest()

三、生成结果

# Process SRecord
print("-----------------Process SRecord------------------")
s19File = 'HashTestS3_oneline.s19'

[srecords, srawDatas] = parse_srecord(s19File)
# check checksum and address error
if len(srecords) > 0:
    print("SRecord format error! ")
else:
    print("Process SRecord Finish OK!")

# HASH demo
print("-------------------HASH demo-------------------")
sha1 = calculate_sha1(bytes(srawDatas))         # bytes(srawDatas) for convert ’int' to ‘byte'
sha512 = calculate_sha512(bytes(srawDatas))
print("[ sha1 ]: " + sha1 + "   Bit len = " + str(int(len(sha1)/2*8)))
print("[sha512]: " + sha512 + "   Bit len = " + str(int(len(sha512)/2*8)))


# ASE demo
print("-------------------AES demo-------------------")
key = b'Sixteen byte key'
# MODE_CBC mode
cipher = AES.new(key, AES.MODE_CBC)
decryptor = AES.new(key, AES.MODE_CBC, iv=cipher.iv)
# 明文数据
message = bytes(srawDatas) 
# 加密
ciphertext = cipher.encrypt(pad(message, AES.block_size))
# 解密
plaintext = unpad(decryptor.decrypt(ciphertext), AES.block_size)
print('[AES明文]: ', binascii.hexlify(message).decode())
print('[AES密文]: ', binascii.hexlify(ciphertext).decode())
print('[AES解密后的明文]: ', binascii.hexlify(plaintext).decode())

# RSA demo
print("-------------------RSA demo-------------------")
(public_key, private_key) = rsa.newkeys(3072)
print('[RSA公钥]:', public_key)
print('[RSA私钥]:', private_key)

message = bytes(srawDatas)
ciphertext = rsa.encrypt(message, public_key)
plaintext = rsa.decrypt(ciphertext, private_key)
print('[RSA明文]: ', binascii.hexlify(message).decode())
print('[RSA密文]: ', binascii.hexlify(ciphertext).decode())
print('[RSA解密后的明文]: ', binascii.hexlify(plaintext).decode())


http://www.kler.cn/a/392154.html

相关文章:

  • [运维][Nginx]Nginx学习(1/5)--Nginx基础
  • uni-app移动端与PC端兼容预览PDF文件
  • 封装el-menu
  • SpringBoot(八)使用AES库对字符串进行加密解密
  • 编译ffmpeg动态库时设置RPATH为$ORIGIN
  • group_concat配置影响程序出bug
  • python练习-Django web入门
  • 【计算机网络】设备网卡NIC的工作内容有哪些呢?
  • python+pptx:(三)添加统计图、删除指定页
  • rust构建web服务器
  • Linux终端命令后台运行
  • spring中r类是什么
  • ubuntu下的一些常用指令
  • 如何从头开始构建神经网络?(附教程)
  • 代码随想录算法训练营第四十五天|Day45 动态规划
  • pandas的to_sql方法中使用if_exists=‘replace‘
  • Spring Boot编程训练系统:最佳实践与技巧
  • MySQL与Oracle对比及区别
  • 图像增强——代数运算
  • vue3面试题1|[2024-11-12]
  • labview用sql server数据库存取数据到一个单元格
  • AI: 情景模拟攻击(草稿)
  • 蓝队的基础
  • 奥迪:在工业边缘使用 VMware 边缘计算堆栈
  • 从 O(n²) 到 O(n):单调栈在算法中的妙用
  • SpringSecurity Demo实操