Java加密工具类非原创

 1  import java.security.InvalidKeyException;
 2     import java.security.NoSuchAlgorithmException;
 3     import java.security.Security;
 4
 5     import javax.crypto.BadPaddingException;
 6     import javax.crypto.Cipher;
 7     import javax.crypto.IllegalBlockSizeException;
 8     import javax.crypto.KeyGenerator;
 9     import javax.crypto.NoSuchPaddingException;
10     import javax.crypto.SecretKey;
11
12     public class EncrypAES {
13
14         //KeyGenerator 提供对称密钥生成器的功能,支持各种算法
15         private KeyGenerator keygen;
16         //SecretKey 负责保存对称密钥
17         private SecretKey deskey;
18         //Cipher负责完成加密或解密工作
19         private Cipher c;
20         //该字节数组负责保存加密的结果
21         private byte[] cipherByte;
22
23         public EncrypAES() throws NoSuchAlgorithmException, NoSuchPaddingException{
24             Security.addProvider(new com.sun.crypto.provider.SunJCE());
25             //实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
26             keygen = KeyGenerator.getInstance("AES");
27             //生成密钥
28             deskey = keygen.generateKey();
29             //生成Cipher对象,指定其支持的DES算法
30             c = Cipher.getInstance("AES");
31         }
32
33         /**
34          * 对字符串加密
35          *
36          * @param str
37          * @return
38          * @throws InvalidKeyException
39          * @throws IllegalBlockSizeException
40          * @throws BadPaddingException
41          */
42         public byte[] Encrytor(String str) throws InvalidKeyException,
43                 IllegalBlockSizeException, BadPaddingException {
44             // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
45             c.init(Cipher.ENCRYPT_MODE, deskey);
46             byte[] src = str.getBytes();
47             // 加密,结果保存进cipherByte
48             cipherByte = c.doFinal(src);
49             return cipherByte;
50         }
51
52         /**
53          * 对字符串解密
54          *
55          * @param buff
56          * @return
57          * @throws InvalidKeyException
58          * @throws IllegalBlockSizeException
59          * @throws BadPaddingException
60          */
61         public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
62                 IllegalBlockSizeException, BadPaddingException {
63             // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
64             c.init(Cipher.DECRYPT_MODE, deskey);
65             cipherByte = c.doFinal(buff);
66             return cipherByte;
67         }
68
69         /**
70          * @param args
71          * @throws NoSuchPaddingException
72          * @throws NoSuchAlgorithmException
73          * @throws BadPaddingException
74          * @throws IllegalBlockSizeException
75          * @throws InvalidKeyException
76          */
77         public static void main(String[] args) throws Exception {
78             EncrypAES de1 = new EncrypAES();
79             String msg ="郭XX-搞笑相声全集";
80             byte[] encontent = de1.Encrytor(msg);
81             byte[] decontent = de1.Decryptor(encontent);
82             System.out.println("明文是:" + msg);
83             System.out.println("加密后:" + new String(encontent));
84             System.out.println("解密后:" + new String(decontent));
85         }
86
87     }  
 1  import java.security.InvalidKeyException;
 2     import java.security.NoSuchAlgorithmException;
 3     import java.security.Security;
 4
 5     import javax.crypto.BadPaddingException;
 6     import javax.crypto.Cipher;
 7     import javax.crypto.IllegalBlockSizeException;
 8     import javax.crypto.KeyGenerator;
 9     import javax.crypto.NoSuchPaddingException;
