0% found this document useful (0 votes)
45 views16 pages

Knapsack Encryption Algorithm in Cryptography

The Knapsack Encryption Algorithm, developed by Ralph Merkle and Martin Hellman in 1978, is a public key cryptography method that utilizes two knapsack problems: an easy one for the private key and a hard one for the public key. The algorithm involves encrypting plaintext by summing values from the public key based on binary representations and decrypting it using the private key. Despite its historical significance, the algorithm was found to be insecure after the discovery of Shamir's attack in 1982.

Uploaded by

urjahahaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views16 pages

Knapsack Encryption Algorithm in Cryptography

The Knapsack Encryption Algorithm, developed by Ralph Merkle and Martin Hellman in 1978, is a public key cryptography method that utilizes two knapsack problems: an easy one for the private key and a hard one for the public key. The algorithm involves encrypting plaintext by summing values from the public key based on binary representations and decrypting it using the private key. Despite its historical significance, the algorithm was found to be insecure after the discovery of Shamir's attack in 1982.

Uploaded by

urjahahaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Knapsack Encryption Algorithm in Cryptography

Knapsack Encryption Algorithm is the first general public key cryptography algorithm. It was
developed by Ralph Merkle and Mertin Hellman in 1978. As it is a Public key cryptography,
it needs two different keys. One is the Public key which is used for the Encryption process
and the other one is the Private key which is used for the Decryption process. In this
algorithm, we will use two different knapsack problems one is easy and the other one is
hard.
The easy knapsack is used as the private key and the hard knapsack is used as the public key.
The easy knapsack is used to derive the hard knapsack. For the easy knapsack, we will
choose a super-increasing problem. Super increasing knapsack is a sequence in which every
next term is greater than the sum of all preceding terms.
Example –
{1, 2, 4, 10, 20, 40} is a super increasing as​
1<2, 1+2<4, 1+2+4<10, 1+2+4+10<20 and 1+2+4+10+20<40.
Derive the Public key
●​ Step-1: Choose a super increasing knapsack {1, 2, 4, 10, 20, 40} as the private key. ​

●​ Step-2: Choose two numbers n and m. Multiply all the values of the private key by
the number n and then find modulo m. The value of m must be greater than the sum
of all values in the private key, for example, 110. The number n should have no
common factor with m, for example, 31. ​

●​ Step-3: Calculate the values of the Public key using m and n.


1x31 mod(110) = 31​
2x31 mod(110) = 62​
4x31 mod(110) = 14​
10x31 mod(110) = 90​
20x31 mod(110) = 70​
40x31 mod(110) = 30
●​ Thus, our public key is {31, 62, 14, 90, 70, 30} ​
And Private key is {1, 2, 4, 10, 20, 40}. ​

Now take an example for understanding the process of encryption and decryption.
Example – Let our plain text be 100100111100101110.
1. Encryption : As our knapsacks contain six values, so we will split our plain text into groups
of six:
100100 111100 101110
Multiply each value of the public key with the corresponding values of each group and take
their sum.
100100 {31, 62, 14, 90, 70, 30}​
1x31+0x62+0x14+1x90+0x70+0x30 = 121​
111100 {31, 62, 14, 90, 70, 30}​
1x31+1x62+1x14+1x90+0x70+0x30 = 197​
101110 {31, 62, 14, 90, 70, 30}​
1x31+0x62+1x14+1x90+1x70+0x30 = 205
So, our cipher text is 121 197 205.
2. Decryption : The receiver receives the cipher text which has to be decrypted. The receiver
also knows the values of m and n. ​
So, first, we need to find the n−1 n−1 , which is the multiplicative inverse of n mod m
i.e.,
n x n−1n−1 mod(m) = 131 xn−1n−1 mod(110) = 1n−1n−1 = 71
Now, we have to multiply 71 with each block of cipher text and take modulo m.
121 x 71 mod(110) = 11
Then, we will have to make the sum of 11 from the values of private key {1, 2, 4, 10, 20, 40}
i.e., 1+10=11 so make the corresponding bits 1 and others 0 which is 100100. Similarly,
197 x 71 mod(110) = 17​
1+2+4+10=17 = 111100​
And, 205 x 71 mod(110) = 35​
1+4+10+20=35 = 101110
After combining them we get the decoded text. ​
100100111100101110 which is our plain text.
Example:
Here’s a Python example of the Knapsack Encryption Algorithm with proper explanation and
output:
import random

# Function to generate a super-increasing sequence for the public key


def generate_super_increasing_sequence(n):
sequence = [random.randint(1, 100)]
while len(sequence) < n:
next_element = sum(sequence) + random.randint(1, 10)
sequence.append(next_element)
return sequence

