Ejemplo n.º 1
0
def decrypt_with_rsa_chunyu(msg):
    '''
    msg必须采用base64编码, 注意: base64编码的数据经过URLDecoder处理之后,可能不正确,其中的+会变成' '
    '''
    msg = base64ToString(msg)
    key = RSA.importKey(_private_rsa_key)
    cipher = PKCS1_v1_5_Cipher.new(key)

    modBits = number.size(key.n)
    k = ceil_div(modBits, 8)  # Convert from bits to bytes
    print "K: ", k

    msglen = len(msg)
    msg_encryted = ""
    start_idx = 0
    ## 处理过长的加密
    while msglen > 0:
        len1 = min([msglen, k])
        cleartext = cipher.decrypt(msg[start_idx:(start_idx + len1)], "")
        msg_encryted = msg_encryted + cleartext
        start_idx = start_idx + len1
        msglen = msglen - len1
    return msg_encryted
Ejemplo n.º 2
0
def decrypt_with_rsa_chunyu(msg):
    '''
    msg必须采用base64编码, 注意: base64编码的数据经过URLDecoder处理之后,可能不正确,其中的+会变成' '
    '''    
    msg = base64ToString(msg)
    key = RSA.importKey(_private_rsa_key)
    cipher = PKCS1_v1_5_Cipher.new(key)
    
    modBits = number.size(key.n)
    k = ceil_div(modBits,8) # Convert from bits to bytes
    print "K: ", k

    msglen = len(msg)
    msg_encryted = ""
    start_idx = 0
    ## 处理过长的加密
    while msglen > 0:
        len1 = min([msglen, k])
        cleartext = cipher.decrypt(msg[start_idx: (start_idx + len1)], "")
        msg_encryted = msg_encryted + cleartext
        start_idx = start_idx + len1
        msglen = msglen - len1
    return msg_encryted
Ejemplo n.º 3
0
def encrypt_with_rsa_chunyu(msg):
    '''
    msg必须采用utf8编码
    '''
    msg = ensure_utf8(msg)

    key = RSA.importKey(_public_rsa_key)
    cipher = PKCS1_v1_5_Cipher.new(key)

    modBits = number.size(key.n)
    k = ceil_div(modBits, 8) - 28  ## 11 # Convert from bits to bytes
    print "K: ", k

    msglen = len(msg)
    msg_encryted = ""
    start_idx = 0
    ## 处理过长的加密
    while msglen > 0:
        len1 = min([msglen, k])
        encrypt = cipher.encrypt(msg[start_idx:(start_idx + len1)])
        msg_encryted = msg_encryted + encrypt
        start_idx = start_idx + len1
        msglen = msglen - len1
    return stringToBase64(msg_encryted)
Ejemplo n.º 4
0
def encrypt_with_rsa_chunyu(msg):
    '''
    msg必须采用utf8编码
    '''
    msg = ensure_utf8(msg)

    key = RSA.importKey(_public_rsa_key)
    cipher = PKCS1_v1_5_Cipher.new(key)
    
    modBits = number.size(key.n)
    k = ceil_div(modBits,8) - 28 ## 11 # Convert from bits to bytes
    print "K: ", k

    msglen = len(msg)
    msg_encryted = ""
    start_idx = 0
    ## 处理过长的加密
    while msglen > 0:
        len1 = min([msglen, k])
        encrypt = cipher.encrypt(msg[start_idx: (start_idx + len1)])
        msg_encryted = msg_encryted + encrypt
        start_idx = start_idx + len1
        msglen = msglen - len1
    return stringToBase64(msg_encryted)