[Principles and Practice of Cryptography] (1) Implementation of shift cipher with java code

Shift Cipher

Reprint please famous source

Cryptosystem

A cryptosystem is a five-tuple (P, C, K, E, D) that meets the following conditions

P represents a finite set of all possible plaintexts

C represents a finite set of all possible ciphertexts

K represents the secret key space, a finite set of all possible keys

For each kK , there is an encryption rule e k ∈ E and a corresponding decryption rule d k ∈ D, and for e k : P->C, d k : C->p, the conditions are met: for each In plaintext x∈P, d k (e k (x)))=x

What you need to understand before learning shift cipher is that the basis of shift cipher is the modular operation in number theory. So first look at the basic definition of modular arithmetic.

Modular operation

Basic definition of modular operation

Assume that both a and b are integers, and m is a positive integer. If m divides ba, it can be expressed as a = b(mod m), the formula a=b(mod m)

Read as "a and b modulo m the same and", the positive integer m is called the modulus.

If you divide a and b by m, the corresponding quotient and remainder can be obtained, and the remainder is between 0 and m-1. That is to say, a and b are respectively expressed as a=q 1 m+r 1 , b=q 2 m+r 2 , where 0<=r1<=m-1,0<=r2<=m-1. This can be It can be seen that if a and b modulo m are congruent if and only if r1=r2. We use the notation a mod m to denote the remainder of dividing a by m. Therefore, a and b modulo m are congruent if and only if a mod m = b mod m. If a mod m is used to replace a, we say that a is reduced by mod m.

To put it simply, if a and b modulo m are the same, the remainder of dividing a by m and dividing b by m is the same.

Arithmetic operations on modulo m

Let Z m denote the set {0,1,2,...m-1}, on which two operations are defined, addition (+) and multiplication (×), whose operations are similar to those on the real number field of put, so The only difference is that the value obtained is the remainder after modulo.

Give a chestnut:

Calculate 11×13 on Z 16 , because 11×13=143 = 8×16+15, so 11×13=15 on Z 16 .

Algorithm
  1. Closed to addition operation: for any a,b∈Z m , a+b belongs to Z m
  2. The addition operation satisfies the commutative law: for any a, b∈Z m , there is a+b=b+a
  3. The addition operation satisfies the associative law: for any a, b, c ∈ Z m , there is (a+b)+c = a+(b+c)
  4. 0 is the additive unit element: for any a belonging to Z m , there is a+0=0+a=a
  5. Any element has an additive inverse element: the inverse element of a is ma, because a+(ma)=(ma)+a=0
  6. Closed to multiplication: for any a, b belongs to Z m , there is ab∈Z m
  7. The multiplication operation satisfies the commutative law: for any a, b belongs to Z m , there is ab=ba
  8. The multiplication operation satisfies the associative law: for any a, b, c ∈ Z m , there is (ab)c=a(bc)
  9. 1 is the multiplicative unit element: for any a belonging to Z m , there is a×1=1×a=a
  10. There is a distributive law between multiplication and addition: for any a, b, c∈Z m , there is (a+b)c=(ac)+(bc), a(b+c)=(ab)+(ac )

Shift password

Let P=C=K=Z 126 , for 0<=K<=25, any x,y∈Z 26 , define e k =(x+K)mod 26 and d k (y)=(yK)mod 26

Build relationships

A B C D E F G H I J K L M N O P Q R S T U V W X AND WITH
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Give a chestnut

Suppose the secret key of the shift password is K=11, and the plain text is wewillmeetatmidnight

From the above we can see that e k = (x+11) mod26

First, the letters in the plaintext correspond to their corresponding integers, and the following number string is obtained: 22 4 22 8 11 11 12 4 4 19 0 19 12 8 3 13 8 6 7 19,

original w e w i l l m e e t a t m i d n i g h t
Corresponding alphabet 22 4 22 8 11 11 12 4 4 19 0 19 12 8 3 13 8 6 7 19
(x+11)mod26 7 15 7 19 22 22 23 15 15 4 11 4 23 19 14 24 19 17 18 4

Refer to the alphabet again to get the ciphertext string as HPHTWWXPPELEXTOYTRSE

The same is true for decryption, just use d k =(y-11)mod26, which will not be described in detail here.

defect

The shift password is insecure and can be deciphered using an exhaustive search method of the secret key.

Code

package com.slp.cryptography;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName ShiftCipher
 * @Description 移位密码
 * @Author sanglp
 * @Date 2020/11/30 9:26
 * @Version 1.0
 **/
public class ShiftCipher {
    
    
    static int moduleNum = 26;//模数
    /**
     * 移位密码算法
     * @param resource 加密明文
     * @param k k
     */
    public static void enShiftCipher(String resource,int k){
    
    
    //由参数可知加密函数为 (x+K)mod 26
        StringBuilder result = new StringBuilder();
        char [] reschar = resource.toCharArray();
        for(int i=0;i<reschar.length;i++){
    
    
            char temp =reschar[i];
            if(reschar[i] >='a'&&reschar[i]<='z'){
    
    //将输入统一转换为大写数字
                temp = (char) (temp-32);
            }
            result.append((char)((temp-'A'+k)%26+'A'));//这样可以不用再维护字母和数字的关系
        }
        System.out.println(result);
    }

    /**
     * 解密算法
     * @param resource
     * @param k
     */
    public static void deShiftCipher(String resource,int k){
    
    
        //由参数可知解密函数为 (y-K)mod 26
        StringBuilder result = new StringBuilder();
        char [] reschar = resource.toCharArray();
        for(int i=0;i<reschar.length;i++){
    
    
            char temp =reschar[i];
            if(reschar[i] >='a'&&reschar[i]<='z'){
    
    //将输入统一转换为大写数字
                temp = (char) (temp-32);
            }
            result.append((char)((temp-'A'-k)%26<0?(temp-'A'-k+26)%26+'A':(temp-'A'-k)%26+'A'));//这里需要考虑如果余数为负的情况
        }
        System.out.println(result);
    }
    public static void main(String[] args) {
    
    
        enShiftCipher("wewillmeetatmidnight",11) ;
        deShiftCipher("HPHTWWXPPELEXTOYTRSE",11);
        forceDecrypt("HPHTWWXPPELEXTOYTRSE");
    }

    /**
     * 暴力破解
     * @param resource
     */
    public static void forceDecrypt(String resource){
    
    
        for(int i=0;i<26;i++){
    
    
            deShiftCipher(resource,i);
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_51656605/article/details/110377659