# Function to generate the private key from the public key


def generate_private_key(public_key, q, r):
private_key = [(r * element) % q for element in public_key]
return private_key

# Function to encrypt the plaintext using the public key


def knapsack_encrypt(plaintext, public_key):
encrypted_message = sum(public_key[i] for i in range(len(plaintext)) if plaintext[i] == '1')
return encrypted_message

# Function to decrypt the ciphertext using the private key


def knapsack_decrypt(ciphertext, private_key, q):
r_inverse = pow(r, -1, q) # Modular multiplicative inverse of r
decrypted_message = ''
for element in reversed(private_key):
if (ciphertext * r_inverse) % q >= element:
decrypted_message = '1' + decrypted_message
ciphertext -= element
else:
decrypted_message = '0' + decrypted_message
return decrypted_message

# Example usage
if __name__ == "__main__":
n = 8 # Number of elements in the super-increasing sequence
q = 103 # Modulus (should be greater than the sum of the super-increasing sequence)
r = 3 # Multiplier for generating private key

# Generate the public key and private key


public_key = generate_super_increasing_sequence(n)
private_key = generate_private_key(public_key, q, r)

plaintext = "11001010"
ciphertext = knapsack_encrypt(plaintext, public_key)
decrypted_message = knapsack_decrypt(ciphertext, private_key, q)

print("Original Message:", plaintext)


