Knapsack Encryption Algorithm in Cryptography
Last Updated :
19 Jun, 2024
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
, which is the multiplicative inverse of n mod m i.e.,
n x n^-1 mod(m) = 131 xn^-1 mod(110) = 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:
Python
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:
- 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
. - 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'
. - 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. - 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.
Similar Reads
What is OSI Model? - Layers of OSI Model The OSI (Open Systems Interconnection) Model is a set of rules that explains how different computer systems communicate over a network. OSI Model was developed by the International Organization for Standardization (ISO). The OSI Model consists of 7 layers and each layer has specific functions and re
13 min read
TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While
7 min read
Types of Network Topology Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. Common types of network topology include bus, star, ring, mesh, and tree topologies, each with its advantages and disadvantages. In this article, we will discuss different types of n
12 min read
Computer Network Tutorial A Computer Network is a system where two or more devices are linked together to share data, resources and information. These networks can range from simple setups, like connecting two devices in your home, to massive global systems, like the Internet. Below are the main components of a computer netw
7 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Difference Between IPv4 and IPv6 IPv4 and IPv6 are two versions of the system that gives devices a unique address on the internet, known as the Internet Protocol (IP). IP is like a set of rules that helps devices send and receive data online. Since the internet is made up of billions of connected devices, each one needs its own spe
7 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
Network Devices (Hub, Repeater, Bridge, Switch, Router, Gateways and Brouter) Network devices are physical devices that allow hardware on a computer network to communicate and interact with each other. Network devices like hubs, repeaters, bridges, switches, routers, gateways, and brouter help manage and direct data flow in a network. They ensure efficient communication betwe
9 min read
RSA Algorithm in Cryptography RSA(Rivest-Shamir-Adleman) Algorithm is an asymmetric or public-key cryptography algorithm which means it works on two different keys: Public Key and Private Key. The Public Key is used for encryption and is known to everyone, while the Private Key is used for decryption and must be kept secret by t
13 min read
Types of Computer Networks A computer network is a system that connects many independent computers to share information (data) and resources. The integration of computers and other different devices allows users to communicate more easily. It is a collection of two or more computer systems that are linked together. A network
11 min read