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
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
Shamir's Secret Sharing Algorithm | Cryptography
Cryptography is a technique of securing information and communications through the use of codes so that only those person for whom the information is intended can understand it and process it. Thus preventing unauthorized access to information. The prefix âcryptâ means âhiddenâ and suffix graphy mea
7 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 (plaint
6 min read
How Hashing Algorithm Used in Cryptography?
A Hash Function (H) takes a variable-length block of data and returns a hash value of a fixed size. A good hash function has a property that when it is applied to a large number of inputs, the outputs will be evenly distributed and appear random. Generally, the primary purpose of a hash function is
12 min read
RC5 Encryption Algorithm
RC5 is a symmetric key block encryption algorithm designed by Ron Rivest in 1994. It is notable for being simple, fast (on account of using only primitive computer operations like XOR, shift, etc.) and consumes less memory. Example: Key : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00Plain Text : 0
9 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 c
6 min read
Twofish Encryption Algorithm
When it comes to data protection, encryption methods act as our buffering agents. One example of an excellent block cipher is the Twofish encryption algorithm. Although it was a competitor of another block cipher in the Advanced Encryption Standard competition and was later succeeded by the latter,
6 min read
ElGamal Encryption Algorithm
ElGamal Encryption is a public-key cryptosystem. It uses asymmetric key encryption to communicate between two parties and encrypt the message. This cryptosystem is based on the difficulty of finding discrete logarithms in a cyclic group that is even if we know ga and gk, it is extremely difficult to
6 min read
Encryption, Its Algorithms And Its Future
Encryption plays a vital role in todayâs digital world, serving a major role in modern cyber security. It involves converting plain text into cipher text, ensuring that sensitive information remains secure from unauthorized access. By making data unreadable to unauthorized parties, encryption helps
10 min read
What is p-box in Cryptography?
P-boxes are permutation boxes which are usually one of the main components of a modern block cipher. They are also known as D-boxes or diffusion boxes. P-boxes are used in the block cipher called the data encryption standard commonly known as DES in cryptography. In this article, we will look into w
5 min read