前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JAVA中的加密算法之双向加密(一)

JAVA中的加密算法之双向加密(一)

作者头像
幽鸿
发布2020-04-01 21:00:15
3.8K0
发布2020-04-01 21:00:15
举报
文章被收录于专栏:大数据-数据人生

JAVA中的加密算法之双向加密(一)

作者:幽鸿 

       加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容。大体上分为双向加密单向加密,而双向加密又分为对称加密非对称加密(有些资料将加密直接分为对称加密和非对称加密)。           双向加密大体意思就是明文加密后形成密文,可以通过算法还原成明文。而单向加密只是对信息进行了摘要计算,不能通过算法生成明文,单向加密从严格意思上说不能算是加密的一种,应该算是摘要算法吧。具体区分可以参考: http://security.group.iteye.com/group/wiki/1710-one-way-encryption-algorithm 一、双向加密 (一)、对称加密 采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密。 需要对加密和解密使用相同密钥的加密算法。由于其速度,对称性加密通常在消息发送方需要加密大量数据时使用。对称性加密也称为密钥加密。 所谓对称,就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密。密钥是控制加密及解密过程的指令。 算法是一组规则,规定如何进行加密和解密。因此对称式加密本身不是安全的。    常用的对称加密有:DES、IDEA、RC2、RC4、SKIPJACK、RC5、AES算法等 对称加密一般java类中中定义成员

Java代码

代码语言:javascript
复制
    //KeyGenerator 提供对称密钥生成器的功能,支持各种算法   
    private KeyGenerator keygen;   
    //SecretKey 负责保存对称密钥   
    private SecretKey deskey;   
    //Cipher负责完成加密或解密工作   
    private Cipher c;   
    //该字节数组负责保存加密的结果   
    private byte[] cipherByte;  

//KeyGenerator 提供对称密钥生成器的功能,支持各种算法
private KeyGenerator keygen;
//SecretKey 负责保存对称密钥
private SecretKey deskey;
//Cipher负责完成加密或解密工作
private Cipher c;
//该字节数组负责保存加密的结果
private byte[] cipherByte;


在构造函数中初始化

Java代码

    Security.addProvider(new com.sun.crypto.provider.SunJCE());   
    //实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)   
    keygen = KeyGenerator.getInstance("DES");//   
    //生成密钥   
    deskey = keygen.generateKey();   
    //生成Cipher对象,指定其支持的DES算法   
    c = Cipher.getInstance("DES");  

    //实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
    keygen = KeyGenerator.getInstance("DES");//
    //生成密钥
    deskey = keygen.generateKey();
    //生成Cipher对象,指定其支持的DES算法
    c = Cipher.getInstance("DES");
代码语言:javascript
复制
1. DES算法为密码体制中的对称密码体制,又被成为美国数据加密标准,是1972年美国IBM公司研制的对称密码体制加密算法。 明文按64位进行
分组, 密钥长64位,密钥事实上是56位参与DES运算(第8、16、24、32、40、48、56、64位是校验位, 使得每个密钥都有奇数个1)分组后
的明文组和56位的密钥按位替代或交换的方法形成密文组的加密方法。

Java代码