10     import javax.crypto.SecretKey;
11
12     public class EncrypDES {
13
14         //KeyGenerator 提供对称密钥生成器的功能,支持各种算法
15         private KeyGenerator keygen;
16         //SecretKey 负责保存对称密钥
17         private SecretKey deskey;
18         //Cipher负责完成加密或解密工作
19         private Cipher c;
20         //该字节数组负责保存加密的结果
21         private byte[] cipherByte;
22
23         public EncrypDES() throws NoSuchAlgorithmException, NoSuchPaddingException{
24             Security.addProvider(new com.sun.crypto.provider.SunJCE());
25             //实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
26             keygen = KeyGenerator.getInstance("DES");
27             //生成密钥
28             deskey = keygen.generateKey();
29             //生成Cipher对象,指定其支持的DES算法
30             c = Cipher.getInstance("DES");
31         }
32
33         /**
34          * 对字符串加密
35          *
36          * @param str
37          * @return
38          * @throws InvalidKeyException
39          * @throws IllegalBlockSizeException
40          * @throws BadPaddingException
41          */
42         public byte[] Encrytor(String str) throws InvalidKeyException,
43                 IllegalBlockSizeException, BadPaddingException {
44             // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
45             c.init(Cipher.ENCRYPT_MODE, deskey);
46             byte[] src = str.getBytes();
47             // 加密,结果保存进cipherByte
48             cipherByte = c.doFinal(src);
49             return cipherByte;
50         }
51
52         /**
53          * 对字符串解密
54          *
55          * @param buff
56          * @return
57          * @throws InvalidKeyException
58          * @throws IllegalBlockSizeException
59          * @throws BadPaddingException
60          */
61         public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
62                 IllegalBlockSizeException, BadPaddingException {
63             // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
64             c.init(Cipher.DECRYPT_MODE, deskey);
65             cipherByte = c.doFinal(buff);
66             return cipherByte;
67         }
68
69         /**
70          * @param args
71          * @throws NoSuchPaddingException
72          * @throws NoSuchAlgorithmException
73          * @throws BadPaddingException
74          * @throws IllegalBlockSizeException
75          * @throws InvalidKeyException
76          */
77         public static void main(String[] args) throws Exception {
78             EncrypDES de1 = new EncrypDES();
79             String msg ="郭XX-搞笑相声全集";
80             byte[] encontent = de1.Encrytor(msg);
81             byte[] decontent = de1.Decryptor(encontent);
82             System.out.println("明文是:" + msg);
83             System.out.println("加密后:" + new String(encontent));
84             System.out.println("解密后:" + new String(decontent));
85         }
86
87     }  
 1   import java.security.InvalidKeyException;
 2     import java.security.NoSuchAlgorithmException;
 3     import java.security.Security;
 4
 5     import javax.crypto.BadPaddingException;
 6     import javax.crypto.Cipher;
 7     import javax.crypto.IllegalBlockSizeException;
 8     import javax.crypto.KeyGenerator;
 9     import javax.crypto.NoSuchPaddingException;
10     import javax.crypto.SecretKey;
11
12     public class EncrypDES3 {
13
14         // KeyGenerator 提供对称密钥生成器的功能,支持各种算法
15         private KeyGenerator keygen;
16         // SecretKey 负责保存对称密钥
17         private SecretKey deskey;
18         // Cipher负责完成加密或解密工作
19         private Cipher c;
20         // 该字节数组负责保存加密的结果
21         private byte[] cipherByte;
22
23         public EncrypDES3() throws NoSuchAlgorithmException, NoSuchPaddingException {
24             Security.addProvider(new com.sun.crypto.provider.SunJCE());
25             // 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
26             keygen = KeyGenerator.getInstance("DESede");
27             // 生成密钥
28             deskey = keygen.generateKey();
29             // 生成Cipher对象,指定其支持的DES算法
30             c = Cipher.getInstance("DESede");
31         }
32
33         /**
34          * 对字符串加密
35          *
36          * @param str
37          * @return
38          * @throws InvalidKeyException
39          * @throws IllegalBlockSizeException
40          * @throws BadPaddingException
41          */
42         public byte[] Encrytor(String str) throws InvalidKeyException,
43                 IllegalBlockSizeException, BadPaddingException {
44             // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
45             c.init(Cipher.ENCRYPT_MODE, deskey);
46             byte[] src = str.getBytes();
47             // 加密,结果保存进cipherByte
48             cipherByte = c.doFinal(src);
49             return cipherByte;
50         }
51
52         /**
53          * 对字符串解密
54          *
55          * @param buff
56          * @return
57          * @throws InvalidKeyException
58          * @throws IllegalBlockSizeException
59          * @throws BadPaddingException
60          */
61         public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
62                 IllegalBlockSizeException, BadPaddingException {
63             // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
64             c.init(Cipher.DECRYPT_MODE, deskey);
65             cipherByte = c.doFinal(buff);
66             return cipherByte;
67         }
68
69         /**
70          * @param args
71          * @throws NoSuchPaddingException
72          * @throws NoSuchAlgorithmException
73          * @throws BadPaddingException
74          * @throws IllegalBlockSizeException
75          * @throws InvalidKeyException
76          */
77         public static void main(String[] args) throws Exception {
78             EncrypDES3 des = new EncrypDES3();
79             String msg ="郭XX-搞笑相声全集";
80             byte[] encontent = des.Encrytor(msg);
81             byte[] decontent = des.Decryptor(encontent);
82             System.out.println("明文是:" + msg);
83             System.out.println("加密后:" + new String(encontent));
84             System.out.println("解密后:" + new String(decontent));
85
86         }
87
88     }  
 1  import java.security.MessageDigest;
 2     import java.security.NoSuchAlgorithmException;
 3
 4     public class EncrypMD5 {
 5
 6         public byte[] eccrypt(String info) throws NoSuchAlgorithmException{
 7             //根据MD5算法生成MessageDigest对象
 8             MessageDigest md5 = MessageDigest.getInstance("MD5");
 9             byte[] srcBytes = info.getBytes();
10             //使用srcBytes更新摘要
11             md5.update(srcBytes);
12             //完成哈希计算,得到result
13             byte[] resultBytes = md5.digest();
14             return resultBytes;
15         }
16
17
18         public static void main(String args[]) throws NoSuchAlgorithmException{
19             String msg = "郭XX-精品相声技术";
20             EncrypMD5 md5 = new EncrypMD5();
21             byte[] resultBytes = md5.eccrypt(msg);
22
23             System.out.println("密文是:" + new String(resultBytes));
24             System.out.println("明文是:" + msg);
25         }
26
27     }  
 1 import java.security.InvalidKeyException;
 2     import java.security.KeyPair;
 3     import java.security.KeyPairGenerator;
 4     import java.security.NoSuchAlgorithmException;
 5     import java.security.interfaces.RSAPrivateKey;
 6     import java.security.interfaces.RSAPublicKey;
 7
 8     import javax.crypto.BadPaddingException;
 9     import javax.crypto.Cipher;
