The decryption method's key is not a number or a string, but a matrix, such as a 3*3 key:
So if we are going to encrypt a string of length n, then divide n by 3 and divide it into small sections of 3 letters of M, and encrypt each small segment with the utmost effort:
1. Divide the plaintext into m-small segments: {{P1,P2,P3},{P4,P5,P6} ... {.. PN}}
2. Encrypt each small segment: C1 = (k11*p1 + k21*p2 + k31*p3)
c2= (k12*p1 + k22*p2 + k32*p3)
c3= (k13*p1 + k23*p2 + k33*p3), i.e.:
Ciphertext C:[C1,C2,C3] = [P1,P2,P3] * KEY MoD 26,
In this way, the algorithm for Hill encryption can be obtained:
C = p*kmod26;
Then the corresponding decryption algorithm is:
P = C * k-1mod26.
Here is the Java implementation:
Package Com.owner.replace.multi;import Jama.matrix;import Com.owner.util.matrix.matrixutil;import java.text.numberformat;/** * Created by Wellmax on 2015/10/19. */public class Hill {private final static Matrix KEY = new Matrix (New Double[][]{{17,17,5},{21,18,21},{2,2,19}}); Private final static Matrixutil MU = new Matrixutil (KEY); Private final static Matrix N_key = Mu.inverse (); private static int chartoint (char c) {return (int) c-97; public string Encrypt (string input) {char[] chars = Input.tochararray (); int[] numbers = new Int[chars.length]; for (int i = 0; i < chars.length; i++) {Numbers[i] = Chartoint (Chars[i]); } int[] encrypts = Mu.rowmultiplymatrix (Numbers,mu.getmatrix ()); for (int i = 0; i < encrypts.length; i++) {Chars[i] = (char) (encrypts[i]+97); } return new String (chars); public string Decrypt (string input) {char[] chars = Input.tochararray (); int[] numbers = new Int[chars.length]; for (int i = 0; i < chars.length; i++) {Numbers[i] = Chartoint (Chars[i]); } int[] decrypts = Mu.rowmultiplymatrix (Numbers,n_key); for (int i = 0; i < decrypts.length; i++) {Chars[i] = (char) (decrypts[i]+97); } return new String (chars); public static void Main (string[] args) {Hill hill = new Hill (); String encrypt = Hill.encrypt ("Paymoremoney"); System.out.println (encrypt); String decrypt = Hill.decrypt (encrypt); System.out.println (decrypt); }}
For 3*3 Key, determine the final cryptographic result of a clear letter:
1.2 letters in the vicinity;
2. Key The value of a column
So for the frequency of a single letter in the ciphertext is completely irregular, the same for the 3*3 key, the frequency of two-letter pairs is also uncertain. As the key dimension grows, so does security, because the value of a key cannot be analyzed by the frequency of letters or letters. This is undoubtedly a qualitative improvement to other previous encryption.
But this encryption is still more easily cracked, a typical method is as follows:
1. Not all matrices have the inverse matrix of this algorithm, so that the value range of the key is reduced.
2. It is possible to verify the correctness of key by verifying that the key that is guessed in step 1 is correct, and we can use the guessing key to decipher the ciphertext.
These two steps will make it easier to decipher the key.
-wellmaxwang
4 Multi-table instead of Password Hill password 2 implementation