Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Elliptic Curve Cryptography in Blockchain Directory #12064

Open
LitZeus opened this issue Oct 14, 2024 · 1 comment
Open

Adding Elliptic Curve Cryptography in Blockchain Directory #12064

LitZeus opened this issue Oct 14, 2024 · 1 comment
Labels
enhancement This PR modified some existing files

Comments

@LitZeus
Copy link

LitZeus commented Oct 14, 2024

Feature description

Objective:

Add a Python module named elliptic_curve_cryptography.py to the Blockchain directory, implementing the core principles of Elliptic Curve Cryptography (ECC) for securing blockchain transactions.

Details:

  • The module will include functions for generating ECC key pairs (public and private keys).
  • It will demonstrate how ECC can be used to sign and verify transactions within the blockchain.
  • Provide examples of using ECC for encryption and decryption processes.
  • Ensure compatibility with other cryptographic mechanisms already present in the blockchain project.

Benefits:

  • Strengthens the security of blockchain transactions using lightweight cryptography.
  • Enhances the ability to integrate with other blockchain features like digital signatures and smart contracts.
  • This feature will help optimize security with less computational overhead compared to other cryptographic methods.

Can you assign it to me under the hacktoberfest-accepted label?
I would like to work on this any many other blockchain related techs which I know about!

@LitZeus LitZeus added the enhancement This PR modified some existing files label Oct 14, 2024
@yuvashrikarunakaran
Copy link

from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import os

class ECC:
@staticmethod
def generate_key_pair():
"""
Generate an ECC private-public key pair.
"""
private_key = ec.generate_private_key(ec.SECP256R1(), default_backend())
public_key = private_key.public_key()
return private_key, public_key

@staticmethod
def sign_message(private_key, message):
    """
    Sign a message using the private key.
    """
    signature = private_key.sign(message, ec.ECDSA(hashes.SHA256()))
    return signature

@staticmethod
def verify_signature(public_key, message, signature):
    """
    Verify a message's signature using the public key.
    """
    try:
        public_key.verify(signature, message, ec.ECDSA(hashes.SHA256()))
        return True
    except Exception:
        return False

@staticmethod
def encrypt_message(public_key, message):
    """
    Encrypt a message using ECC public key.
    """
   
    shared_key = public_key.public_numbers().x.to_bytes(32, 'big')
    iv = os.urandom(16)  # Initialization Vector
    cipher = Cipher(algorithms.AES(shared_key[:16]), modes.CFB(iv), backend=default_backend())
    encryptor = cipher.encryptor()
    encrypted_message = encryptor.update(message) + encryptor.finalize()
    return encrypted_message, iv

@staticmethod
def decrypt_message(private_key, encrypted_message, iv):
    """
    Decrypt a message using ECC private key.
    """
   
    shared_key = private_key.private_numbers().private_value.to_bytes(32, 'big')
    cipher = Cipher(algorithms.AES(shared_key[:16]), modes.CFB(iv), backend=default_backend())
    decryptor = cipher.decryptor()
    decrypted_message = decryptor.update(encrypted_message) + decryptor.finalize()
    return decrypted_message

if name == "main":

private_key, public_key = ECC.generate_key_pair()


message = b"Blockchain transaction data"


signature = ECC.sign_message(private_key, message)
print("Signature:", signature)


is_valid = ECC.verify_signature(public_key, message, signature)
print("Signature valid:", is_valid)


encrypted_message, iv = ECC.encrypt_message(public_key, message)
print("Encrypted message:", encrypted_message)


decrypted_message = ECC.decrypt_message(private_key, encrypted_message, iv)
print("Decrypted message:", decrypted_message.decode())

You can use this code !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files
Projects
None yet
Development

No branches or pull requests

2 participants