代码语言:javascript
复制
    import java.security.InvalidKeyException;   
    import java.security.NoSuchAlgorithmException;   
    import java.security.Security;   
      
    import javax.crypto.BadPaddingException;   
    import javax.crypto.Cipher;   
    import javax.crypto.IllegalBlockSizeException;   
    import javax.crypto.KeyGenerator;   
    import javax.crypto.NoSuchPaddingException;   
    import javax.crypto.SecretKey;   
      
    public class EncrypDES {   
           
        //KeyGenerator 提供对称密钥生成器的功能,支持各种算法   
        private KeyGenerator keygen;   
        //SecretKey 负责保存对称密钥   
        private SecretKey deskey;   
        //Cipher负责完成加密或解密工作   
        private Cipher c;   
        //该字节数组负责保存加密的结果   
        private byte[] cipherByte;   
           
        public EncrypDES() throws NoSuchAlgorithmException, NoSuchPaddingException{   
            Security.addProvider(new com.sun.crypto.provider.SunJCE());   
            //实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)   
            keygen = KeyGenerator.getInstance("DES");   
            //生成密钥   
            deskey = keygen.generateKey();   
            //生成Cipher对象,指定其支持的DES算法   
            c = Cipher.getInstance("DES");   
        }   
           
        /**  
         * 对字符串加密  
         *   
         * @param str  
         * @return  
         * @throws InvalidKeyException  
         * @throws IllegalBlockSizeException  
         * @throws BadPaddingException  
         */  
        public byte[] Encrytor(String str) throws InvalidKeyException,   
                IllegalBlockSizeException, BadPaddingException {   
            // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式   
            c.init(Cipher.ENCRYPT_MODE, deskey);   
            byte[] src = str.getBytes();   
            // 加密,结果保存进cipherByte   
            cipherByte = c.doFinal(src);   
            return cipherByte;   
        }   
      
        /**  
         * 对字符串解密  
         *   
         * @param buff  
         * @return  
         * @throws InvalidKeyException  
         * @throws IllegalBlockSizeException  
         * @throws BadPaddingException  
         */  
        public byte[] Decryptor(byte[] buff) throws InvalidKeyException,   
                IllegalBlockSizeException, BadPaddingException {   
            // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式   
            c.init(Cipher.DECRYPT_MODE, deskey);   
            cipherByte = c.doFinal(buff);   
            return cipherByte;   
        }   
      
        /**  
         * @param args  
         * @throws NoSuchPaddingException   
         * @throws NoSuchAlgorithmException   
         * @throws BadPaddingException   
         * @throws IllegalBlockSizeException   
         * @throws InvalidKeyException   
         */  
        public static void main(String[] args) throws Exception {   
            EncrypDES de1 = new EncrypDES();   
            String msg ="郭XX-搞笑相声全集";   
            byte[] encontent = de1.Encrytor(msg);   
            byte[] decontent = de1.Decryptor(encontent);   
            System.out.println("明文是:" + msg);   
            System.out.println("加密后:" + new String(encontent));   
            System.out.println("解密后:" + new String(decontent));   
        }   
      
    }  

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class EncrypDES {
    //KeyGenerator 提供对称密钥生成器的功能,支持各种算法
    private KeyGenerator keygen;
    //SecretKey 负责保存对称密钥
    private SecretKey deskey;
    //Cipher负责完成加密或解密工作
    private Cipher c;
    //该字节数组负责保存加密的结果
    private byte[] cipherByte;
    public EncrypDES() throws NoSuchAlgorithmException, NoSuchPaddingException{
       Security.addProvider(new com.sun.crypto.provider.SunJCE());
             //实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
       keygen = KeyGenerator.getInstance("DES");     //生成密钥
       deskey = keygen.generateKey();              //生成Cipher对象,指定其支持的DES算法
       c = Cipher.getInstance("DES");
     }

      /** * 对字符串加密 *  * 
           @param str * 
           @return * 
           @throws InvalidKeyException *
           @throws IllegalBlockSizeException * 
           @throws BadPaddingException
       */
       public byte[] Encrytor(String str) throws InvalidKeyException,
           IllegalBlockSizeException,   BadPaddingException {
        // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
        c.init(Cipher.ENCRYPT_MODE, deskey);
        byte[] src = str.getBytes();// 加密,结果保存进          
        cipherBytecipherByte = c.doFinal(src);return cipherByte;
   }

    /** * 对字符串解密 *  * 
        @param buff * 
        @return * @throws InvalidKeyException * 
        @throws IllegalBlockSizeException * 
        @throws BadPaddingException */
    public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
         IllegalBlockSizeException, BadPaddingException {
        // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
       c.init(Cipher.DECRYPT_MODE, deskey);cipherByte = c.doFinal(buff);
       return cipherByte;
    }

    /** * @param args * 
          @throws NoSuchPaddingException  *
          @throws NoSuchAlgorithmException  * 
          @throws BadPaddingException  * 
          @throws IllegalBlockSizeException  * 
          @throws InvalidKeyException  */
    public static void main(String[] args) throws Exception {
          EncrypDES de1 = new EncrypDES();
          String msg ="郭XX-搞笑相声全集";
          byte[] encontent = de1.Encrytor(msg);
          byte[] decontent = de1.Decryptor(encontent);
          System.out.println("明文是:" + msg);
          System.out.println("加密后:" + new String(encontent));
          System.out.println("解密后:" + new String(decontent));
    }}

