示例#1
0
def eccDecrypt(data_cipher, privateKey):
    "Giải mã ECC với đầu vào là chuỗi bytes từ hình ảnh đã mã hoá"

    c = data_cipher[:64]
    C1 = (OS2IP(c[:32]), OS2IP(c[-32:]))
    C = EccMath.scalar_Point(privateKey, C1)
    C = EccMath.negatives_Point(C)

    data_cipher = data_cipher[64:]
    points = []
    for i in range(0, len(data_cipher), 64):
        data = data_cipher[i:i + 64]
        x = OS2IP(data[:32])
        y = OS2IP(data[-32:])
        points.append((x, y))

    plain_data = []
    for point in points:
        plain_data.append(elgamalDe(C, point))

    plain_bytes = []
    for j in plain_data:
        plain_bytes.append(I2OSP(j, 31))

    plain_data = b''.join(plain_bytes)

    plain_data = remove_padding(plain_data)

    return plain_data
示例#2
0
def eccDecrypt(data_cipher, privateKey):
    c = data_cipher[:64]
    C1 = (OS2IP(c[:32]), OS2IP(c[-32:]))
    C = EccMath.scalar_Point(privateKey, C1)
    C = EccMath.negatives_Point(C)

    data_cipher = data_cipher[64:]
    points = []
    for i in range(0, len(data_cipher), 64):
        data = data_cipher[i:i+64]
        x = OS2IP(data[:32])
        y = OS2IP(data[-32:])
        points.append((x, y))

    plain_data = []
    for point in points:
        plain_data.append(elgamalDecrypt(C, point))

    plain_bytes = []
    for j in plain_data:
        plain_bytes.append(I2OSP(j, 31))

    plain_data = b''.join(plain_bytes)

    return plain_data
示例#3
0
def eccEncrypt(data, publicKey):
    k = random.randint(1, n)
    C1 = EccMath.scalar_Point(k, G)
    P = EccMath.scalar_Point(k, publicKey)
    cipher_points = []
    for i in range(0, len(data), 31):
        plain = data[i:i+31]
        plain = OS2IP(plain)
        ecc_enc = elgamalEncrypt(plain, P)
        cipher_points.append(ecc_enc)

    cipher_bytes = []
    for point in cipher_points:
        point_to_bytes = I2OSP(point[0], 32) + I2OSP(point[1], 32)
        cipher_bytes.append(point_to_bytes)

    cipher_bytes = b''.join(cipher_bytes)
    c = I2OSP(C1[0], 32) + I2OSP(C1[1], 32)

    return c + cipher_bytes
示例#4
0
def eccEncrypt(rsa_value, publicKey):
    "Mã hoá ECC với đầu vào là chuỗi bytes đã được mã hoá bằng RSA"

    k = random.randint(1, n)
    C1 = EccMath.scalar_Point(k, G)
    P = EccMath.scalar_Point(k, publicKey)

    cipher_points = []
    for i in range(0, len(rsa_value), 31):
        plain = rsa_value[i:i + 31]
        plain = OS2IP(plain)
        ecc_enc = elgamalEn(plain, P)
        cipher_points.append(ecc_enc)

    cipher_bytes = []
    for point in cipher_points:
        point_to_bytes = I2OSP(point[0], 32) + I2OSP(point[1], 32)
        cipher_bytes.append(point_to_bytes)

    cipher_bytes = b''.join(cipher_bytes)
    c = I2OSP(C1[0], 32) + I2OSP(C1[1], 32)

    return c + cipher_bytes
示例#5
0
def elgamalDe(C, C2):
    "Giải mã Elgamal trả về bản rõ m"

    M = EccMath.add_Points(C2, C)
    m = EncodeECC.decode(M)
    return m
示例#6
0
def elgamalEn(m, P):
    "Mã hoá Elgamal tính giá trị C2"

    M = EncodeECC.encode(m)
    C2 = EccMath.add_Points(M, P)
    return C2
示例#7
0
def elgamalDecrypt(C, C2):
    M = EccMath.add_Points(C2, C)
    m = EncodeECC.decode(M)

    return m
示例#8
0
def elgamalEncrypt(m, P):
    M = EncodeECC.encode(m)
    C2 = EccMath.add_Points(M, P)
    return C2