java加密算法分享(rsa解密、对称加密、md5加密)

时间:2021-08-30 16:22:08 


import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import com.sun.mail.util.BASE64DecoderStream;
import com.sun.mail.util.BASE64EncoderStream;


public class util {
    /**
     * 传入名文和公钥钥对数据进行RSA解密
     * <br>返回值:String
     * <br>@param src
     * <br>@param pubkey
     * <br>@return
     */
    public static String rsaEncoding(String src,PublicKey pubkey){
        try {
            Cipher cip = Cipher.getInstance("RSA");
            cip.init(cip.ENCRYPT_MODE, pubkey);
            byte[] by = cip.doFinal(src.getBytes());
            return new String(BASE64EncoderStream.encode(by));

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }

    }
    /**
     * 传入RSA密文和私钥对数据进行解密
     * <br>返回值:String
     * <br>@param sec
     * <br>@param privkey
     * <br>@return
     */
    public static String rsaDeEncoding(String sec,PrivateKey privkey){
        try {
            Cipher cip = Cipher.getInstance("RSA");
            cip.init(cip.DECRYPT_MODE, privkey);
            byte[] by = BASE64DecoderStream.decode(sec.getBytes());
            return new String(cip.doFinal(by));

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 传入字符串、密钥,并加密字符串(对称加密加密),支持:DES、AES、DESede(3DES)
     * <br>返回值:String 密文
     * <br>@param src
     * <br>@param key
     * <br>@param method(DES、AES、DESede)
     * <br>@return
     */
    //对称加密加密
    public static String doubKeyEncoding(String src,String keysrc,String method) {
        SecretKey key;
        try {
            //生成密钥
            KeyGenerator kg =  KeyGenerator.getInstance(method);
            //初始化此密钥生成器。
            kg.init(new SecureRandom(keysrc.getBytes("utf-8")));
            key = kg.generateKey();

            //加密
            Cipher ciph =  Cipher.getInstance(method);
            ciph.init(Cipher.ENCRYPT_MODE, key);
            ciph.update(src.getBytes("utf-8"));
            //使用64进行编码,一避免出现丢数据情景
            byte[] by = BASE64EncoderStream.encode(ciph.doFinal());
            return new String(by);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 传入字符串、密钥、加密方式,并解密字符串(对称加密解密密),支持:DES、AES、DESede(3DES)
     * <br>生成时间:2014年5月2日  下午1:12:13
     * <br>返回值:String 密钥原文
     * <br>@param sec
     * <br>@param key
     * <br>@param method(DES、AES、DESede)
     * <br>@return
     */
    public static String doubKeyDencoding(String sec,String keysrc,String method) {
        SecretKey key;
        try {
            //生成密钥
            KeyGenerator kg =  KeyGenerator.getInstance(method);
            //初始化此密钥生成器。
            kg.init(new SecureRandom(keysrc.getBytes("utf-8")));
            key = kg.generateKey();
            //加密
            Cipher ciph =  Cipher.getInstance(method);
            ciph.init(ciph.DECRYPT_MODE, key);
            //使用64进行解码,一避免出现丢数据情景
            byte[] by = BASE64DecoderStream.decode(sec.getBytes());
            ciph.update(by);
            return new String(ciph.doFinal());

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 单向信息加密(信息摘要),支持:md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512,
     * <br>返回值:String         加密后的密文
     * <br>@param src     传入加密字符串(明文)
     * <br>@param method  指定算法(md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512)
     * <br>@return
     */
    public static String ecodingPasswd(String src,String method){

        try {
            //信息摘要算法
            MessageDigest md5 = MessageDigest.getInstance(method);
            md5.update(src.getBytes());
            byte[] encoding = md5.digest();
            //使用64进行编码,一避免出现丢数据情景
            return new String(BASE64EncoderStream.encode(encoding));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e+"加密失败!!");
        }

    }
}

标签:java,加密算法,rsa,md5
0
投稿

猜你喜欢

  • 关于@Autowired注入依赖失败的问题及解决

    2023-11-29 00:50:42
  • 关于@GetMapping和@GetMapping(value=““)的区别

    2023-11-27 11:07:37
  • 如何利用java控制鼠标操作一些重复的事情

    2021-11-12 12:54:02
  • 在IDEA中maven配置MyBatis的流程详解

    2021-08-26 17:02:27
  • 关于MVC与SpringMVC的介绍、区别、执行流程

    2023-11-28 02:25:56
  • windows下java -jar 后台运行以及杀死后台进程的操作

    2022-02-09 17:28:13
  • Java面试题冲刺第二十三天--分布式

    2023-09-24 07:30:43
  • Mybatis SqlSessionFactory与SqlSession详细讲解

    2021-12-24 22:42:56
  • 解决Mybatis-Plus操作分页后数据失效问题

    2023-11-25 11:31:55
  • 简单实现安卓里百度地图持续定位

    2023-07-29 07:59:22
  • Java中避免过多if-else的几种方法

    2023-11-28 13:07:09
  • Java对象在JVM中的生命周期详解

    2023-11-24 16:15:03
  • Spring Boot使用profile如何配置不同环境的配置文件

    2023-11-25 12:52:36
  • C++右值引用与move和forward函数的使用详解

    2023-07-05 19:27:33
  • 浅谈mybatis中SQL语句给boolean类型赋值问题

    2023-01-19 15:15:42
  • java中使用interrupt通知线程停止详析

    2023-09-03 11:41:26
  • Android客户端与服务端数据加密传输方案详解

    2023-07-14 13:55:37
  • 关于Java8 parallelStream并发安全的深入讲解

    2023-11-15 06:37:35
  • 简单了解Java多态向上转型相关原理

    2023-10-11 16:11:01
  • Java中的接口回调实例

    2023-11-29 08:05:43
  • asp之家 软件编程 m.aspxhome.com