10     import javax.crypto.IllegalBlockSizeException;
11     import javax.crypto.NoSuchPaddingException;
12
13     public class EncrypRSA {
14
15         /**
16          * 加密
17          * @param publicKey
18          * @param srcBytes
19          * @return
20          * @throws NoSuchAlgorithmException
21          * @throws NoSuchPaddingException
22          * @throws InvalidKeyException
23          * @throws IllegalBlockSizeException
24          * @throws BadPaddingException
25          */
26         protected byte[] encrypt(RSAPublicKey publicKey,byte[] srcBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
27             if(publicKey!=null){
28                 //Cipher负责完成加密或解密工作,基于RSA
29                 Cipher cipher = Cipher.getInstance("RSA");
30                 //根据公钥,对Cipher对象进行初始化
31                 cipher.init(Cipher.ENCRYPT_MODE, publicKey);
32                 byte[] resultBytes = cipher.doFinal(srcBytes);
33                 return resultBytes;
34             }
35             return null;
36         }
37
38         /**
39          * 解密
40          * @param privateKey
41          * @param srcBytes
42          * @return
43          * @throws NoSuchAlgorithmException
44          * @throws NoSuchPaddingException
45          * @throws InvalidKeyException
46          * @throws IllegalBlockSizeException
47          * @throws BadPaddingException
48          */
49         protected byte[] decrypt(RSAPrivateKey privateKey,byte[] srcBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
50             if(privateKey!=null){
51                 //Cipher负责完成加密或解密工作,基于RSA
52                 Cipher cipher = Cipher.getInstance("RSA");
53                 //根据公钥,对Cipher对象进行初始化
54                 cipher.init(Cipher.DECRYPT_MODE, privateKey);
55                 byte[] resultBytes = cipher.doFinal(srcBytes);
56                 return resultBytes;
57             }
58             return null;
59         }
60
61         /**
62          * @param args
63          * @throws NoSuchAlgorithmException
64          * @throws BadPaddingException
65          * @throws IllegalBlockSizeException
66          * @throws NoSuchPaddingException
67          * @throws InvalidKeyException
68          */
69         public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
70             EncrypRSA rsa = new EncrypRSA();
71             String msg = "郭XX-精品相声";
72             //KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
73             KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
74             //初始化密钥对生成器,密钥大小为1024位
75             keyPairGen.initialize(1024);
76             //生成一个密钥对,保存在keyPair中
77             KeyPair keyPair = keyPairGen.generateKeyPair();
78             //得到私钥
79             RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate();
80             //得到公钥
81             RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic();
82
83             //用公钥加密
84             byte[] srcBytes = msg.getBytes();
85             byte[] resultBytes = rsa.encrypt(publicKey, srcBytes);
86
87             //用私钥解密
88             byte[] decBytes = rsa.decrypt(privateKey, resultBytes);
89
90             System.out.println("明文是:" + msg);
91             System.out.println("加密后是:" + new String(resultBytes));
92             System.out.println("解密后是:" + new String(decBytes));
93         }
94
95     } 
 1  import java.security.MessageDigest;
 2     import java.security.NoSuchAlgorithmException;
 3
 4     public class EncrypSHA {
 5
 6         public byte[] eccrypt(String info) throws NoSuchAlgorithmException{
 7             MessageDigest md5 = MessageDigest.getInstance("SHA");
 8             byte[] srcBytes = info.getBytes();
 9             //使用srcBytes更新摘要
10             md5.update(srcBytes);
11             //完成哈希计算,得到result
12             byte[] resultBytes = md5.digest();
13             return resultBytes;
14         }
15
16         /**
17          * @param args
18          * @throws NoSuchAlgorithmException
19          */
20         public static void main(String[] args) throws NoSuchAlgorithmException {
21             String msg = "郭XX-精品相声技术";
22             EncrypSHA sha = new EncrypSHA();
23             byte[] resultBytes = sha.eccrypt(msg);
24             System.out.println("明文是:" + msg);
25             System.out.println("密文是:" + new String(resultBytes));
26
27         }
28
29     } 
时间: 2024-10-15 11:27:35

