Exemple #1
0
def decrypt():
    print("请输入从平台复制得到的字符串: ")
    str = input()
    bhindex = str.find("密图哈希:")
    ehindex = str.find(',', bhindex)
    bkeyindex = str.find("密钥:")
    ekeyindex = str.find(",", bkeyindex)

    ehash = str[bhindex + 5:ehindex]
    rawkey = str[bkeyindex + 3:ekeyindex]

    url = "https://ipfs.io/ipfs/" + ehash
    imgpath = "enc.png"
    deimgpath = "output.png"

    # with request.urlopen(url) as web:
    #     # 为保险起见使用二进制写文件模式,防止编码错误
    #     with open(imgpath, 'wb') as outfile:
    #         outfile.write(web.read())
    print("请输入您的私钥文件路径:")
    keypath = input()
    if os.path.isfile(keypath):
        with open(keypath, 'r') as f:
            prikey = f.read()
        imgkey = SM2Python.SM2Decrypt(rawkey, prikey)
        rc = rc4.RC4(imgkey, imgpath, deimgpath)
        rc.encrypted()
        # sm4_d = sm4.Sm4()
        # sm4_d.sm4_set_key(imgkey, 1)  # 解密
        # sm4_d.sm4_crypt_ecb(imgpath, deimgpath)
        os.remove(imgpath)
        print("图片解密完成,您的原图为output.png")
    else:
        print("您输入的文件不存在")
Exemple #2
0
def strdecrypt():
    print("请输入等待解密的字符")
    encstr = input()
    # bhindex = encstr.find("对方公钥:")
    # ehindex = len(encstr)-1
    # enckey = encstr[bhindex+5: ehindex]
    print("请输入您的私钥文件路径:")
    keypath = input()
    if os.path.isfile(keypath):
        with open(keypath, 'r') as f:
            prikey = f.read()
        strdec = SM2Python.SM2Decrypt(encstr, prikey)
        dec_m = binascii.a2b_hex(strdec)
        print("解密出来的字符串:", dec_m.decode())
    else:
        print("您输入的文件不存在")
Exemple #3
0
def sm2_decrypt(cipher: str, prikey: str, length=None):
    '''
    返回十六进制明文字符串,length指明文是否有固定的长度
    '''
    incre = 254
    # 密文快为254*4bit
    block = [cipher[i:i + incre] for i in range(0, len(cipher), incre)]
    plain = []
    for each in block:
        plain.append(sm2.SM2Decrypt(each, prikey))
    plain = ''.join(plain)
    plain = list(plain)
    #print(plain)
    while plain[-1] == '0':
        if length != None and len(plain) > length:
            plain.pop()
        elif length != None and len(plain) <= length:
            break
        else:
            plain.pop()
    plain = ''.join(plain)
    return plain
Exemple #4
0
import SM2Python

str_k = "F6000277CA814FFF1D7BA2E499297B0E00F8575DCF5F3480C00FCB7DFFBA743E"
str_pubkey = "6456CC2649C6216281EE91DCDC5A75C8E92706C3C9B85362796E8E8277BB34A663C11AF6619F6C5A452626EF2703BE187681A816D988467DED48D17E5E54F613"
str_prikey = "50E7324D208DC091C089FB98FAEC64468EAE6789B0F707EDFE86EF7CB754DAEA"
str_plain = "B0448E89946BB21EC649FDF3BA46296602182849FBE2D329AAF843DE0D7CA7"

print("plain :\n" + str_plain)

str_cipher = SM2Python.SM2Encrypt(str_k, str_pubkey, str_plain)

print("cipher :\n" + str_cipher)

plain = SM2Python.SM2Decrypt(str_cipher, str_prikey)

print("decrypt cipher and get plain :\n" + plain)

input()