前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python精简代码实现循环左移循环右移

Python精简代码实现循环左移循环右移

作者头像
IBinary
发布2020-12-22 14:48:20
2.2K0
发布2020-12-22 14:48:20
举报
文章被收录于专栏:逆向技术逆向技术

Python实现循环左移右移

一丶Python实现循环左移 右移 原理

1.1 Python实现循环左移

1.1.1 文字原理介绍

循环左移原理 拿一个32位的数(4个字节)来说 进行移动八位 如: 0x12345678 rol 8 之后 = 0x34567812 其原理如下: 1.首先左移八位得到 0x345678 2.然后右移24位得到 0x12 最后 0x345678 | 0x12 = 0x34567812 鉴于Python的特殊性.我们只需要32bit数即可. 也就是最后要 & 0xFFFFFFFF 其它移位同理

1.2 Python实现循环右移

1.2.1 右移位原理介绍

​ 设你要移动的数 是一个 4字节(32bit)的数 要移动八位 则原理如下: 0x12345678 ror 8(bit) = 0x78123456

1.首先得到 123456 也就是 >> 8位即可得到 2.然后得到 78 也就是 << 24位即可得到 得到的两个值进行|运算 也就是 0x78 | 0x 123456 = 0x78123456 0x78 | 0x 123456 = 0x78123456 但是python的特殊性 你移位之后数值不会溢出,所以我们只需要相应位数的字节即可. 也就是我们只要32位(4个字节) 最后&0xFFFFFFFF 就可以 得到我们想要的数值的

二丶代码示例

2.1 代码介绍

上面说了下移位的原理那么这里介绍下Python代码的使用

以循环右移为例子 总共就分为下类三个函数

代码语言:javascript
复制
rorCustomByte       自定义定制字节移位 
rorCustomBit        自定义Bit移位
ror8bit             按照8位数值 进行移位

下面说下什么意思

2.2 完成Python代码

代码语言:javascript
复制
__author__ = 'IBinary blob https://www.cnblogs.com/ibinary/'

class RorObj():
    def __init__(self):
        pass
    #字节循环移位
    #参数1 要移动的值
    #参数2 值是多少个字节
    #参数3 要移动的字节位数
    def rorCustomByte(self,v, Byte, shiftByte):
        shiftByte = (shiftByte * 4) & (Byte * 4 - 1)  # 按照bit值 来进行设置移动位数
        if shiftByte == 0:
            return v
        a1 = (v >> (shiftByte))  # 右移shift位 空出高位shift位
        a2 = (v << ((Byte * 4) - shiftByte))  # 计算出剩下要移动的位数
        _ = '0x' + 'F' * Byte
        FfValue = int(_, 16)
        value = (a2 | a1) & FfValue
        return value

        # 循环右移n位
        # 参数1 要移动的值
        # 参数2 你的这个是是多少位的. 比如 0x12345678 就是32位
        # 参数3 你要移动多少位 比如 4位  那么移动之后就是 0x81234567

    def rorCustomBit(self, v, Bytebit, shift):
        shift &= (Bytebit - 1)  # 按照bit值 来进行设置移动位数
        if shift == 0:
            return v
        a1 = (v >> shift)  # 右移shift位 空出高位shift位
        a2 = (v << ((Bytebit) - shift))  # 计算出剩下要移动的位数
        l = [x for x in range(4, Bytebit + 1) if x % 4 == 0]
        LCount = len(l)
        _ = '0x' + 'F' * LCount
        FfValue = int(_, 16)
        value = (a2 | a1) & FfValue
        return value
    def ror4Bit(self, v, shift):
        shift &= 3
        if shift == 0:
            return v
        return ((v >> shift) | (v << (4 - shift))) & 0xF
    def ror8Bit(self,v, shift):
        shift &= 7
        if shift == 0:
            return v
        return ((v >> shift) | (v << (8 - shift))) & 0xFF
    def ror12Bit(self,v, shift):
        shift &= 11
        if shift == 0:
            return v
        return ((v >> shift) | (v << (12 - shift))) & 0xFFF
    def ror16Bit(self,v, shift):
        shift &= 15
        if shift == 0:
            return v  # value 右移shift位
        return ((v >> shift) | (v << (16 - shift))) & 0xFFFF
    def ror20Bit(self,v, shift):
        if shift == 0:
            return v  # value 右移shift位
        return ((v >> shift) | (v << (20 - shift))) & 0xFFFFF
    def ror24Bit(self,v, shift):
        shift &= 23
        if shift == 0:
            return v  # value 右移shift位
        return ((v >> shift) | (v << (24 - shift))) & 0xFFFFFF
    def ror28Bit(self,v, shift):
        if shift == 0:
            return v  # value 右移shift位
        return ((v >> shift) | (v << (28 - shift))) & 0xFFFFFFF
    def ror32Bit(self,v, shift):
        shift &= 0x1F  # 设置要移动的位数
        if shift == 0:
            return v  # value 右移shift位
        return ((v >> shift) | (v << (32 - shift))) & 0xFFFFFFFF
class RolObl():
    def __init__(self):
        pass

    # 循环左移原理
    # 拿一个32位的数(4个字节)来说 进行移动八位
    # 如:
    # 0x12345678  rol 8 之后  = 0x34567812
    # 其原理如下:
    # 1.首先左移八位得到 0x345678
    # 2.然后右移24位得到 0x12
    # 最后 0x345678 | 0x12  = 0x34567812
    # 鉴于Python的特殊性.我们只需要32bit数即可. 也就是最后要 & 0xFFFFFFFF
    # 其它移位同理
    def rolCustomBit(self,v,bit,shift):
        shift &= (bit-1)
        if shift == 0:
            return v
        HightBit = v >> (bit - shift)
        LowBit = v << shift
        l = [x for x in range(4, bit + 1) if x % 4 == 0]
        LCount = len(l)
        _ = '0x' + 'F' * LCount
        FfValue = int(_, 16)

        Value = (HightBit | LowBit) & FfValue
        return Value

    #按照字节移位
    def rolCustomByte(self,v,Byte,shiftByte):
        shiftByte = (shiftByte * 4) & (Byte * 4 - 1)  # 按照bit值 来进行设置移动位数
        if shiftByte == 0:
            return v
        Low = (v << (shiftByte))  #左移shift位
        Hight = (v >> ((Byte * 4) - shiftByte))  # 计算出剩下要移动的位数
        _ = '0x' + 'F' * Byte
        FfValue = int(_, 16)
        value = (Hight | Low) & FfValue
        return value

    def rol4Bit(self,v,shift):
        shift &= 3
        if shift == 0:
            return  v
        HightBit = v >> (4 - shift)
        LowBit =  v << shift
        Value = (HightBit | LowBit) & 0xF
        return Value
    def rol8Bit(self,v,shift):
        shift &= 7
        if shift == 0:
            return  v
        HightBit = v >> (8 - shift)
        LowBit =  v << shift
        Value = (HightBit | LowBit) & 0xFF
        return Value

    def rol12Bit(self, v, shift):
        shift &= 11
        if shift == 0:
            return v
        HightBit = v >> (12 - shift)
        LowBit = v << shift
        Value = (HightBit | LowBit) & 0xFFF
        return Value

    def rol16Bit(self, v, shift):
        shift &= 15
        if shift == 0:
            return v
        HightBit = v >> (16 - shift)
        LowBit = v << shift
        Value = (HightBit | LowBit) & 0xFFFF
        return Value

    def rol20Bit(self, v, shift):
        if shift == 0:
            return v
        HightBit = v >> (20 - shift)
        LowBit = v << shift
        Value = (HightBit | LowBit) & 0xFFFFF
        return Value

    def rol24Bit(self, v, shift):
        shift &= 23
        if shift == 0:
            return v
        HightBit = v >> (24 - shift)
        LowBit = v << shift
        Value = (HightBit | LowBit) & 0xFFFFFF
        return Value

    def rol28Bit(self, v, shift):
        if shift == 0:
            return v
        HightBit = v >> (28 - shift)
        LowBit = v << shift
        Value = (HightBit | LowBit) & 0xFFFFFFF
        return Value
    def rol32bit(self,v,shift):
        shift &= 0x1F
        if shift == 0:
            return v
        HightBit = v >> (32 - shift)
        LowBit = v << shift
        value = (HightBit | LowBit) & 0xFFFFFFFF
        return value
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-12-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Python实现循环左移右移
    • 一丶Python实现循环左移 右移 原理
      • 1.1 Python实现循环左移
      • 1.2 Python实现循环右移
    • 二丶代码示例
      • 2.1 代码介绍
      • 2.2 完成Python代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档