Java常用的加密技术

项目结构:

总体代码:

package VirtualUtils;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Random;
public class CryptoUtils {
    private static final String ENCODE="utf-8";
    private static final String ALGORITHM="AES";
    private static final String PATTERN="AES/ECB/PKCS5PADDING";
    private static final String ALLCHAR="0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
    public static String GenerateKey()
    {
        StringBuffer buffer=new StringBuffer();
        Random random=new Random();
        for (int i = 0; i < 16; i++) {
            buffer.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
        }
        return buffer.toString();
    }
    public static int GenerateIntKey()
    {
        Random random=new Random();
        return random.nextInt(999999999);
    }
    public static String encrypt_msg(String txt,String key) throws Exception {
        SecretKey secretKey=new SecretKeySpec(key.getBytes(ENCODE),ALGORITHM);
        Cipher cipher=Cipher.getInstance(PATTERN);
        cipher.init(Cipher.ENCRYPT_MODE,secretKey);
        byte[] encrypt_data= cipher.doFinal(txt.getBytes(ENCODE));
        return Base64.getEncoder().encodeToString(encrypt_data);
    }
    public static String decrypt_msg(String txt,String key) throws Exception {
        SecretKey secretKey=new SecretKeySpec(key.getBytes(ENCODE),ALGORITHM);
        Cipher cipher=Cipher.getInstance(PATTERN);
        cipher.init(Cipher.DECRYPT_MODE,secretKey);
        byte[] decrypt_data= cipher.doFinal(Base64.getDecoder().decode(txt));
        return new String(decrypt_data, ENCODE);
    }
    public static byte[] Encrypt_MD5(byte[]data) throws NoSuchAlgorithmException {
        MessageDigest digest=MessageDigest.getInstance("MD5");
        digest.update(data);
        return digest.digest();
    }
    public static byte[] Encrypt_SHA(byte[]data) throws NoSuchAlgorithmException {
        MessageDigest digest=MessageDigest.getInstance("SHA");
        digest.update(data);
        return digest.digest();
    }
    public static KeyPairGenerator generator;
    public static KeyPair keyPair;
    public static final class keystore{
        public byte[] public_key;
        public byte[] private_key;
    }
    static {
        try {
            generator=KeyPairGenerator.getInstance("RSA");
            generator.initialize(2048);
            keyPair=generator.generateKeyPair();
        }catch (Exception err)
        {
            err.printStackTrace();
        }
    }
    public static keystore get_key_pair(){
        keystore key_value=new keystore();
        key_value.public_key=keyPair.getPublic().getEncoded();
        key_value.private_key=keyPair.getPrivate().getEncoded();
        return key_value;
    }
    public static byte[] encrypt_data_by_public(byte[] msg, byte[] key) throws Exception {
        KeyFactory keyFactory=KeyFactory.getInstance("RSA");
        X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(key);
        PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
        Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE,publicKey);
        return cipher.doFinal(msg);
    }
    public static byte[] decrypt_data_by_private(byte[] msg, byte[] key) throws Exception{
        KeyFactory keyFactory=KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(key);
        PrivateKey privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE,privateKey);
        return cipher.doFinal(msg);
    }
    public static byte[] encrypt_data_by_private(byte[] msg, byte[] key) throws Exception
    {
        KeyFactory keyFactory=KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(key);
        PrivateKey privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE,privateKey);
        return cipher.doFinal(msg);
    }
    public static byte[] decrypt_data_by_public(byte[] msg,byte[] key) throws Exception
    {
        KeyFactory keyFactory=KeyFactory.getInstance("RSA");
        X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(key);
        PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
        Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE,publicKey);
        return cipher.doFinal(msg);
    }
    public static String Base64ToString(byte[]data)
    {
        return Base64.getEncoder().encodeToString(data);
    }
    public static String BytesToString(byte[]data,String encode) throws UnsupportedEncodingException {
        return new String(data,encode);
    }
}

代码组成:

单向加密:sha散列加密

public static byte[] Encrypt_SHA(byte[]data) throws NoSuchAlgorithmException {
        MessageDigest digest=MessageDigest.getInstance("SHA");
        digest.update(data);
        return digest.digest();
    }

单向加密:md5摘要加密

public static byte[] Encrypt_MD5(byte[]data) throws NoSuchAlgorithmException {
        MessageDigest digest=MessageDigest.getInstance("MD5");
        digest.update(data);
        return digest.digest();
    }

对称加密:AES加密

private static final String ENCODE="utf-8";
    private static final String ALGORITHM="AES";
    private static final String PATTERN="AES/ECB/PKCS5PADDING";
    private static final String ALLCHAR="0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
public static String encrypt_msg(String txt,String key) throws Exception {
        SecretKey secretKey=new SecretKeySpec(key.getBytes(ENCODE),ALGORITHM);
        Cipher cipher=Cipher.getInstance(PATTERN);
        cipher.init(Cipher.ENCRYPT_MODE,secretKey);
        byte[] encrypt_data= cipher.doFinal(txt.getBytes(ENCODE));
        return Base64.getEncoder().encodeToString(encrypt_data);
    }
    public static String decrypt_msg(String txt,String key) throws Exception {
        SecretKey secretKey=new SecretKeySpec(key.getBytes(ENCODE),ALGORITHM);
        Cipher cipher=Cipher.getInstance(PATTERN);
        cipher.init(Cipher.DECRYPT_MODE,secretKey);
        byte[] decrypt_data= cipher.doFinal(Base64.getDecoder().decode(txt));
        return new String(decrypt_data, ENCODE);
    }

