安全学

第一题:

import random
import math
from random import choice


def createPrime(num):
    isPrime = [0]*(num+1)
    primeList = []
    isPrime[2] = 1
    for i in range(3,num,2):
        isPrime[i] = 1
    for i in range(3,int(math.sqrt((num+1)))+1,2):
        if(isPrime[i] == 1):
            for j in range(i*i,num+1,2*i):
                isPrime[j] = 0
    for i in range(2,num+1):
        if (isPrime[i]==1):
            primeList.append(i)
    global a,b
    while True:
        p,q = (random.sample(primeList,2))
        if(p!=q):
            a,b = p,q
            break
    return a,b


def isCoprime(a,b):
    flag = 0
    if(b>a):
        a,b = b,a
    while(a%b):
        tmp = b
        b = a%b
        a = tmp
    if(b==1):
        flag = 1
    return flag


def getZnStar(num):
    znStar = []
    p,q = getRandomPrime(num)
    print("p is %d, q is %d"%(p,q))
    N = p*q
    print("N is %d"%N)
    for i in range(1,N):
        if(isCoprime(N,i)==1):
            znStar.append(i)
    print("Zn_star is " + str(znStar))
    return znStar,N,p,q


def isEveryInverse(znStar,N):
    count = 0
    size = len(znStar)
    for i in range(0,size):
        for j in range(0,size):
            if((znStar[i] * znStar[j]) % N == 1):
                count = count + 1
                break
    if(count==size):
        print("每一个元素都有逆元")
        return 1
    else:
        print("不是每一个元素都有逆元")
        return 0


def isGroup(znStar,N):
    closure_flag = 0
    exchange_flag = 0
    unit_flag = 0
    size = len(znStar)
    a,b = random.sample(range(0,size),2)
    c = choice(range(0,size))
    for i in range(0,size):
        if((znStar[a] * znStar[b]) % N == znStar[i]):
            closure_flag = 1
            break
    if ((((znStar[a]*znStar[b])%N)*znStar[c])%N == (((znStar[b]*znStar[c])%N)*znStar[a])%N):
            exchange_flag=1
    if(znStar[0] == 1):
            unit_flag = 1
    if(closure_flag == 1 and exchange_flag == 1 and unit_flag == 1 and isEveryInverse(znStar,N)==1):
            print ("是群")
    else:
            print ("不是群")


def Number(znStar,p,q):
    size = len(znStar)
    if (size == (p-1)*(q-1)):
        print ("大小为(p-1)*(q-1)")
    else:
        print("大小为(p-1)*(q-1)")


if __name__=="__main__":
    znStar,N,p,q = getZnStar(100)
    isGroup(znStar,N)
    Number(znStar,p,q)

第二题:

def ROTL8(x,shift) : 
        return 0xff & (((x)<<(shift))|((x)>>(8-(shift))))


def initialize_aes_sbox() :
        sbox = [None] * 256
        p = q = 1
        firstTime = True


       
        while p != 1 or firstTime :
                p = p ^ (p << 1) ^ (0x1B if p & 0x80 else 0)
                p = p & 0xff
                q ^= q << 1
                q ^= q << 2
                q ^= q << 4
                q ^= 0x09 if q & 0x80 else 0
                q = q & 0xff
                xformed = q ^ ROTL8(q, 1) ^ ROTL8(q, 2) ^ ROTL8(q, 3) ^ ROTL8(q, 4)


                sbox[p] = xformed ^ 0x63
                firstTime = False
        sbox[0] = 0x63


        return sbox

你可能感兴趣的:(安全学)