在密码学中经常用到循环移位,现在自己做一个总结,并给出如下代码:
参数说明
value:数值
shiftBits:移动位数
bits:比特总长度
python代码
def life_round(value, shiftBits, bits):
t1 = (value >> (bits - shiftBits)) ^ (value << shiftBits)
t2 = ((2 ** bits) - 1)
return t1 & t2
def right_round(value, shiftBits, bits):
t1 = (value << (bits - shiftBits)) ^ (value >> shiftBits)
t2 = ((2 ** bits) - 1)
return t1 & t2
Java代码
public static int life_round(int value, int shiftBits, int bits) {
int t1 = (value >> (bits - shiftBits)) ^ (value << shiftBits);
int t2 = (Math.pow(2, bits) - 1);
return (t1 & t2);
}
public static int right_round(int value, int shiftBits, int bits) {
int t1 = (value << (bits - shiftBits)) ^ (value>> shiftBits);
int t2 = (Math.pow(2, bits) - 1);
return (t1 & t2);
}