非对称加密:RSA加密

public static KeyPairGenerator generator;
    public static KeyPair keyPair;
    public static final class keystore{
        public byte[] public_key;
        public byte[] private_key;
    }
    static {
        try {
            generator=KeyPairGenerator.getInstance("RSA");
            generator.initialize(2048);
            keyPair=generator.generateKeyPair();
        }catch (Exception err)
        {
            err.printStackTrace();
        }
    }
    public static keystore get_key_pair(){
        keystore key_value=new keystore();
        key_value.public_key=keyPair.getPublic().getEncoded();
        key_value.private_key=keyPair.getPrivate().getEncoded();
        return key_value;
    }
    public static byte[] encrypt_data_by_public(byte[] msg, byte[] key) throws Exception {
        KeyFactory keyFactory=KeyFactory.getInstance("RSA");
        X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(key);
        PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
        Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE,publicKey);
        return cipher.doFinal(msg);
    }
    public static byte[] decrypt_data_by_private(byte[] msg, byte[] key) throws Exception{
        KeyFactory keyFactory=KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(key);
        PrivateKey privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE,privateKey);
        return cipher.doFinal(msg);
    }
    public static byte[] encrypt_data_by_private(byte[] msg, byte[] key) throws Exception
    {
        KeyFactory keyFactory=KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(key);
        PrivateKey privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE,privateKey);
        return cipher.doFinal(msg);
    }
    public static byte[] decrypt_data_by_public(byte[] msg,byte[] key) throws Exception
    {
        KeyFactory keyFactory=KeyFactory.getInstance("RSA");
        X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(key);
        PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
        Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE,publicKey);
        return cipher.doFinal(msg);
    }

随机数生成(字符串):

public static String GenerateKey()
    {
        StringBuffer buffer=new StringBuffer();
        Random random=new Random();
        for (int i = 0; i < 16; i++) {
            buffer.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
        }
        return buffer.toString();
    }

随机数生成(整形):

public static int GenerateIntKey()
    {
        Random random=new Random();
        return random.nextInt(999999999);
    }

Base64编码转字符串:

public static String Base64ToString(byte[]data)
    {
        return Base64.getEncoder().encodeToString(data);
    }

字节数组转字符串:

public static String BytesToString(byte[]data,String encode) throws UnsupportedEncodingException {
        return new String(data,encode);
    }

代码测试:

import VirtualUtils.CryptoUtils;
public class Main {
    public static void main(String[]args) throws Exception {
        String msg="This is a test";
        CryptoUtils.keystore keystore=CryptoUtils.get_key_pair();
        byte[] en=CryptoUtils.encrypt_data_by_public(msg.getBytes("utf-8"),keystore.public_key);
        System.out.println(CryptoUtils.Base64ToString(CryptoUtils.decrypt_data_by_private(en,keystore.private_key)));
        System.out.println(new String(CryptoUtils.decrypt_data_by_private(en,keystore.private_key)));
        byte[] en1=CryptoUtils.encrypt_data_by_private(msg.getBytes("utf-8"),keystore.private_key);
        System.out.println(CryptoUtils.Base64ToString(en1));
        System.out.println(CryptoUtils.BytesToString(CryptoUtils.decrypt_data_by_public(en1,keystore.public_key),"utf-8"));
    }
}

测试结果:

  • 16
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java文件压缩加密是一种将Java文件进行压缩和加密保护的技术。通过对文件进行压缩,可以减小文件的体积,方便传输和存储。而通过加密文件,可以保护文件的内容,防止未经授权的人员获取和修改文件。 在Java中,可以使用压缩和加密相关的类和方法来实现文件压缩加密操作。常用的压缩类有java.util.zip.ZipOutputStream和java.util.zip.ZipInputStream,通过这些类可以将文件或文件夹压缩为zip格式的压缩包,或者解压缩已有的压缩包。 对于文件加密,可以使用Java提供的加密算法和密码库来实现。常用的加密算法有对称加密算法和非对称加密算法。对称加密算法如AES和DES可以使用相同的密钥进行加密和解密,而非对称加密算法如RSA则需要一对公钥和私钥进行加密和解密。通过在程序中使用对应的算法和密钥,可以将文件内容加密后保存,只有使用正确的密钥才能解密文件内容。 在实现文件压缩加密时,可以先将文件进行压缩,然后再对压缩后的文件进行加密操作。这样可以同时达到减小文件体积和保护文件内容的目的。在传输或存储文件时,可以先解密再解压缩,得到原始的文件内容。 总之,Java文件压缩加密是一种通过压缩和加密的方式保护文件内容的技术,通过使用Java提供的压缩和加密类、算法和密码库,可以方便地实现文件的压缩和加密操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值