Python: pad and unpad byte array

79 篇文章 0 订阅

import chilkat
 
 
data = chilkat.CkByteData()
data.append( '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09' , 10 )
 
#  These are the padding options for the "pad" method:
#  0 = Each padding byte is the pad count (16 extra added if size is already a multiple of 16)
#  1 = Random bytes except the last is the pad count (16 extra added if size is already multiple of 16)
#  2 = Pad with random data. (If already a multiple of 16, no padding is added).
#  3 = Pad with NULLs. (If already a multiple of 16, no padding is added).
#  4 = Pad with SPACE chars(0x20). (If already a multiple of 16, no padding is added).
 
#  We'll pad to a block size of 16 bytes.
blockSize = 16
 
paddingScheme = 0
 
#  Pad, display the padded byte data, unpad, then re-display
data.pad(blockSize,paddingScheme)
print data.getEncoded( "hex" )
data.unpad( 16 ,paddingScheme)
print data.getEncoded( "hex" )
print "----"
 
paddingScheme = 1
#  Pad, display the padded byte data, unpad, then re-display
data.pad(blockSize,paddingScheme)
print data.getEncoded( "hex" )
data.unpad( 16 ,paddingScheme)
print data.getEncoded( "hex" )
print "----"
 
#  There is no unpadding with schemes 2,3, and 4
data2 = chilkat.CkByteData()
data2.append( '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09' , 10 )
paddingScheme = 2
#  Pad and display the padded byte data
data2.pad(blockSize,paddingScheme)
print data2.getEncoded( "hex" )
print "----"
 
#  There is no unpadding with schemes 2,3, and 4
data3 = chilkat.CkByteData()
data3.append( '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09' , 10 )
paddingScheme = 3
#  Pad and display the padded byte data
data3.pad(blockSize,paddingScheme)
print data3.getEncoded( "hex" )
print "----"
 
#  There is no unpadding with schemes 2,3, and 4
data4 = chilkat.CkByteData()
data4.append( '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09' , 10 )
paddingScheme = 4
#  Pad and display the padded byte data
data4.pad(blockSize,paddingScheme)
print data4.getEncoded( "hex" )
print "----"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>