AES DES 3DES DESede Blowfish RSA 及其他 生成密钥

Posted on 2019-08-27 17:24  橙子j  阅读(539)  评论(0编辑  收藏  举报

MD5(不可逆),对称,非对称 区别https://blog.csdn.net/wangpeng322/article/details/84106548

 

AES DES 3DES(DESede) Blowfish

DESede就是3DES  http://www.voidcn.com/article/p-sqomorlk-btw.html

 

把编码规则和长度设置成需要的就行:

输出十六进制

 1 package encryption_test;
 2 
 3 import java.security.NoSuchAlgorithmException;
 4 import java.security.SecureRandom;
 5 
 6 import javax.crypto.KeyGenerator;
 7 import javax.crypto.SecretKey;
 8 
 9 public class aesEn {
10 
11     /**
12       * 随机生成秘钥
13       */
14       public static void getKey() {
15         try {
16           KeyGenerator kg = KeyGenerator.getInstance("AES");
17           kg.init(256);
18           //要生成多少位,只需要修改这里即可128, 192或256
19           SecretKey sk = kg.generateKey();
20           byte[] b = sk.getEncoded();
21           String s = byteToHexString(b);
22           System.out.println(s);
23           System.out.println("十六进制密钥长度为"+s.length());
24           //System.out.println("二进制密钥的长度为"+s.length()*4);
25         }
26         catch (NoSuchAlgorithmException e) {
27           e.printStackTrace();
28           System.out.println("没有此算法。");
29         }
30       }
31 
32       /**
33        * 使用指定的字符串生成秘钥
34        
35        public static void getKeyByPass() {
36          //生成秘钥
37          String password="testkey";
38          try {
39            KeyGenerator kg = KeyGenerator.getInstance("AES");
40            // kg.init(128);//要生成多少位,只需要修改这里即可128, 192或256
41            //SecureRandom是生成安全随机数序列,password.getBytes()是种子,只要种子相同,序列就一样,所以生成的秘钥就一样。
42            kg.init(128, new SecureRandom(password.getBytes()));
43            SecretKey sk = kg.generateKey();
44            byte[] b = sk.getEncoded();
45            String s = byteToHexString(b);
46            System.out.println(s);
47            System.out.println("十六进制密钥长度为"+s.length());
48            System.out.println("二进制密钥的长度为"+s.length()*4);
49          }
50          catch (NoSuchAlgorithmException e) {
51            e.printStackTrace();
52            System.out.println("没有此算法。");
53          }
54        }*/
55 
56        /**
57         * byte数组转化为16进制字符串
58         * @param bytes
59         * @return
60         */
61         public static String byteToHexString(byte[] bytes) {
62           StringBuffer sb = new StringBuffer();
63           for (int i = 0; i < bytes.length; i++) {
64             String strHex=Integer.toHexString(bytes[i]);
65             if(strHex.length() > 3) {
66               sb.append(strHex.substring(6));
67             } else {
68               if(strHex.length() < 2) {
69                 sb.append("0" + strHex);
70               } else {
71                 sb.append(strHex);
72               }
73             }
74           }
75           return sb.toString();
76         }
77 
78          
79      
80     public static void main(String[] args) {
81         getKey();//会生成16进制的代码
82         //getKeyByPass();
83         
84     }
85 
86 }

 

输出Base64

 

package secretKey;

import java.security.NoSuchAlgorithmException;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import sun.misc.BASE64Encoder;

public class desKey {
    
    /**
     * 随机生成秘钥
     */
     public static void getKey() {
       try {
         KeyGenerator kg = KeyGenerator.getInstance("DES");
         kg.init(56);
         //要生成多少位,只需要修改这里即可56
         SecretKey sk = kg.generateKey();
         byte[] b = sk.getEncoded();
         //String s = byteToHexString(b);
         //System.out.println(s);
         
         //定义一个BASE64Encoder
         BASE64Encoder base = new BASE64Encoder();
         
         //将byte[]转换为base64
         String s = base.encode(b);
         //String base64 = base.encode("五笔字型电子计算机".getBytes());
         
         System.out.println(s);
         
         //System.out.println("十六进制密钥长度为"+s.length());
         //System.out.println("二进制密钥的长度为"+s.length()*4);
       }
       catch (NoSuchAlgorithmException e) {
         e.printStackTrace();
         System.out.println("没有此算法。");
       }
     }
     
     /**
      * byte数组转化为16进制字符串
      * @param bytes
      * @return
      
      public static String byteToHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
          String strHex=Integer.toHexString(bytes[i]);
          if(strHex.length() > 3) {
            sb.append(strHex.substring(6));
          } else {
            if(strHex.length() < 2) {
              sb.append("0" + strHex);
            } else {
              sb.append(strHex);
            }
          }
        }
        return sb.toString();
      }*/

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        getKey();
    }

}

别的参考:https://blog.csdn.net/sahadev_/article/details/48769857

 

Byte[] 转Base64Eclipse中无法直接使用Base64Encoder:

https://blog.csdn.net/u011514810/article/details/72725398 

byte[] 与Base64的互相转换:https://zhidao.baidu.com/question/921599647972485219.html

 

RSA(包括PKCS8)

linux生成RSA(用openssl):https://www.cnblogs.com/fps2tao/p/8685588.html