Exemplo n.º 1
0
 def init(self, key):
     self.S = [i2b(i, 1) for i in range(self.p)]
     self.T = [i2b(key[i % len(key)], 1) for i in range(self.p)]
     j = 0
     for i in range(self.p):
         j = (j + self.S[i][0] + self.T[i][0]) % self.p
         self.S[i], self.S[j] = self.S[j], self.S[i]
     print(self.S)
Exemplo n.º 2
0
    def decryptFile(self,
                    filename,
                    key,
                    IV,
                    mode='cbc',
                    padding='pkcs7',
                    coding='base64',
                    cfb_s=8):
        self.readFile(filename)
        if coding.lower() == 'base64':
            self.data = base64.b64decode(self.data)
        elif coding.lower() == 'hex':
            tmp = b''
            for i in range(0, len(self.data), 2):
                tmp += i2b(int(self.data[i:i + 2].decode('utf-8'), 16), 1)
            self.data = tmp
        else:
            raise ('没有这种编码方案')

        if mode.lower() == 'ecb':
            self.ECB_D(key)
        elif mode.lower() == 'cbc':
            self.CBC_D(key, IV)
        elif mode.lower() == 'cfb':
            self.CFB_D(key, IV, cfb_s)
        else:
            raise ('没有这种工作模式。')
        self.rePadding(padding)
        self.writeFile(filename, '.decrypt')
Exemplo n.º 3
0
def generateKey():
    # 密钥生成
    global AlicePrivateKey, AlicePublicKey, BobPrivateKey, BobPublicKey, sm4key, IV
    a = RSA()
    a.generateKey(nbits=512)
    AlicePrivateKey = a.outputPrivateKey(pathA)
    AlicePublicKey = a.outputPublicKey(pathB)
    print('Alice私钥生成成功,公钥已传输给Bob。')
    a.generateKey()
    BobPrivateKey = a.outputPrivateKey(pathB)
    BobPublicKey = a.outputPublicKey(pathA)
    print('Bob私钥生成成功,公钥已传输给Alice。')
    sm4key = IV = b''
    for i in range(16):
        sm4key += i2b(randint(0, 255), 1)
        IV += i2b(randint(0, 255), 1)
    print('对称加密密钥与初始向量生成成功。')
Exemplo n.º 4
0
def createFile():
    # 文件创建
    output = b''
    for i in range(fileSize(64)):
        output += i2b(randint(0, rang), 16)
    with open(sfile, 'wb') as f:
        f.write(output)
    print('文件创建成功。')
Exemplo n.º 5
0
 def CBC_D(self, key, IV):
     self.cdata = b''
     self.a.generateKey(key)
     for i in range(0, len(self.data), self.blocksize):
         tmp = self.a.aBlockDecode(self.data[i:i + self.blocksize])
         for j in range(self.blocksize):
             self.cdata += i2b(IV[j] ^ tmp[j], 1)
         IV = self.data[i:i + self.blocksize]
Exemplo n.º 6
0
 def CBC_E(self, key, IV):
     self.cdata = b''
     self.a.generateKey(key)
     for i in range(0, len(self.data), self.blocksize):
         tmp = b''
         # print(len(self.data))
         for j in range(self.blocksize):
             # print(j,i+j)
             tmp += i2b(IV[j] ^ self.data[i + j], 1)
         IV = self.a.aBlockEncode(tmp)
         self.cdata += IV