Java加密工具类非原创的相关文章

Java加密工具类(依赖:java.security.MessageDigest或org.apache.commons.codec.digest.DigestUtils)

依赖于java.security.MessageDigest,支持MD5,SHA-1,SHA-256 1 import java.security.MessageDigest; 2 import java.security.NoSuchAlgorithmException; 3 4 /** 5 * CiphertextUtil 6 * 7 * @author ysj 8 */ 9 public class CiphertextUtil { 10 public static final Strin

Java AES 加密工具类

package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.logging.Level; import java.util.logging.Logger; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import jav

加密工具类 - CryptoUtils.java

加密工具类,包含MD5,BASE64,SHA,CRC32的加密与解密方法. 源码如下:(点击下载  - CryptoUtils.java.commons-io-2.4.jar.commons-codec-1.9.jar ) import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java

Java MD5,base64,AES加密工具类

import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.regex.Matcher; import java.util.regex.Pat

android开发MD5加密工具类(一)

MD5加密工具类整理: 1 package com.gzcivil.utils; 2 3 import java.io.UnsupportedEncodingException; 4 import java.security.MessageDigest; 5 import java.security.NoSuchAlgorithmException; 6 7 public class MD5Tool { 8 9 public static String md5(String string) {

android加密工具类

import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * 加密工具类 * Created by Administrator on 2015/10/21 0021. */ public class EncryptUtils { /** * 字符串加密使用MD5算法 */ public final static String encryptMD5(String source) {

wemall app商城源码android开发MD5加密工具类

wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享android开发MD5加密工具类主要代码,供技术员参考学习. package com.gzcivil.utils; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgori

md5加密工具类

md5加密工具类: package cn.sniper.encrypt.util; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /**  * 加密工具类  *   * md5加密出来的长度是32位  *   * sha加密出来的长度是40位  *   * @author sniper  

Java并发工具类CyclicBarrier

CyclicBarrier同步屏障 java并发工具类中有一个叫做CyclicBarrier的类,与CountDownLatch类似,都可以实现线程间的同步,但是差别是CyclicBarrier是可重置的同步屏障. 想象一个场景,有N个人不同时间走到一扇门,因为门需要N个人合力才能推开,所以人不足N个时,只能阻塞在此,等到N个人都到了之后,可以推开门,继续进行之前的工作.CyclicBarrier就是这扇门. 看看下面的代码,定义了一个线程数为2的,CyclicBarrier,并在主线程和另外一