Knapsack Encryption Algorithm in Cryptography
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.
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
# 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
plaintext = "11001010"
ciphertext = knapsack_encrypt(plaintext, public_key)
decrypted_message = knapsack_decrypt(ciphertext, private_key, q)
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
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