The Merkle–Hellman knapsack cryptosystem was one of the earliest public key cryptosystems. It was published by Ralph Merkle and Martin Hellman in 1978. A polynomial time attack was published by Adi Shamir in 1984. As a result, the cryptosystem is now considered insecure.[1]: 465 [2]: 190
The concept of public key cryptography was introduced by Whitfield Diffie and Martin Hellman in 1976.[3] At that time they proposed the general concept of a "trap-door one-way function", a function whose inverse is computationally infeasible to calculate without some secret "trap-door information"; but they had not yet found a practical example of such a function. Several specific public-key cryptosystems were then proposed by other researchers over the next few years, such as RSA in 1977 and Merkle-Hellman in 1978.[4]
Merkle–Hellman is a public key cryptosystem, meaning that two keys are used, a public key for encryption and a private key for decryption. It is based on the subset sum problem (a special case of the knapsack problem).[5] The problem is as follows: given a set of integers A {\displaystyle A} and an integer c {\displaystyle c} , find a subset of A {\displaystyle A} which sums to c {\displaystyle c} . In general, this problem is known to be NP-complete. However, if A {\displaystyle A} is superincreasing, meaning that each element of the set is greater than the sum of all the numbers in the set lesser than it, the problem is "easy" and solvable in polynomial time with a simple greedy algorithm.
In Merkle–Hellman, decrypting a message requires solving an apparently "hard" knapsack problem. The private key contains a superincreasing list of numbers W {\displaystyle W} , and the public key contains a non-superincreasing list of numbers B {\displaystyle B} , which is actually a "disguised" version of W {\displaystyle W} . The private key also contains some "trapdoor" information that can be used to transform a hard knapsack problem using B {\displaystyle B} into an easy knapsack problem using W {\displaystyle W} .
Unlike some other public key cryptosystems such as RSA, the two keys in Merkle-Hellman are not interchangeable; the private key cannot be used for encryption. Thus Merkle-Hellman is not directly usable for authentication by cryptographic signing, although Shamir published a variant that can be used for signing.[6]
1. Choose a block size n {\displaystyle n} . Integers up to n {\displaystyle n} bits in length can be encrypted with this key.
2. Choose a random superincreasing sequence of n {\displaystyle n} positive integers
3. Choose a random integer q {\displaystyle q} such that
4. Choose a random integer r {\displaystyle r} such that gcd ( r , q ) = 1 {\displaystyle \gcd(r,q)=1} (that is, r {\displaystyle r} and q {\displaystyle q} are coprime).
5. Calculate the sequence
The public key is B {\displaystyle B} and the private key is ( W , q , r ) {\displaystyle (W,q,r)} .
Let m {\displaystyle m} be an n {\displaystyle n} -bit message consisting of bits m 1 m 2 … m n {\displaystyle m_{1}m_{2}\dots m_{n}} , with m 1 {\displaystyle m_{1}} the highest order bit. Select each b i {\displaystyle b_{i}} for which m i {\displaystyle m_{i}} is nonzero, and add them together. Equivalently, calculate
The ciphertext is c {\displaystyle c} .
To decrypt a ciphertext c {\displaystyle c} , we must find the subset of B {\displaystyle B} which sums to c {\displaystyle c} . We do this by transforming the problem into one of finding a subset of W {\displaystyle W} . That problem can be solved in polynomial time since W {\displaystyle W} is superincreasing.
1. Calculate the modular inverse of r {\displaystyle r} modulo q {\displaystyle q} using the Extended Euclidean algorithm. The inverse will exist since r {\displaystyle r} is coprime to q {\displaystyle q} .
2. Calculate
3. Solve the subset sum problem for c ′ {\displaystyle c'} using the superincreasing sequence W {\displaystyle W} , by the simple greedy algorithm described below. Let X = ( x 1 , x 2 , … , x k ) {\displaystyle X=(x_{1},x_{2},\dots ,x_{k})} be the resulting list of indexes of the elements of W {\displaystyle W} which sum to c ′ {\displaystyle c'} . (That is, c ′ = ∑ i = 1 k w x i {\displaystyle c'=\sum _{i=1}^{k}w_{x_{i}}} .)
4. Construct the message m {\displaystyle m} with a 1 in each x i {\displaystyle x_{i}} bit position and a 0 in all other bit positions:
This simple greedy algorithm finds the subset of a superincreasing sequence W {\displaystyle W} which sums to c ′ {\displaystyle c'} , in polynomial time:
Create a key to encrypt 8-bit numbers by creating a random superincreasing sequence of 8 values:
The sum of these is 706, so select a larger value for q {\displaystyle q} :
Choose r {\displaystyle r} to be coprime to q {\displaystyle q} :
Construct the public key B {\displaystyle B} by multiplying each element in W {\displaystyle W} by r {\displaystyle r} modulo q {\displaystyle q} :
Hence B = ( 295 , 592 , 301 , 14 , 28 , 353 , 120 , 236 ) {\displaystyle B=(295,592,301,14,28,353,120,236)} .
Let the 8-bit message be m = 97 = 01100001 2 {\displaystyle m=97=01100001_{2}} . We multiply each bit by the corresponding number in B {\displaystyle B} and add the results:
0 * 295 + 1 * 592 + 1 * 301 + 0 * 14 + 0 * 28 + 0 * 353 + 0 * 120 + 1 * 236 = 1129
The ciphertext c {\displaystyle c} is 1129.
To decrypt 1129, first use the Extended Euclidean Algorithm to find the modular inverse of r {\displaystyle r} mod q {\displaystyle q} :
Compute c ′ = c r ′ mod q = 1129 ∗ 442 mod 8 81 = 372 {\displaystyle c'=cr'{\bmod {q}}=1129*442{\bmod {8}}81=372} .
Use the greedy algorithm to decompose 372 into a sum of w i {\displaystyle w_{i}} values:
Thus 372 = 354 + 11 + 7 = w 8 + w 3 + w 2 {\displaystyle 372=354+11+7=w_{8}+w_{3}+w_{2}} , and the list of indexes is X = ( 8 , 3 , 2 ) {\displaystyle X=(8,3,2)} . The message can now be computed as
In 1984 Adi Shamir published an attack on the Merkle-Hellman cryptosystem which can decrypt encrypted messages in polynomial time without using the private key. [7] The attack analyzes the public key B = ( b 1 , b 2 , … , b n ) {\displaystyle B=(b_{1},b_{2},\dots ,b_{n})} and searches for a pair of numbers u {\displaystyle u} and m {\displaystyle m} such that ( u b i mod m ) {\displaystyle (ub_{i}{\bmod {m}})} is a superincreasing sequence. The ( u , m ) {\displaystyle (u,m)} pair found by the attack may not be equal to ( r ′ , q ) {\displaystyle (r',q)} in the private key, but like that pair it can be used to transform a hard knapsack problem using B {\displaystyle B} into an easy problem using a superincreasing sequence. The attack operates solely on the public key; no access to encrypted messages is necessary.
Shamir's attack on the Merkle-Hellman cryptosystem works in polynomial time even if the numbers in the public key are randomly shuffled, a step which is usually not included in the description of the cryptosystem, but can be helpful against some more primitive attacks.