RSA加密解密实例(Java)

一、应用情景

在这里插入图片描述
总结:公钥加密(对方公钥)、私钥解密(自己私钥);私钥签名、公钥验签。

代码实例

// AES.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
 * AES加解密工具
 *
 * @author Created by zqc on 2018/11/02.
 */
public class AES {
   

    private final static Logger log = LoggerFactory.getLogger(AES.class);

    // 加密。sSrc等待加密的数据;sKey用来加密数据的明文密码
    public static byte[] Encrypt(String sSrc, String sKey) throws Exception {
   
        if (sKey == null) {
   
            log.error("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 16) {
   
            log.error("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);//先把明文密码用AES算法加密一下成为AES密码
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));//再用加密后的明文密码(AES密码)来加密数据

        return encrypted;
    }

    // 解密
    public static byte[] Decrypt(byte[] sSrc, String sKey) throws Exception {
   
        try {
   
            // 判断Key是否正确
            if (sKey == null) {
   
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
   
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = sSrc;//先用base64解密
            try {
   
                byte[] original = cipher.doFinal(encrypted1);
                return original;
            } catch (Exception e) {
   
                log.error(e.toString());
                return null;
            }
        } catch (Exception ex) {
   
            log.error(ex.toString());
            return null;
        }
    }
}
//BaseUtils.java
import java.io.*;
import java.util.Base64;
/**
 * BASE64编码解码工具包
 *
 * @author Created by zqc on 2018/11/02.
 */
public class BaseUtils {
   

    /**
     * 文件读取缓冲区大小
     */
    private static final int CACHE_SIZE = 1024;

    /**
     * BASE64字符串解码为二进制数据
     *
     * @param base64
     * @return
     * @throws Exception
     */
    public static byte[] decode(String base64) throws Exception {
   
         return Base64.getDecoder().decode(base64);
//        return Base64.decode(base64.getBytes());
    }

    /**
     * 二进制数据编码为BASE64字符串
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public static String encode(byte[] bytes) throws Exception {
   
//        return new String(Base64.encode(bytes));
        return Base64.getEncoder().encodeToString(bytes);
    }

    /**
     * 将文件编码为BASE64字符串  大文件慎用,可能会导致内存溢出
     *
     * @param filePath 文件绝对路径
     * @return
     * @throws Exception
     */
    public static String encodeFile(String filePath) throws Exception {
   
        byte[] bytes = fileToByte(filePath);
        return encode(bytes);
    }

    /**
     * <p>
     * BASE64字符串转回文件
     *
     * @param filePath 文件绝对路径
     * @param base64   编码字符串
     * @throws Exception
     */
    public static void decodeToFile
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值