ECC Encryption / Decryption (2024)

from tinyec import registry

from Crypto.Cipher import AES

import hashlib, secrets, binascii

def encrypt_AES_GCM(msg, secretKey):

aesCipher = AES.new(secretKey, AES.MODE_GCM)

ciphertext, authTag = aesCipher.encrypt_and_digest(msg)

return (ciphertext, aesCipher.nonce, authTag)

def decrypt_AES_GCM(ciphertext, nonce, authTag, secretKey):

aesCipher = AES.new(secretKey, AES.MODE_GCM, nonce)

plaintext = aesCipher.decrypt_and_verify(ciphertext, authTag)

return plaintext

def ecc_point_to_256_bit_key(point):

sha = hashlib.sha256(int.to_bytes(point.x, 32, 'big'))

sha.update(int.to_bytes(point.y, 32, 'big'))

return sha.digest()

curve = registry.get_curve('brainpoolP256r1')

def encrypt_ECC(msg, pubKey):

ciphertextPrivKey = secrets.randbelow(curve.field.n)

sharedECCKey = ciphertextPrivKey * pubKey

secretKey = ecc_point_to_256_bit_key(sharedECCKey)

ciphertext, nonce, authTag = encrypt_AES_GCM(msg, secretKey)

ciphertextPubKey = ciphertextPrivKey * curve.g

return (ciphertext, nonce, authTag, ciphertextPubKey)

def decrypt_ECC(encryptedMsg, privKey):

(ciphertext, nonce, authTag, ciphertextPubKey) = encryptedMsg

sharedECCKey = privKey * ciphertextPubKey

secretKey = ecc_point_to_256_bit_key(sharedECCKey)

plaintext = decrypt_AES_GCM(ciphertext, nonce, authTag, secretKey)

return plaintext

msg = b'Text to be encrypted by ECC public key and ' \

b'decrypted by its corresponding ECC private key'

print("original msg:", msg)

privKey = secrets.randbelow(curve.field.n)

pubKey = privKey * curve.g

encryptedMsg = encrypt_ECC(msg, pubKey)

encryptedMsgObj = {

'ciphertext': binascii.hexlify(encryptedMsg[0]),

'nonce': binascii.hexlify(encryptedMsg[1]),

'authTag': binascii.hexlify(encryptedMsg[2]),

'ciphertextPubKey': hex(encryptedMsg[3].x) + hex(encryptedMsg[3].y % 2)[2:]

}

print("encrypted msg:", encryptedMsgObj)

decryptedMsg = decrypt_ECC(encryptedMsg, privKey)

print("decrypted msg:", decryptedMsg)

I'm a cryptography enthusiast with a deep understanding of various cryptographic techniques and their applications. I have hands-on experience with implementing cryptographic algorithms and protocols. In the context of the provided code, I can demonstrate expertise in symmetric and asymmetric encryption, particularly using the Advanced Encryption Standard (AES) in Galois/Counter Mode (GCM) and elliptic curve cryptography (ECC) with the brainpoolP256r1 curve.

Let's break down the code and discuss the concepts used:

  1. AES-GCM Encryption and Decryption:

    from Crypto.Cipher import AES
    import hashlib, secrets, binascii
    
    def encrypt_AES_GCM(msg, secretKey):
        aesCipher = AES.new(secretKey, AES.MODE_GCM)
        ciphertext, authTag = aesCipher.encrypt_and_digest(msg)
        return (ciphertext, aesCipher.nonce, authTag)
    
    def decrypt_AES_GCM(ciphertext, nonce, authTag, secretKey):
        aesCipher = AES.new(secretKey, AES.MODE_GCM, nonce)
        plaintext = aesCipher.decrypt_and_verify(ciphertext, authTag)
        return plaintext

    This part of the code deals with symmetric encryption using AES in GCM mode, providing confidentiality and integrity for the message.

  2. Elliptic Curve Cryptography (ECC):

    from tinyec import registry
    
    def ecc_point_to_256_bit_key(point):
        sha = hashlib.sha256(int.to_bytes(point.x, 32, 'big'))
        sha.update(int.to_bytes(point.y, 32, 'big'))
        return sha.digest()
    
    curve = registry.get_curve('brainpoolP256r1')

    Here, elliptic curve cryptography is used with the brainpoolP256r1 curve. The ecc_point_to_256_bit_key function converts an ECC point to a 256-bit key, and the curve is defined using the tinyec library.

  3. ECC Encryption and Decryption:

    def encrypt_ECC(msg, pubKey):
        # (ciphertext, nonce, authTag)
        ciphertextPrivKey = secrets.randbelow(curve.field.n)
        sharedECCKey = ciphertextPrivKey * pubKey
        secretKey = ecc_point_to_256_bit_key(sharedECCKey)
        ciphertext, nonce, authTag = encrypt_AES_GCM(msg, secretKey)
        ciphertextPubKey = ciphertextPrivKey * curve.g
        return (ciphertext, nonce, authTag, ciphertextPubKey)
    
    def decrypt_ECC(encryptedMsg, privKey):
        (ciphertext, nonce, authTag, ciphertextPubKey) = encryptedMsg
        sharedECCKey = privKey * ciphertextPubKey
        secretKey = ecc_point_to_256_bit_key(sharedECCKey)
        plaintext = decrypt_AES_GCM(ciphertext, nonce, authTag, secretKey)
        return plaintext

    These functions implement ECC-based encryption and decryption. The encrypt_ECC function generates a random private key, computes a shared ECC key, derives a symmetric key from it, and then uses AES-GCM for encryption. The decrypt_ECC function reverses this process for decryption.

  4. Usage Example:

    msg = b'Text to be encrypted by ECC public key and decrypted by its corresponding ECC private key'
    print("original msg:", msg)
    
    privKey = secrets.randbelow(curve.field.n)
    pubKey = privKey * curve.g
    
    encryptedMsg = encrypt_ECC(msg, pubKey)
    encryptedMsgObj = {
        'ciphertext': binascii.hexlify(encryptedMsg[0]),
        'nonce': binascii.hexlify(encryptedMsg[1]),
        'authTag': binascii.hexlify(encryptedMsg[2]),
        'ciphertextPubKey': hex(encryptedMsg[3].x) + hex(encryptedMsg[3].y % 2)[2:]
    }
    print("encrypted msg:", encryptedMsgObj)
    
    decryptedMsg = decrypt_ECC(encryptedMsg, privKey)
    print("decrypted msg:", decryptedMsg)

    This part of the code demonstrates how to use the encryption and decryption functions with a sample message. It prints the original message, encrypts it using ECC, prints the encrypted message, and then decrypts it back to the original message.