2. 3DES又称Triple DES,是DES加密算法的一种模式,它使用3条56位的密钥对3DES 数据进行三次加密。数据加密标准(DES)是美国的一种由来已久的加密标准,它使用对称密钥加密法,并于1981年被ANSI组织规范为ANSI X.3.92。DES使用56位密钥和密码块的方法,而在密码块的方法中,文本被分成64位大小的文本块然后再进行加密。比起最初的DES,3DES更为安全。    3DES(即Triple DES)是DES向AES过渡的加密算法(1999年,NIST将3-DES指定为过渡的加密标准),是DES的一个更安全的变形。它以DES为基本模块,通过组合分组方法设计出分组加密算法,其具体实现如下: 设Ek()和Dk()代表DES算法的加密和解密过程,K代表DES算法使用的密钥,P代表明文,C代表密文, 这样,    3DES加密过程为:C=Ek3(Dk2(Ek1(P))) 3DES解密过程为:P=Dk1((EK2(Dk3(C)))

Java代码

代码语言:javascript
复制
    import java.security.InvalidKeyException;   
    import java.security.NoSuchAlgorithmException;   
    import java.security.Security;   
      
    import javax.crypto.BadPaddingException;   
    import javax.crypto.Cipher;   
    import javax.crypto.IllegalBlockSizeException;   
    import javax.crypto.KeyGenerator;   
    import javax.crypto.NoSuchPaddingException;   
    import javax.crypto.SecretKey;   
      
    public class EncrypDES3 {   
      
        // KeyGenerator 提供对称密钥生成器的功能,支持各种算法   
        private KeyGenerator keygen;   
        // SecretKey 负责保存对称密钥   
        private SecretKey deskey;   
        // Cipher负责完成加密或解密工作   
        private Cipher c;   
        // 该字节数组负责保存加密的结果   
        private byte[] cipherByte;   
      
        public EncrypDES3() throws NoSuchAlgorithmException, NoSuchPaddingException {   
            Security.addProvider(new com.sun.crypto.provider.SunJCE());   
            // 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)   
            keygen = KeyGenerator.getInstance("DESede");   
            // 生成密钥   
            deskey = keygen.generateKey();   
            // 生成Cipher对象,指定其支持的DES算法   
            c = Cipher.getInstance("DESede");   
        }   
      
        /**  
         * 对字符串加密  
         *   
         * @param str  
         * @return  
         * @throws InvalidKeyException  
         * @throws IllegalBlockSizeException  
         * @throws BadPaddingException  
         */  
        public byte[] Encrytor(String str) throws InvalidKeyException,   
                IllegalBlockSizeException, BadPaddingException {   
            // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式   
            c.init(Cipher.ENCRYPT_MODE, deskey);   
            byte[] src = str.getBytes();   
            // 加密,结果保存进cipherByte   
            cipherByte = c.doFinal(src);   
            return cipherByte;   
        }   
      
        /**  
         * 对字符串解密  
         *   
         * @param buff  
         * @return  
         * @throws InvalidKeyException  
         * @throws IllegalBlockSizeException  
         * @throws BadPaddingException  
         */  
        public byte[] Decryptor(byte[] buff) throws InvalidKeyException,   
                IllegalBlockSizeException, BadPaddingException {   
            // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式   
            c.init(Cipher.DECRYPT_MODE, deskey);   
            cipherByte = c.doFinal(buff);   
            return cipherByte;   
        }   
      
        /**  
         * @param args  
         * @throws NoSuchPaddingException   
         * @throws NoSuchAlgorithmException   
         * @throws BadPaddingException   
         * @throws IllegalBlockSizeException   
         * @throws InvalidKeyException   
         */  
        public static void main(String[] args) throws Exception {   
            EncrypDES3 des = new EncrypDES3();   
            String msg ="郭XX-搞笑相声全集";   
            byte[] encontent = des.Encrytor(msg);   
            byte[] decontent = des.Decryptor(encontent);   
            System.out.println("明文是:" + msg);   
            System.out.println("加密后:" + new String(encontent));   
            System.out.println("解密后:" + new String(decontent));   
      
        }   
      
    }  

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class EncrypDES3 {
     // KeyGenerator 提供对称密钥生成器的功能,支持各种算法
     private KeyGenerator keygen;// SecretKey 负责保存对称密钥
     private SecretKey deskey;// Cipher负责完成加密或解密工作
     private Cipher c;// 该字节数组负责保存加密的结果
     private byte[] cipherByte;

     public EncrypDES3() throws NoSuchAlgorithmException, NoSuchPaddingException {
            Security.addProvider(new com.sun.crypto.provider.SunJCE());
                  // 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
            keygen = KeyGenerator.getInstance("DESede");// 生成密钥
            deskey = keygen.generateKey();// 生成Cipher对象,指定其支持的DES算法c =       
            Cipher.getInstance("DESede");
      }

      /** * 对字符串加密 * 
          * @param str * @return * 
            @throws InvalidKeyException * @throws IllegalBlockSizeException * 
            @throws BadPaddingException */
      public byte[] Encrytor(String str) throws InvalidKeyException,
            IllegalBlockSizeException, BadPaddingException {
        // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
        c.init(Cipher.ENCRYPT_MODE, deskey);byte[] src = str.getBytes();
        // 加密,结果保存进cipherByte
        cipherByte = c.doFinal(src);
        return cipherByte;
       }

       /** * 对字符串解密 * 
           * @param buff * @return * @throws InvalidKeyException 
           * @throws IllegalBlockSizeException * @throws BadPaddingException */
       public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
           IllegalBlockSizeException, BadPaddingException {
           // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
           c.init(Cipher.DECRYPT_MODE, deskey);
           cipherByte = c.doFinal(buff);
           return cipherByte;
       }

       /** * @param args * @throws NoSuchPaddingException  
           * @throws NoSuchAlgorithmException 
           * @throws BadPaddingException  
           * @throws IllegalBlockSizeException  
           * @throws InvalidKeyException  */
       public static void main(String[] args) throws Exception {
           EncrypDES3 des = new EncrypDES3();
           String msg ="郭XX-搞笑相声全集";
           byte[] encontent = des.Encrytor(msg);
           byte[] decontent = des.Decryptor(encontent);
           System.out.println("明文是:" + msg);
           System.out.println("加密后:" + new String(encontent));
           System.out.println("解密后:" + new String(decontent));
       }
   }

3. AES密码学中的高级加密标准(Advanced Encryption Standard,AES),又称高级加密标准Rijndael加密法,是美国联邦政府采 用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美 国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标 准已然成为对称密钥加密中最流行的算法之一。   该算法为比利时密码学家Joan Daemen和Vincent Rijmen所设计,结合两位作者的 名字,以Rijndael之命名之,投稿高级加密标准的甄选流程。(Rijdael的发音近于 "Rhinedoll"。)

Java代码

代码语言:javascript
复制
    import java.security.InvalidKeyException;   
    import java.security.NoSuchAlgorithmException;   
    import java.security.Security;   
      
    import javax.crypto.BadPaddingException;   
    import javax.crypto.Cipher;   
    import javax.crypto.IllegalBlockSizeException;   
    import javax.crypto.KeyGenerator;   
    import javax.crypto.NoSuchPaddingException;   
    import javax.crypto.SecretKey;   
      
    public class EncrypAES {   
           
        //KeyGenerator 提供对称密钥生成器的功能,支持各种算法   
        private KeyGenerator keygen;   
        //SecretKey 负责保存对称密钥   
        private SecretKey deskey;   
        //Cipher负责完成加密或解密工作   
        private Cipher c;   
        //该字节数组负责保存加密的结果   
        private byte[] cipherByte;   
           
        public EncrypAES() throws NoSuchAlgorithmException, NoSuchPaddingException{   
            Security.addProvider(new com.sun.crypto.provider.SunJCE());   
            //实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)   
            keygen = KeyGenerator.getInstance("AES");   
            //生成密钥   
            deskey = keygen.generateKey();   
            //生成Cipher对象,指定其支持的DES算法   
            c = Cipher.getInstance("AES");   
        }   
           
        /**  
         * 对字符串加密  
         *   
         * @param str  
         * @return  
         * @throws InvalidKeyException  
         * @throws IllegalBlockSizeException  
         * @throws BadPaddingException  
         */  
        public byte[] Encrytor(String str) throws InvalidKeyException,   
                IllegalBlockSizeException, BadPaddingException {   
            // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式   
            c.init(Cipher.ENCRYPT_MODE, deskey);   
            byte[] src = str.getBytes();   
            // 加密,结果保存进cipherByte   
            cipherByte = c.doFinal(src);   
            return cipherByte;   
        }   
      
        /**  
         * 对字符串解密  
         *   
         * @param buff  
         * @return  
         * @throws InvalidKeyException  
         * @throws IllegalBlockSizeException  
         * @throws BadPaddingException  
         */  
        public byte[] Decryptor(byte[] buff) throws InvalidKeyException,   
                IllegalBlockSizeException, BadPaddingException {   
            // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式   
            c.init(Cipher.DECRYPT_MODE, deskey);   
            cipherByte = c.doFinal(buff);   
            return cipherByte;   
        }   
      
        /**  
         * @param args  
         * @throws NoSuchPaddingException   
         * @throws NoSuchAlgorithmException   
         * @throws BadPaddingException   
         * @throws IllegalBlockSizeException   
         * @throws InvalidKeyException   
         */  
        public static void main(String[] args) throws Exception {   
            EncrypAES de1 = new EncrypAES();   
            String msg ="郭XX-搞笑相声全集";   
            byte[] encontent = de1.Encrytor(msg);   
            byte[] decontent = de1.Decryptor(encontent);   
            System.out.println("明文是:" + msg);   
            System.out.println("加密后:" + new String(encontent));   
            System.out.println("解密后:" + new String(decontent));   
        }   
      
    }  

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class EncrypAES {
    //KeyGenerator 提供对称密钥生成器的功能,支持各种算法
    private KeyGenerator keygen;
    //SecretKey 负责保存对称密钥
    private SecretKey deskey;
    //Cipher负责完成加密或解密工作
    private Cipher c;
    //该字节数组负责保存加密的结果
    private byte[] cipherByte;
    
    public EncrypAES() throws NoSuchAlgorithmException, NoSuchPaddingException{
         Security.addProvider(new com.sun.crypto.provider.SunJCE());//实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
         keygen = KeyGenerator.getInstance("AES");//生成密钥
         deskey = keygen.generateKey();//生成Cipher对象,指定其支持的DES算法
         c = Cipher.getInstance("AES");
     }

     /** * 对字符串加密 *  * 
         @param str * 
         @return * 
         @throws InvalidKeyException * 
         @throws IllegalBlockSizeException * 
         @throws BadPaddingException */
     public byte[] Encrytor(String str) throws InvalidKeyException,IllegalBlockSizeException, BadPaddingException {
         // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
         c.init(Cipher.ENCRYPT_MODE, deskey);
         byte[] src = str.getBytes();
         // 加密,结果保存进
         cipherBytecipherByte = c.doFinal(src);
         return cipherByte;
      }

      /** * 对字符串解密 *  * 
          @param buff * @return * 
          @throws InvalidKeyException * 
          @throws IllegalBlockSizeException * 
          @throws BadPaddingException */
      public byte[] Decryptor(byte[] buff) throws InvalidKeyException,IllegalBlockSizeException, BadPaddingException {
          // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
          c.init(Cipher.DECRYPT_MODE, deskey);
          cipherByte = c.doFinal(buff);return cipherByte;
       }

      /** * 
          @param args * 
          @throws NoSuchPaddingException  * 
          @throws NoSuchAlgorithmException  * 
          @throws BadPaddingException  * 
          @throws IllegalBlockSizeException  * 
          @throws InvalidKeyException  */
      public static void main(String[] args) throws Exception {
          EncrypAES de1 = new EncrypAES();
          String msg ="郭XX-搞笑相声全集";
          byte[] encontent = de1.Encrytor(msg);
          byte[] decontent = de1.Decryptor(encontent);
          System.out.println("明文是:" + msg);
          System.out.println("加密后:" + new String(encontent));
          System.out.println("解密后:" + new String(decontent));
    }}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • JAVA中的加密算法之双向加密(一)
相关产品与服务
图像处理
图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档