print("Encrypted Ciphertext:", ciphertext)
print("Decrypted Message:", decrypted_message)
Explanation:
1.​ The ‘generate_super_increasing_sequence()' function generates a super-increasing
sequence, which serves as the public key. The function starts with a random element
and adds random values to it until the sequence reaches the desired length n.
2.​ The ‘generate_private_key()' the function generates the private key from the public
key using the formula: ‘private_key[i] = (r * public_key[i]) % q'.
3.​ The ‘knapsack_encrypt()' function takes the plaintext and the public key as input. It
converts the plaintext to binary and encrypts it by summing the elements of the
public key corresponding to ‘1’ bits in the binary representation of the plaintext.
4.​ The ‘knapsack_decrypt()' function takes the ciphertext, private key, and modulus q as
input. It uses the private key to reconstruct the original binary representation of the
plaintext by finding ‘1’ bits in the ciphertext and subtracting the corresponding
elements from the private key.
Output:
Original Message: 11001010​
Encrypted Ciphertext: 426​
Decrypted Message: 11001010
In this example, the plaintext “11001010” is encrypted using the Knapsack Encryption
Algorithm, resulting in the ciphertext “426”. The ciphertext is then decrypted back to the
original plaintext, which demonstrates the correctness of the algorithm.
Conclusion
The Knapsack Encryption Algorithm is an encryption technique that uses a public key based
on the knapsack problem. It changes messages into unreadable data without the private key.
But in the end, this system proved to be not secure and therefore could not be used for
protection, especially after 1982 when Shamir`s attack was discovered. Even though it was
significant for cryptographic development this method did not help improve security around
communications because of some weaknesses in its design.
Knapsack Encryption Algorithm in Cryptography -FAQs
What is the knapsack encryption algorithm?
Starting generation of user encryption keys begins as two knapsacks, one is public key while
the other is private key in knapsack cryptosystem, also known as Merkle-Hellman cipher. The
hard knapsack becomes the public key while the super-increasing knapsack serves as the
private key.
What is Merkle Hellman’s knapsack cryptosystem?
The Merkle–Hellman knapsack cryptosystem was one of the first public key cryptosystems,
published by Ralph Merkle and Martin Hellman in 1978. In 1984, Adi Shamir published an
attack that could be completed in a polynomial amount of time. Following this occurrence,
people have come to find out that the cryptosystem is not safe anymore.
What is the Merkle tree proof algorithm?
A proof of membership, also known as a Merkle proof, is a series of hashes that demonstrate
the membership of a given leaf within the tree. It is comprised of a proof for a particular leaf
node and includes a siblings of the leaf, and each non-leaf hash that could not have been
calculated without other leaf nodes.

Dreaming of M.Tech in IIT? Get AIR under 100 with our GATE 2026 CSE & DA courses! Get
flexible weekday/weekend options, live mentorship, and mock tests. Access exclusive
features like All India Mock Tests, and Doubt Solving—your GATE success starts now!

Comment
More info
Advertise with us
Next Article
RSA Algorithm in Cryptography
Similar Reads
Difference between Software Encryption and Hardware Encryption
Encryption is a vital component in securing digital information, and it can be implemented in
two primary ways: the first type is known as software encryption while the other type is
referred to as hardware encryption. As signified earlier, both means are used to safeguard
data by encoding it in such a way that any other person who intends to acces
8 min read

Difference Between Homomorphic Encryption and End-to-End Encryption


Homomorphic Encryption and End-to-End Encryption are two ways to protect data.
Homomorphic Encryption allows computations on encrypted data without needing to
decrypt it first. End-to-End Encryption, on the other hand, makes sure that only the sender
and receiver can read the messages, keeping them safe during transmission.What is
Homomorphic Encry
3 min read

Difference between Encryption and Cryptography


Encryption and Cryptography are the two terms that are generally used in Cyber Security.
Encryption is the process in which the data should be securely locked which means only
authorized users access the data while cryptography is the process of securing information
or data by using cryptographic algorithms so that any hacker or unauthorized person
4 min read

Encryption vs Digest in Cryptography


Encryption and Digest algorithms are used prominently in cryptography to protect the
information which is always in high demand. Both are used as protection for data, however,
their roles and capabilities of use are quite varied. Encryption replaces the normal or
readable form of information (plaintext) with an unreadable form (ciphertext) to maint
6 min read

Symmetric Encryption Cryptography in Java


Cryptography is the study of different techniques to secure data from an unauthorized
entity. In computer science, we try to develop strategies and practices for protecting
sensitive data. Most of the cryptography involves very advanced Mathematical functions
used for securing data. The sole purpose of the algorithms developed for cryptography is t
10 min read

Asymmetric Encryption Cryptography in Java


Cryptography is the study of different techniques to secure data from an unauthorized
entity. In computer science, we try to develop strategies and practices for protecting
sensitive data. Most of the cryptography involves very advanced Mathematical functions
used for securing data. The sole purpose of the algorithms developed for cryptography is t
4 min read

Fernet (symmetric encryption) using Cryptography module in Python


Cryptography is the practice of securing useful information while transmitting from one
computer to another or storing data on a computer. Cryptography deals with the encryption
of plaintext into ciphertext and decryption of ciphertext into plaintext. Python supports a
cryptography package that helps us encrypt and decrypt data. The fernet module o
3 min read

Custom Building Cryptography Algorithms (Hybrid Cryptography)


Cryptography can be defined as an art of encoding and decoding the patterns (in the form of
messages). Cryptography is a very straightforward concept which deals with manipulating
the strings (or text) to make them unreadable for the intermediate person. It has a very
effective way to encrypt or decrypts the text coming from the other parties. Some
15+ min read

Classical Cryptography and Quantum Cryptography


Cryptography is the technique that is used for secure communication between two parties in
a public environment where unauthorized users and malicious attackers are present. In
cryptography, there are two processes i.e. encryption and decryption performed at the
sender and receiver end respectively. Encryption is the process where simple multimedia
7 min read

RC4 Encryption Algorithm


RC4 is a stream cipher and variable-length key algorithm. This algorithm encrypts one byte
at a time (or larger units at a time). A key input is a pseudorandom bit generator that
produces a stream 8-bit number that is unpredictable without knowledge of the input key,
The output of the generator is called key-stream, and is combined one byte at a ti
6 min read

81k+ interested Geeks


DSA for Interview Preparation
Explore

42k+ interested Geeks


Complete Data Analytics Program
Explore

1k+ interested Geeks


Complete IP Addressing and Subnetting Course
Avail 90% Refund

13k+ interested Geeks


GATE CSE Rank Booster with Expert-Curated Questions
Avail 90% Refund
82k+ interested Geeks
Core Computer Science Subject for Interview Preparation
Avail 90% Refund

Corporate & Communications Address:


A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar
Pradesh, 201305

Advertise with us
●​ Company
●​ About Us
●​ Legal
●​ Privacy Policy
●​ Careers
●​ In Media
●​ Contact Us
●​ GfG Corporate Solution
●​ Placement Training Program
●​ Explore
●​ Job-A-Thon Hiring Challenge
●​ Hack-A-Thon
●​ GfG Weekly Contest
●​ Offline Classes (Delhi/NCR)
●​ DSA in JAVA/C++
●​ Master System Design
●​ Master CP
●​ GeeksforGeeks Videos
●​ Geeks Community
●​ Languages
●​ Python
●​ Java
●​ C++
●​ PHP
●​ GoLang
●​ SQL
●​ R Language
●​ Android Tutorial
●​ DSA
●​ Data Structures
●​ Algorithms
●​ DSA for Beginners
●​ Basic DSA Problems
●​ DSA Roadmap
●​ DSA Interview Questions
●​ Competitive Programming
●​ Data Science & ML
●​ Data Science With Python
●​ Data Science For Beginner
●​ Machine Learning
●​ ML Maths
●​ Data Visualisation
●​ Pandas
●​ NumPy
●​ NLP
●​ Deep Learning
●​ Web Technologies
●​ HTML
●​ CSS
●​ JavaScript
●​ TypeScript
●​ ReactJS
●​ NextJS
●​ NodeJs
●​ Bootstrap
●​ Tailwind CSS
●​ Python Tutorial
●​ Python Programming Examples
●​ Django Tutorial
●​ Python Projects
●​ Python Tkinter
●​ Web Scraping
●​ OpenCV Tutorial
●​ Python Interview Question
●​ Computer Science
●​ GATE CS Notes
●​ Operating Systems
●​ Computer Network
●​ Database Management System
●​ Software Engineering
●​ Digital Logic Design
●​ Engineering Maths
●​ DevOps
●​ Git
●​ AWS
●​ Docker
●​ Kubernetes
●​ Azure
●​ GCP
●​ DevOps Roadmap
●​ System Design
●​ High Level Design
●​ Low Level Design
●​ UML Diagrams
●​ Interview Guide
●​ Design Patterns
●​ OOAD
●​ System Design Bootcamp
●​ Interview Questions
●​ School Subjects
●​ Mathematics
●​ Physics
●​ Chemistry
●​ Biology
●​ Social Science
●​ English Grammar
●​ Software and Tools
●​ AI Tools Directory
●​ Marketing Tools Directory
●​ Accounting Software Directory
●​ HR Management Tools
●​ Editing Software Directory
●​ Microsoft Products and Apps
●​ Figma Tutorial
●​ Databases
●​ SQL
●​ MYSQL
●​ PostgreSQL
●​ PL/SQL
●​ MongoDB
●​ Preparation Corner
●​ Company-Wise Recruitment Process
●​ Resume Templates
●​ Aptitude Preparation
●​ Puzzles
●​ Company-Wise Preparation
●​ Companies
●​ Colleges
●​ Competitive Exams
●​ JEE Advanced
●​ UGC NET
●​ UPSC
●​ SSC CGL
●​ SBI PO
●​ SBI Clerk
●​ IBPS PO
●​ IBPS Clerk
●​ More Tutorials
●​ Software Development
●​ Software Testing
●​ Product Management
●​ Project Management
●​ Linux
●​ Excel
●​ All Cheat Sheets
●​ Recent Articles
●​ Free Online Tools
●​ Typing Test
●​ Image Editor
●​ Code Formatters
●​ Code Converters
●​ Currency Converter
●​ Random Number Generator
●​ Random Password Generator
●​ Write & Earn
●​ Write an Article
●​ Improve an Article
●​ Pick Topics to Write
●​ Share your Experiences
●​ Internships
●​ DSA/Placements
●​ DSA - Self Paced Course
●​ DSA in JavaScript - Self Paced Course
●​ DSA in Python - Self Paced
●​ C Programming Course Online - Learn C with Data Structures
●​ Complete Interview Preparation
●​ Master Competitive Programming
●​ Core CS Subject for Interview Preparation
●​ Mastering System Design: LLD to HLD
●​ Tech Interview 101 - From DSA to System Design [LIVE]
●​ DSA to Development [HYBRID]
●​ Placement Preparation Crash Course [LIVE]
●​ Development/Testing
●​ JavaScript Full Course
●​ React JS Course
●​ React Native Course
●​ Django Web Development Course
●​ Complete Bootstrap Course
●​ Full Stack Development - [LIVE]
●​ JAVA Backend Development - [LIVE]
●​ Complete Software Testing Course [LIVE]
●​ Android Mastery with Kotlin [LIVE]
●​ Machine Learning/Data Science
●​ Complete Machine Learning & Data Science Program - [LIVE]
●​ Data Analytics Training using Excel, SQL, Python & PowerBI - [LIVE]
●​ Data Science Training Program - [LIVE]
●​ Mastering Generative AI and ChatGPT
●​ Data Science Course with IBM Certification
●​ Programming Languages
●​ C Programming with Data Structures
●​ C++ Programming Course
●​ Java Programming Course
●​ Python Full Course
●​ Clouds/Devops
●​ DevOps Engineering
●​ AWS Solutions Architect Certification
●​ Salesforce Certified Administrator Course
●​ GATE 2026
●​ GATE CS Rank Booster
●​ GATE DA Rank Booster
●​ GATE CS & IT Course - 2026
●​ GATE DA Course 2026
●​ GATE Rank Predictor
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy

You might also like