Common encryption method-symmetric encryption

Symmetric encryption

  • Using the single-key cryptosystem encryption method, the same key can be used for both encryption and decryption of information at the same time. This encryption method is called symmetric encryption, also known as single-key encryption .
  • Example
    • We now have an original 3 to send to B
    • Set the key to 108, 3 * 108 = 324, send 324 as ciphertext to B
    • After B gets the ciphertext 324, use 324/108 = 3 to get the original text
  • Common encryption algorithms
    • DES: Data Encryption Standard , which is a data encryption standard, is a block algorithm that uses key encryption. In 1977, it was determined as the Federal Data Processing Standard (FIPS) by the National Bureau of Standards of the US Federal Government and authorized to be used in non-secret government communications Use, and then the algorithm is widely circulated internationally.
    • AES: Advanced Encryption Standard, Advanced Encryption Standard . Also known as Rijndael encryption in cryptography, it is a block encryption standard adopted by the US Federal Government. This standard is used to replace the original DES, which has been analyzed by many parties and is widely used all over the world.
  • Features
    • Fast encryption speed, can encrypt large files
    • The ciphertext is reversible, once the key file is leaked, it will lead to data exposure
    • Corresponding characters cannot be found in the encoding table after encryption, and garbled characters appear
    • Generally used in combination with Base64


DES encryption

Sample code des encryption algorithm

Cipher: Documentation https://docs.oracle.com/javase/8/docs/api/javax/crypto/Cipher.html#getInstance-java.lang.String-

DesAesDemo.java

package com.dym.aesanddes;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class DesAesDemo {
    public static void main(String[] args) throws Exception{
        // 原文
        String input = "止兮";
        // des加密必须是8位
        String key = "123456";
        // 算法
        String algorithm = "DES";

        String transformation = "DES";
        // Cipher:密码,获取加密对象
        // transformation:参数表示使用什么类型加密
        Cipher cipher = Cipher.getInstance(transformation);
        // 指定秘钥规则
        // 第一个参数表示:密钥,key的字节数组
        // 第二个参数表示:算法
        SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
        // 对加密进行初始化
        // 第一个参数:表示模式,有加密模式和解密模式
        // 第二个参数:表示秘钥规则
        cipher.init(Cipher.ENCRYPT_MODE,sks);
        // 进行加密
        byte[] bytes = cipher.doFinal(input.getBytes());
        // 打印字节,因为ascii码有负数,解析不出来,所以乱码
//        for (byte b : bytes) {
//            System.out.println(b);
//        }
        // 打印密文
        System.out.println(new String(bytes));
    }
}



Modify the key key = "12345678", run again, the garbled code appears because the corresponding byte has a negative number, but the negative number does not appear in the ascii code table, so the garbled code appears and needs to be transcoded with base64



Use  base64 to encode

base64 When importing the apache package , you need to pay attention, don’t import the package wrong, you need to import the  package



DesDemo.java

package com.dym.aesanddes;

import com.sun.org.apache.xml.internal.security.utils.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

class DesDemo {
    // DES加密算法,key的大小必须是8个字节

    public static void main(String[] args) throws Exception {
        String input ="止兮666";
        // DES加密算法,key的大小必须是8个字节
        String key = "12345678";

        String transformation = "DES";
        // 指定获取密钥的算法
        String algorithm = "DES";
        String encryptDES = encryptDES(input, key, transformation, algorithm);
        System.out.println("加密:" + encryptDES);
        String s = decryptDES(encryptDES, key, transformation, algorithm);
        System.out.println("解密:" + s);

    }

    /**
     * 使用DES加密数据
     *
     * @param input          : 原文
     * @param key            : 密钥(DES,密钥的长度必须是8个字节)
     * @param transformation : 获取Cipher对象的算法
     * @param algorithm      : 获取密钥的算法
     * @return : 密文
     * @throws Exception
     */
    private static String encryptDES(String input, String key, String transformation, String algorithm) throws Exception {
        // 获取加密对象
        Cipher cipher = Cipher.getInstance(transformation);
        // 创建加密规则
        // 第一个参数key的字节
        // 第二个参数表示加密算法
        SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
        // ENCRYPT_MODE:加密模式
        // DECRYPT_MODE: 解密模式
        // 初始化加密模式和算法
        cipher.init(Cipher.ENCRYPT_MODE,sks);
        // 加密
        byte[] bytes = cipher.doFinal(input.getBytes());

        // 输出加密后的数据
        String encode = Base64.encode(bytes);

        return encode;
    }

    /**
     * 使用DES解密
     *
     * @param input          : 密文
     * @param key            : 密钥
     * @param transformation : 获取Cipher对象的算法
     * @param algorithm      : 获取密钥的算法
     * @throws Exception
     * @return: 原文
     */
    private static String decryptDES(String input, String key, String transformation, String algorithm) throws Exception {
        // 1,获取Cipher对象
        Cipher cipher = Cipher.getInstance(transformation);
        // 指定密钥规则
        SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
        cipher.init(Cipher.DECRYPT_MODE, sks);
        // 3. 解密,上面使用的base64编码,下面直接用密文
        byte[] bytes = cipher.doFinal(Base64.decode(input));
        //  因为是明文,所以直接返回
        return new String(bytes);
    }
}

Guess you like

Origin blog.csdn.net/qq_39368007/article/details/114527347