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

 更新时间:2014年05月04日 09:37:27   作者:  
这篇文章主要介绍了java加密算法,包括rsa解密、对称加密、md5加密等,需要的朋友可以参考下

复制代码 代码如下:

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基础之反射详解

    Java基础之反射详解

    这篇文章主要介绍了教你怎么通过IDEA设置堆内存空间,文中有非常详细的代码示例,对正在使用IDEA的小伙伴们很有帮助哟,需要的朋友可以参考下
    2021-05-05
  • SpringBoot对接twilio实现邮件信息发送功能

    SpringBoot对接twilio实现邮件信息发送功能

    这篇文章主要为大家详细介绍了SpringBoot如何对接twilio实现邮件信息发送功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-03-03
  • Spring事务执行流程及如何创建事务

    Spring事务执行流程及如何创建事务

    这篇文章主要介绍了Spring事务执行流程及如何创建事务,帮助大家更好的理解和学习使用spring框架,感兴趣的朋友可以了解下
    2021-03-03
  • JAVA JNI函数的注册过程详细介绍

    JAVA JNI函数的注册过程详细介绍

    这篇文章主要介绍了JAVA JNI函数的注册过程详细介绍的相关资料,需要的朋友可以参考下
    2016-11-11
  • Java 断言 assert的用法详解

    Java 断言 assert的用法详解

    Java assert断言机制是Java5中推出的新特性,它主要用于在程序运行时检查状态或假设的正确性,本篇文章将全面详细地讲解Java assert断言机制,包括断言概述、语法规则、工作原理、使用场景、注意事项以及示例代码等方面,需要的朋友可以参考下
    2023-05-05
  • Java中的MarkerFilter的应用场景及使用示例详解

    Java中的MarkerFilter的应用场景及使用示例详解

    这篇文章主要介绍了Java中的MarkerFilter的应用场景及使用示例详解,使用log4j2,负责从消息队列收集日志的,现在系统收集到的日志能和这个系统本身的日志分开,需要的朋友可以参考下
    2024-01-01
  • Java中Optional.of()方法及源码解析(非常详细!)

    Java中Optional.of()方法及源码解析(非常详细!)

    这篇文章主要给大家介绍了关于Java中Optional.of()方法及源码解析的相关资料,Java中java.util .Optional类的of()方法用于获得该Optional类中具有指定类型的指定值的一个实例,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-06-06
  • 在SpringBoot中定义和读取自定义配置的方法步骤

    在SpringBoot中定义和读取自定义配置的方法步骤

    在Spring Boot中定义和读取自定义配置是日常开发中常见的需求,它允许我们以灵活的方式管理应用的配置信息,无论是通过外部配置文件还是通过环境变量,本文是一个详细的步骤说明,包括示例代码,需要的朋友可以参考下
    2024-10-10
  • Struts 2中实现Ajax的三种方式

    Struts 2中实现Ajax的三种方式

    这篇文章主要介绍了Struts 2中实现Ajax的三种方式,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • Java数据结构及算法实例:冒泡排序 Bubble Sort

    Java数据结构及算法实例:冒泡排序 Bubble Sort

    这篇文章主要介绍了Java数据结构及算法实例:冒泡排序 Bubble Sort,本文直接给出实现代码,代码中包含详细注释,需要的朋友可以参考下
    2015-06-06

最新评论