Last active
April 14, 2019 10:27
-
-
Save J4ckKn1ght/69267cf0d10edde0f2972fd610df3839 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
baseList = [1] | |
table1 = [0, 1, 0, 1, 0, 1] | |
table2 = [1, 1, 0, 0, -1, -1] | |
#Kiem tra moi phan tu deu khac 0 | |
def noZero(aList): | |
return all(c != 0 for c in aList) | |
def encrypt1(list1, list2): | |
i = 0 | |
secretValue = 0 | |
while True: | |
if i >= len(list2): | |
break | |
newValue = 0 | |
for shiftValue in range(8): | |
a1 = (list1[i] >> shiftValue) & 1 | |
a2 = (list2[i] >> shiftValue) & 1 | |
index = a1 + a2 + secretValue + 2 | |
if table1[index]: | |
#Bat bit | |
newValue |= 1 << shiftValue | |
secretValue = table2[index] | |
# Neu do dai list1 nho hon list2 thi them phan tu vao list1 | |
if i == len(list1): | |
list1.append(0) | |
#Replace new value | |
list1[i] = newValue | |
i += 1 | |
while secretValue != 0: | |
# Neu list 1 ko du phan tu tinh tiep thi them vao | |
if i >= len(list1): | |
list1.append(0) | |
newValue = 0 | |
for shiftValue in range(8): | |
a = (list1[i] >> shiftValue) & 1 | |
index = a + secretValue + 2 | |
if table1[index]: | |
#Max only 255 | |
newValue |= 1 << shiftValue | |
secretValue = table2[index] | |
#Replace new value | |
list1[i] = newValue | |
i+= 1 | |
def encrypt2(list1, list2): | |
i = 0 | |
secretValue = 0 | |
while True: | |
if i >= len(list2): | |
break | |
newValue = 0 | |
for shiftValue in range(8): | |
a1 = (list1[i] >> shiftValue) & 1 | |
a2 = (list2[i] >> shiftValue) & 1 | |
#Chi khac voi encrypt1 tai day. Thay a1 + a2 = a1 - a2 | |
index = a1 - a2 + secretValue + 2 | |
if table1[index]: | |
#Bat bit len | |
newValue |= 1 << shiftValue | |
secretValue = table2[index] | |
#Neu do dai list1 nho hon list2 thi them phan tu vao list1 | |
if i == len(list1): | |
list1.append(0) | |
#Replace new value | |
list1[i] = newValue | |
i+= 1 | |
while secretValue != 0: | |
#Neu list 1 ko du phan tu tinh tiep thi them vao | |
if i >= len(list1): | |
list1.append(0) | |
newValue = 0 | |
for shiftValue in range(8): | |
a = (list1[i] >> shiftValue) & 1 | |
index = a + secretValue + 2 | |
if table1[index]: | |
newValue |= 1 << shiftValue | |
secretValue = table2[index] | |
#Replace new value | |
list1[i] = newValue | |
i += 1 | |
def RecursiveFunc(list1, list2): | |
if not noZero(list1): | |
resultList = [0] | |
encrypt1(resultList, list2) | |
encrypt1(resultList, baseList) | |
return resultList | |
elif not noZero(list2): | |
newList1 = [0] | |
encrypt1(newList1, list1) | |
encrypt2(newList1, baseList) | |
return RecursiveFunc(newList1, baseList) | |
else: | |
newList2 = [0] | |
encrypt1(newList2, list2) | |
encrypt2(newList2, baseList) | |
newList2 = RecursiveFunc(list1, newList2) | |
newList1 = [0] | |
encrypt1(newList1, list1) | |
encrypt2(newList1, baseList) | |
return RecursiveFunc(newList1, newList2) | |
if __name__ == '__main__': | |
aList = [0x1E] | |
result = RecursiveFunc(aList, aList) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment