首页 > 代码库 > Java加密工具类非原创

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 str37          * @return38          * @throws InvalidKeyException39          * @throws IllegalBlockSizeException40          * @throws BadPaddingException41          */ 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 =http://www.mamicode.com/ str.getBytes(); 47             // 加密,结果保存进cipherByte 48             cipherByte = c.doFinal(src); 49             return cipherByte; 50         } 51      52         /**53          * 对字符串解密54          * 55          * @param buff56          * @return57          * @throws InvalidKeyException58          * @throws IllegalBlockSizeException59          * @throws BadPaddingException60          */ 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 args71          * @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 str37          * @return38          * @throws InvalidKeyException39          * @throws IllegalBlockSizeException40          * @throws BadPaddingException41          */ 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 =http://www.mamicode.com/ str.getBytes(); 47             // 加密,结果保存进cipherByte 48             cipherByte = c.doFinal(src); 49             return cipherByte; 50         } 51      52         /**53          * 对字符串解密54          * 55          * @param buff56          * @return57          * @throws InvalidKeyException58          * @throws IllegalBlockSizeException59          * @throws BadPaddingException60          */ 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 args71          * @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 str37          * @return38          * @throws InvalidKeyException39          * @throws IllegalBlockSizeException40          * @throws BadPaddingException41          */ 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 =http://www.mamicode.com/ str.getBytes(); 47             // 加密,结果保存进cipherByte 48             cipherByte = c.doFinal(src); 49             return cipherByte; 50         } 51      52         /**53          * 对字符串解密54          * 55          * @param buff56          * @return57          * @throws InvalidKeyException58          * @throws IllegalBlockSizeException59          * @throws BadPaddingException60          */ 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 args71          * @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 publicKey18          * @param srcBytes19          * @return20          * @throws NoSuchAlgorithmException21          * @throws NoSuchPaddingException22          * @throws InvalidKeyException23          * @throws IllegalBlockSizeException24          * @throws BadPaddingException25          */ 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 privateKey41          * @param srcBytes42          * @return43          * @throws NoSuchAlgorithmException44          * @throws NoSuchPaddingException45          * @throws InvalidKeyException46          * @throws IllegalBlockSizeException47          * @throws BadPaddingException48          */ 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 args63          * @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 args18          * @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     } 

 

Java加密工具类非原创