AES加解密字符串和DES加解密文件例子

AES加解密字符串示例

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
 * Created by pengfei.wang on 2021/1/14
 * Describe: 字符串加解密工具类
 */
public class AESUtil {

    // 密匙
    private static final String KEY = "111111111";
    // 偏移量
    private static final String OFFSET = "11111";
    // 编码
    private static final String ENCODING = "UTF-8";
    //算法
    private static final String ALGORITHM = "AES";
    // 默认的加密算法
    private static final String CIPHER_ALGORITHM = "AES/CBC/ZeroBytePadding";


    /**
     * 加密
     *
     * @param data
     * @return String
     * @throws Exception
     * @author tyg
     * @date 2018年6月28日下午2:50:35
     */
    public static String encrypt(String data) throws Exception {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("ASCII"), ALGORITHM);
        IvParameterSpec iv = new IvParameterSpec(OFFSET.getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(data.getBytes(ENCODING));
        return new String(encrypted, ENCODING).trim();
    }

    /**
     * 解密
     *
     * @param data
     * @return String
     * @throws Exception
     * @author tyg
     * @date 2018年6月28日下午2:50:43
     */
    public static String decrypt(String data) throws Exception {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("ASCII"), ALGORITHM);
        IvParameterSpec iv = new IvParameterSpec(OFFSET.getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] buffer = ParseSystemUtil.parseHexStr2Byte(data);//16进制字符串变成2进制数组
        byte[] encrypted = cipher.doFinal(buffer);
        return new String(encrypted,ENCODING).trim();
    }
}

DES加解密文件示例

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

 /**
 * Created by pengfei.wang on 2021/1/14
 * Describe: 解密文件工具类
 */
public class ObedienceEncryTools {

    private static Cipher cipher;

    /**
     * DES加密算法
     *
     * @param secretKey 密钥
     * @param mode      模式: 加密,解密
     * @param data      需要加密的内容
     * @return 将内容加密后的结果也是byte[]格式的
     */
    public static byte[] des(String secretKey, int mode, byte[] data) {
        byte[] keyByte = secretKey.getBytes();
        byte[] ret = null;
        //加密的内容存在并且密钥存在且长度为8个字节
        if (data != null
                && data.length > 0
                && keyByte != null
                && keyByte.length == 8) {
            try {
                if (cipher == null) {
                    cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");//解密算法
                }
                DESKeySpec keySpec = new DESKeySpec(keyByte);
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");//解密方式
                SecretKey key = keyFactory.generateSecret(keySpec);
                //根据加密方式这里添加偏移量
                /* IvParameterSpec paramSpec = new IvParameterSpec("偏移量".getBytes());
                cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);*/
                cipher.init(mode, key);
                ret = cipher.doFinal(data);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (InvalidKeySpecException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    //DES 加密
    public static byte[] desEncrypt(String key, byte[] data) {
        return des(key, Cipher.ENCRYPT_MODE, data);
    }

    //DES 解密
    public static byte[] desDecrypt(String key, byte[] data) {
        return des(key, Cipher.DECRYPT_MODE, data);
    }
}

public static String TestDecode(String secretKey,String filePath,String targetPath){
        FileInputStream inputStream = null;
        try {
            File keyValueFile = new File(filePath);
            File targetFile = new File(targetPath);
         
            RandomAccessFile keyValueAccess = new RandomAccessFile(keyValueFile, "r");
            byte[] keyValueBuffer = new byte[(int) keyValueFile.length()];
            keyValueAccess.read(keyValueBuffer);
            byte[] decryBuffer =ObedienceEncryTools.desDecrypt(secretKey, keyValueBuffer);
            keyValueAccess.close();
            RandomAccessFile resultFile = new RandomAccessFile(targetFile, "rw");
            resultFile.write(decryBuffer, 0, decryBuffer.length);
            resultFile.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return targetPath;
    }
上一篇:Ant 简易教程


下一篇:Security and Cryptography in Python - Substitution Cipher