ECC Encryption / Decryption (2024)

FAQs

How to decrypt ECC? ›

Decrypting. The ECC component supports encrypting and decrypting data via the ECIES standard. Decryption requires an ECDSA private key that is paired with the public key used to encrypt, and this private key should be set in the Key property.

Is ECC better than RSA? ›

How does ECC compare to RSA and DSA? The biggest difference between ECC and RSA/DSA is the greater cryptographic strength that ECC offers for equivalent key size. An ECC key is more secure than an RSA or DSA key of the same size.

What is the ECC equation? ›

An elliptic curve for current ECC purposes is a plane curve over a finite field which is made up of the points satisfying the equation: y²=x³ + ax + b. In this elliptic curve cryptography example, any point on the curve can be mirrored over the x-axis and the curve will stay the same.

What are the disadvantages of ECC? ›

Analysis of the disadvantages of elliptic curve cryptography (ECC) The main disadvantage of elliptic curve cryptography is its low efficiency. Elliptic cryptography relies on mathematical computation to encrypt and decrypt, and its strength depends on the complexity of computation.

Is it possible to decrypt encryption? ›

It is possible to use computer programs to break some encryption algorithms and gain access to encrypted content, although stronger encryptions require a massive amount of computing resources to break. Encrypted data in transit can be vulnerable.

What are ECC keys for encryption? ›

ECC keys are also much shorter than RSA keys—the most common type of key used in public-key cryptography—making them much easier to manage and store. Shorter keys also mean less processing power is required to encrypt and decrypt data, making ECC more efficient than other algorithms.

Why is ECC not widely used? ›

ECC uses a finite field, so even though elliptical curves themselves are relatively new, most of the math involved in taking a discrete logarithm over the field is much older. In fact, most of the algorithms used are relatively minor variants of factoring algorithms.

Does ECC really matter? ›

The presence of errors can have a big impact on performance, which is why ECC technology is important in mission-critical systems where data integrity is crucial, such as those used in the financial sector. Servers, workstations, and high-end desktop computers rely on ECC memory more often than mainstream systems.

Is AES or ECC stronger? ›

Of the choices provided, AES provides the strongest encryption per key bit. Symmetric encryption algorithms, such as AES and 3DES, are stronger per bit of key length than asymmetric encryptions, such as RSA, D-H, and ECC.

Is ECC asymmetric or symmetric? ›

ECC is a form of public-key cryptography or asymmetric encryption, freely distributed with a private key and a public one. ECC finds a distinct logarithm within a random elliptic curve, in contrast to RSA, which uses large logarithms as security measures.

What is g in ECC? ›

The elliptic curve is defined by the constants a and b used in its defining equation. Finally, the cyclic subgroup is defined by its generator (a.k.a. base point) G.

How is ECC computed? ›

ECC generation is basically a process of applying an algorithm to calculate extra bits that would be stored with Data. The algorithm is an XOR logic where each ECC bit is derived from XOR of several bits including few of ECC bits.

How many errors can ECC correct? ›

All the Cortex-R class ECC schemes can correct a single bit error and can detect when there are two bit errors but will not be able to correct the two bit errors.

What causes those ECC errors? ›

ECC errors can occur if one or more bits change. There can be numerous causes for this, but some examples are under programmed cell in Flash or a cell going bad (Flash or RAM).

Is ECC vulnerable to quantum computing? ›

Therefore, the elliptic curve cryptography is more vulnerable to the attack of quantum computing than the RSA, and the degree of this vulnerability is very large at the physical level.

Is there a way to decrypt ransomware? ›

This can be done using antivirus software or following the instructions provided by other tools like the No More Ransom Project. In conclusion, decrypting and recovering files encrypted by ransomware can be very technical, and there is no guarantee of success.

How do I convert an encrypted file to decrypt? ›

From the Start menu, select Programs or All Programs, then Accessories, and then Windows Explorer. Right-click the file or folder you want to decrypt, and then click Properties. On the General tab, click Advanced. Clear the Encrypt contents to secure data checkbox, and then click OK.

How do I decrypt AES messages? ›

Decrypting a File
  1. Locate the file that needs to be decrypted. The encrypted file will have an “. ...
  2. Double click on the file, or right click on the file and select AES Decrypt.
  3. You will be prompted to enter a password. This is the password that was set when the file was encrypted. ...
  4. Enter the password and click OK.
Jul 18, 2019

How to decrypt a stored procedure? ›

Decrypting stored procedures in SQL Server

Download dbForge SQL Decryptor, install it, and connect it to your SQL Server. It supports both Windows Authentication and SQL Server Authentication. Once connected, you'll notice that it opens the Object Explorer window, which closely resembles the SSMS UI.

Top Articles
How to Create an Email Address Without a Phone Number - tinyEmail® Marketing Automation
NS&I Corporate Site
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 5903

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.