Get a Pentest and security assessment of your IT network.

Cyber Security

Asymmetric Encryption for Data Integrity

TL;DR

Using asymmetric encryption (like RSA) to create a digital signature provides data integrity verification. It’s not a traditional hash function, but it achieves similar goals – confirming if data has been altered. This guide explains how to do it and why it works.

How Asymmetric Encryption Acts Like Hashing

Hashing functions create a fixed-size ‘fingerprint’ of data. Asymmetric encryption doesn’t directly produce a hash, but we can use its properties to achieve the same result: verifying if data is unchanged. The process involves:

  1. Signing the Data: Encrypting a hash of the data with your private key.
  2. Verification: Decrypting the signature using your corresponding public key and comparing it to a newly calculated hash of the original data. If they match, the data hasn’t been tampered with.

Step-by-Step Guide

This example uses OpenSSL for demonstration. You’ll need OpenSSL installed on your system.

1. Generate an RSA Key Pair

First, create a private and public key pair:

openssl genrsa -out private.pem 2048

This creates a 2048-bit RSA private key file named private.pem.

2. Extract the Public Key

Extract the public key from the private key:

openssl rsa -in private.pem -pubout -out public.pem

This creates a public key file named public.pem.

3. Create a Hash of Your Data

Let’s say your data is in a file called data.txt. Create a SHA-256 hash:

openssl dgst -sha256 data.txt

This will output the hexadecimal representation of the SHA-256 hash.

4. Sign the Hash with Your Private Key

Encrypt the hash using your private key:

openssl rsautl -sign -inkey private.pem -in data.txt -out signature.sig -sha256

This creates a file named signature.sig containing the encrypted hash (the digital signature).

5. Verify the Signature

Decrypt the signature using your public key and compare it to a new hash of the original data:

openssl rsautl -verify -inkey public.pem -in data.txt -signature signature.sig -sha256

If the verification is successful, you’ll see VERIFIED OK. If it fails, the data has been modified.

Important Considerations

  • Hash Algorithm: Choose a strong hash algorithm like SHA-256 or SHA-384.
  • Key Length: Use an appropriate key length (at least 2048 bits for RSA).
  • Padding Schemes: OpenSSL uses padding schemes automatically, but be aware of them in other implementations. PKCS#1 v1.5 is common, but OAEP offers better security.
  • Not a Replacement for True Hashing: Asymmetric encryption is slower than dedicated hashing algorithms. Use it when you need non-repudiation (proof of who signed the data) and integrity verification. For speed, use traditional hash functions where possible.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation