Beispiel #1
0
def tests_low():
    from slowSM4 import SM4
    from time import time
    KEY = "01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10".replace(" ","")
    DATA = unhexs(KEY)
    obj = SM4(KEY)
    s1 = time()
    res = obj.encrypt(DATA)
    for i in xrange(9999):
        res = obj.encrypt(res)
        if i% 1000 ==500:
            print 500 / (time()-s1) , "times/s"
            s1=time()
            print(hexs(res))
    print hexs(res)
Beispiel #2
0
def tests_low():
    from slowSM4 import SM4
    from time import time
    KEY = "01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10".replace(" ", "")
    DATA = unhexs(KEY)
    obj = SM4(KEY)
    s1 = time()
    res = obj.encrypt(DATA)
    for i in xrange(9999):
        res = obj.encrypt(res)
        if i % 1000 == 500:
            print 500 / (time() - s1), "times/s"
            s1 = time()
            print(hexs(res))
    print hexs(res)
Beispiel #3
0
def test_fast():
    import pySM4 as sm4
    from binascii import hexlify as hexs,unhexlify as unhexs
    key=unhexs("0123456789ABCDEFFEDCBA9876543210")
    clear= key
    cipher=clear 
    for i in xrange(10**6):
        cipher  = sm4.encrypt(key, cipher)

    print hexs(cipher)
    clear=unhexs("595298c7c6fd271f0402f804c33d3f66")
    cipher=clear 
    for i in xrange(10**6):
        cipher  = sm4.decrypt(key, cipher)

    print hexs(cipher)
Beispiel #4
0
def test_fast():
    import pySM4 as sm4
    from binascii import hexlify as hexs, unhexlify as unhexs
    key = unhexs("0123456789ABCDEFFEDCBA9876543210")
    clear = key
    cipher = clear
    for i in xrange(10**6):
        cipher = sm4.encrypt(key, cipher)

    print hexs(cipher)
    clear = unhexs("595298c7c6fd271f0402f804c33d3f66")
    cipher = clear
    for i in xrange(10**6):
        cipher = sm4.decrypt(key, cipher)

    print hexs(cipher)
Beispiel #5
0
def test ():
    log.info( "XORMAC: 64bits:" + hexs(getMAC(unhexs("0123456789ABCDEF"), unhexs("12345678901234567890"), 'DES-XOR')).upper() )
    log.info( "XORMAC:128bits:" + hexs(getMAC(unhexs("0123456789ABCDEFFEDCBA9876543210"), unhexs("12345678901234567890"), 'DES-XOR')).upper() )
    log.info( "XORMAC:192bits:" + hexs(getMAC(unhexs("0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"), unhexs("12345678901234567890"), 'DES-XOR')).upper() )
    log.info( "CBCMAC: 64bits:" + hexs(getMAC(unhexs("0123456789ABCDEF"), unhexs("12345678901234567890"), 'DES-CBC')).upper() )
    log.info( "ECBMAC:128bits:" + hexs(getMAC(unhexs("0123456789ABCDEFFEDCBA9876543210"), unhexs("12345678901234567890"), 'DES-ECB')).upper() )
    log.info( "3DESMAC:128bits:" + hexs(getMAC(unhexs("0123456789ABCDEFFEDCBA9876543210"), unhexs("12345678901234567890"), '3DES')).upper() )
Beispiel #6
0
def checkKeyOdd( hexKey ):
    u"""
    #当实际数据中 "1"的个数为偶数的时候,这个校验位就是"1",否则这个校验位就是"0“,这样就可以保证传送数据满足奇校验的要求
    #如果前7bit中 "1"的个数是 偶数个,则第8bit是1,这样满足是奇数个 1;
    #如果前7bit中 "1"的个数是 奇数个,则第8bit是0,这样满足是奇数个 1.
    """
    ret = ""
    for key in hexKey.decode("hex"):
        try :
            ret +=hexs(checkOdd(key))
        except Exception as e:
            print e
    return ret.upper()
Beispiel #7
0
def CVN(KEY, PAN, MM, YY, ServiceID):
    u"""
    KEY: CVV业务的明文密钥,双倍长,32个HexString
    MM:  失效月份 01-12 数字
    YY:  失效年份 00-99 数字
    PAN: 用户的IC卡号,19个数字字符
    ServiceID: 服务码3个数字
    """
    #去除空格
    KEY=KEY.replace(" ", "")
    PAN=PAN.replace(" ", "")
    if len(KEY) != 32:
        raise ValueError("KEY")
    #if len(PAN) != 19:
    #    raise ValueError("PAN")
    if MM not in range(1,13):
        raise ValueError("MM")
    if YY not in range(100):
        raise ValueError("YY")
    if ServiceID not in range(1000):
        raise ValueError("ServiceID")
    try:
        unhexs(KEY)
        int(PAN)
    except TypeError as et:
        raise ValueError("KEY")
    except ValueError as ev:
        raise ValueError("PAN")
    #检测完毕 计算
    KeyA=unhexs(KEY[:16])
    KeyB=unhexs(KEY[16:])
    log.debug(u'CVV step1:\nKeyA:['+ HexOut(hexs(KeyA).upper()) +']KeyB:['+HexOut(hexs(KeyB).upper())+']') 
    hData = "%s%02d%02d%03d" % (PAN, YY, MM, ServiceID)
    while len(hData) != 32:
        hData += "0"  
    bData = unhexs(hData)
    log.debug(u'CVV step2:'+HexOut(hexs(bData).upper()) )
    data1 = bData[:8]
    data2 = bData[8:]
    log.debug(u"CVV step3:\nBlock1:["+HexOut(hexs(data1).upper()) +"]Block2:["+HexOut(hexs(data2).upper()+']') )
    ka=DES(KeyA)
    kb=DES(KeyB)
    fData = ka.encrypt(data1)
    log.debug(u'CVV  step4:'+HexOut(hexs(fData).upper()) )
    fData = XOR(fData, data2)
    log.debug(u'CVV  step5:'+HexOut(hexs(fData).upper()) )
    ###以下3步加解密事实上就是3DES对于双倍长密钥的加密方式的拆分
    fData = ka.encrypt(fData)
    log.debug(u'CVV  step6:'+HexOut(hexs(fData).upper()) )
    fData = kb.decrypt(fData)
    log.debug(u'CVV  step7:'+HexOut(hexs(fData).upper()) )
    fData = ka.encrypt(fData)
    fHexs = hexs(fData).upper()
    resNum = getNum(fHexs)
    log.debug(u'CVV  step8:'+ HexOut(resNum) )
    resHex = getHex(fHexs)
    log.debug(u'CVV  step9.1:'+ HexOut(resHex) )
    resHex_Num = ""
    for i in resHex:
        resHex_Num += str(int(i,16) % 10)
    log.debug(u'CVV  step9.2:' + HexOut(resHex_Num) )
    Resault = resNum + resHex_Num
    log.debug(u'CVV  setp10:RES:' + HexOut(Resault) )
    return Resault[:3]