Exemplo n.º 7
0
 def aBlockEncode(self, message128):
     if len(message128) != 16:
         print('分组消息长度错误。')
     message128 = b2i(message128)
     X = cutNumber2List(message128, 128, 32)
     for i in range(32):
         X.append(self.F(X[i], X[i + 1], X[i + 2], X[i + 3], self.rk[i]))
     Y = X[32::]
     Y = Y[::-1]
     Y = mergeList2Number(Y, 32)
     Y = i2b(Y, 128 // 8)
     return Y
Exemplo n.º 8
0
 def aPadding(self, s):
     if s.lower() == 'zero':
         t = self.blocksize - len(self.data) % self.blocksize
         for _ in range(t):
             self.data += b'\x00'
     elif s.lower() == 'pkcs7':
         t = self.blocksize - len(self.data) % self.blocksize
         pad = i2b(t, 1)
         for _ in range(t):
             self.data += pad
     else:
         raise ('没有这种填充方式。')
Exemplo n.º 9
0
 def CFB_D(self, key, IV, cfb_s=8):
     if cfb_s % 8 != 0 or cfb_s > self.blocksize * 8:
         raise ('明文分组长度出错。')
     self.cdata = b''
     self.a.generateKey(key)
     for i in range(0, len(self.data), cfb_s // 8):
         tmp = b''
         eIV = self.a.aBlockEncode(IV)
         for j in range(cfb_s // 8):
             tmp += i2b(eIV[j] ^ self.data[i + j], 1)
         IV = IV[cfb_s // 8:] + self.data[i:i + cfb_s // 8]
         self.cdata += tmp
Exemplo n.º 10
0
def lm_hash(passwd):
    # 用户的密码转换为大写,并转换为16进制字符串
    passwd = passwd.upper().encode('utf-8')
    pswd = ''
    for i in passwd:
        pswd += hex(i)[2:].rjust(2, '0')
    passwd = pswd
    str_len = len(passwd)

    # 密码不足14字节将会用0来补全
    if str_len < 28:
        passwd = passwd.ljust(28, '0')

    # 固定长度的密码被分成两个7byte部分
    t_1 = passwd[0:14]
    t_2 = passwd[14:]

    # 每部分转换成比特流,并且长度位56bit,长度不足使用0在左边补齐长度
    t_1 = bin(int(t_1, 16)).lstrip('0b').rjust(56, '0')
    t_2 = bin(int(t_2, 16)).lstrip('0b').rjust(56, '0')

    # 再分7bit为一组末尾加0,组成新的编码
    t_1 = Zero_padding(t_1)
    t_2 = Zero_padding(t_2)
    t_1 = hex(int(t_1, 2))
    t_2 = hex(int(t_2, 2))
    t_1 = t_1[2:].rjust(16, '0')
    t_2 = t_2[2:].rjust(16, '0')
    t_1 = i2b(int(t_1, 16), 8)
    t_2 = i2b(int(t_2, 16), 8)

    a = DES()
    a.generateKey(t_1)
    LM_1 = a.aBlockEncode(magic)
    a.generateKey(t_2)
    LM_2 = a.aBlockEncode(magic)
    LM = hex(b2i(LM_1 + LM_2))[2:].rjust(32, '0').upper()
    return LM
Exemplo n.º 11
0
 def generateKey(self, key64):
     self.key = []
     if len(key64) != 8:
         raise ('初始密钥长度错误。')
     bits = byte2bit(key64)
     if self.checkKey(bits) == False:
         return False
     CD = self.doPC_1(key64)
     CD = b2i(CD)
     C, D = CD >> 28, CD & 0xFFFFFFF
     for i in range(16):
         C = aCycleLeftMove(C, 28, self.SHIFT[i])
         D = aCycleLeftMove(D, 28, self.SHIFT[i])
         self.key.append(self.doPC_2(i2b((C << 28) | D, 56 // 8)))
Exemplo n.º 12
0
 def getPadding(self, message):
     fir = b'\x80'
     other = b'\x00'
     length = len(message)
     if length % 64 == 56:
         message += fir + other * 63
     elif length % 64 < 56:
         message += fir + other * (56 - length % 64 - 1)
     else:
         message += fir + other * (120 - length % 64 - 1)
     length *= 8
     length %= 2**64
     message += i2b(length, 8)[::-1]
     return message
Exemplo n.º 13
0
 def Decrypt(self, messageBytes):
     if self.mode != 'private':
         raise ('请载入一个私钥。')
     return i2b(speed(b2i(messageBytes), self.d, self.n))
Exemplo n.º 14
0
 def Encrypt(self, messageBytes):
     if b2i(messageBytes) >= self.n:
         print('消息过长无法加密。')
     return i2b(speed(b2i(messageBytes), self.e, self.n))