标签:
Blowfish 加密解密
1 /**
2 * Blowfish加密
3 *
4 * @param text 需要加密的数据
5 * @param privateKey 加密密钥
6 * @return
7 */
8 public static String f_EnBlowfish(String text, String privateKey)
9 {
10 byte[] cifrado1 = null;
11 byte[] cifrado2 = null;
12 byte[] mensaje = text.getBytes();
13 try
14 {
15 Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
16 byte[] keys = privateKey.getBytes();
17 SecretKeySpec sk = new SecretKeySpec(keys, "Blowfish");
18 Date inic = new Date();
19 // 加密
20 Cipher c1 = Cipher.getInstance("Blowfish", "BC");
21 c1.init(Cipher.ENCRYPT_MODE, sk);
22 cifrado1 = c1.doFinal(mensaje, 0, mensaje.length);
23 }
24 catch (Exception e)
25 {
26 e.printStackTrace();
27 }
28 return Usual.f_toBase64String(cifrado1);
29 }
30 /**
31 * Blowfish解密
32 *
33 * @param text 需要解密的数据
34 * @param privateKey 加密密钥
35 * @return
36 */
37 public static String f_DeBlowfish(String enText, String privateKey)
38 {
39 byte[] cifrado1 = null;
40 byte[] cifrado2 = null;
41 try
42 {
43 Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
44 byte[] keys = privateKey.getBytes();
45 SecretKeySpec sk = new SecretKeySpec(keys, "Blowfish");
46 // 解密
47 // 将加密后的数据转为byte
48 cifrado1 = Usual.f_fromBase64String(enText);
49 Cipher c2 = Cipher.getInstance("Blowfish", "BC");
50 c2.init(Cipher.DECRYPT_MODE, sk);
51 cifrado2 = c2.doFinal(cifrado1, 0, cifrado1.length);
52 }
53 catch (Exception e)
54 {
55 e.printStackTrace();
56 }
57 return new String(cifrado2, Charset.forName("gb2312"));
58 }
Rijndael 加密解密
1 /**
2 * Rijndael加密
3 *
4 * @param text 需要加密的数据
5 * @param privateKey 加密密钥
6 * @return
7 */
8 public static String f_EnRijndael(String text, String privateKey)
9 {
10 byte[] cifrado1 = null;
11 byte[] cifrado2 = null;
12 byte[] mensaje = text.getBytes();
13 byte[] keys = privateKey.getBytes();
14 // 位数限制问题
15 // http://search.oracle.com/search/search?start=1&search_p_main_operator=all&q=Java+Cryptography+Extension+&group=Technology+Network
16 SecretKeySpec sk = new SecretKeySpec(keys, "Rijndael");
17 Date inic = new Date();
18 try
19 {
20 Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
21 Cipher c1 = Cipher.getInstance("Rijndael", "BC"); // 有可能出现 java.security.NoSuchAlgorithmException异常,使用下面一句试试
22 // Cipher c1 = Cipher.getInstance("Rijndael/ECB/PKCS7Padding", "BC");
23 c1.init(Cipher.ENCRYPT_MODE, sk);
24 // 加密
25 cifrado1 = c1.doFinal(mensaje, 0, mensaje.length);
26 }
27 catch (Exception e)
28 {
29 e.printStackTrace();
30 }
31 return Usual.f_toBase64String(cifrado1);
32 }
33 /**
34 * Rijndael解密
35 * @param enText 需要解密的数据
36 * @param privateKey 加密密钥
37 * @return
38 */
39 public static String f_DeRijndael(String enText, String privateKey)
40 {
41 byte[] cifrado1 = null;
42 byte[] cifrado2 = null;
43 byte[] keys = privateKey.getBytes();
44 // 位数限制问题
45 // http://search.oracle.com/search/search?start=1&search_p_main_operator=all&q=Java+Cryptography+Extension+&group=Technology+Network
46 SecretKeySpec sk = new SecretKeySpec(keys, "Rijndael");
47 try
48 {
49 Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
50 Cipher c2 = Cipher.getInstance("Rijndael", "BC");
51 // Cipher c2 = Cipher.getInstance("Rijndael/ECB/", "BC");
52 // Cipher c2 = Cipher.getInstance("Rijndael/ECB/PKCS7Padding", "BC");
53 c2.init(Cipher.DECRYPT_MODE, sk);
54 // 将加密后的数据转为byte
55 cifrado1 = Usual.f_fromBase64String(enText);
56 // 解密
57 cifrado2 = c2.doFinal(cifrado1, 0, cifrado1.length);
58 }
59 catch (Exception e)
60 {
61 e.printStackTrace();
62 }
63 return new String(cifrado2, 0, cifrado2.length);
64 }
AES 加密解密
1 /**
2 * AES加密
3 *
4 * @param mText 需要加密的数据
5 * @param privateKey 加密密钥
6 * @return
7 */
8 public static String f_EnAES(String mText, String privateKey)
9 {
10 // 注册BouncyCastleProvider,扩展RSA "BC"
11 if (Security.getProvider(new BouncyCastleProvider().getName()) == null)
12 {
13 Security.addProvider(new BouncyCastleProvider());
14 }
15 String enDataStr = Usual.mEmpty;
16 try
17 {
18 byte[] mKeys = privateKey.getBytes();
19 byte[] enData = BCVFactory
20 .enCrpytoSin(Usual.f_toBytes(mText), mKeys, "AES", "AES", "BC", Usual.mEmptyBytes);
21 enDataStr = Usual.f_toBase64String(enData);
22 System.out.println(enDataStr);
23 }
24 catch (Exception e)
25 {
26 System.out.println(e.getMessage());
27 }
28 return enDataStr;
29 }
30 /**
31 * AES解密
32 *
33 * @param mEnText 需要解密的数据
34 * @param privateKey 加密密钥
35 * @return
36 */
37 public static String f_DeAES(String mEnText, String privateKey)
38 {
39 // 注册BouncyCastleProvider,扩展RSA "BC"
40 if (Security.getProvider(new BouncyCastleProvider().getName()) == null)
41 {
42 Security.addProvider(new BouncyCastleProvider());
43 }
44 String deDataStr = Usual.mEmpty;
45 try
46 {
47 byte[] mKeys = privateKey.getBytes();
48 byte[] enData = Usual.f_fromBase64String(mEnText);
49 byte[] deData = BCVFactory.deCrpytoSin(enData, mKeys, "AES", "AES", "BC", Usual.mEmptyBytes);
50 deDataStr = Usual.f_fromBytes(deData);
51 }
52 catch (Exception e)
53 {
54 e.printStackTrace();
55 // TODO: handle exception
56 }
57 return deDataStr;
58 }
DESede 加密解密
1 /**
2 * DESede加密
3 * @param text 需要加密的数据
4 * @param privateKey 加密密钥
5 * @param args
6 */
7 public static String f_EnDESede(String text, String privateKey)
8 {
9 // 注册BouncyCastleProvider,扩展RSA "BC"
10 if (Security.getProvider(new BouncyCastleProvider().getName()) == null)
11 {
12 Security.addProvider(new BouncyCastleProvider());
13 }
14 try
15 {
16 byte[] mKeys = privateKey.getBytes();
17 byte[] enData = BCVFactory.enCrpytoSin(Usual.f_toBytes(text), mKeys, "CBC", "DESede/ECB/PKCS5Padding",
18 "BC", Usual.mEmptyBytes);
19 System.out.println(Usual.f_toBase64String(enData));
20 return Usual.f_toBase64String(enData);
21 }
22 catch (Exception e)
23 {
24 }
25 return null;
26 }
27 /**
28 * DESede解密
29 * @param mEntext 需要解密的数据
30 * @param privateKey 加密密钥
31 *
32 * @param args
33 */
34 public static String f_DeDESede(String mEntext, String privateKey)
35 {
36 // 注册BouncyCastleProvider,扩展RSA "BC"
37 if (Security.getProvider(new BouncyCastleProvider().getName()) == null)
38 {
39 Security.addProvider(new BouncyCastleProvider());
40 }
41 try
42 {
43 byte[] mKeys = privateKey.getBytes();
44 byte[] enData = Usual.f_fromBase64String(mEntext);
45 byte[] deData = BCVFactory.deCrpytoSin(enData, mKeys, "CBC", "DESede/ECB/PKCS5Padding", "BC",
46 Usual.mEmptyBytes);
47 return Usual.f_fromBytes(deData);
48 }
49 catch (Exception e)
50 {
51 }
52 return null;
53 }
1 String Str = "我爱中国"; // 加密数据
2 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
3 keyPairGen.initialize(512); // 密钥位数
4 KeyPair keyPair = keyPairGen.generateKeyPair(); // 密钥对
5
6 PublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 公钥对象
7 PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 私钥对象
8
9 String publicKeyString = RsaCrypto.f_getKeyString(publicKey); //公钥值
10 String privateKeyString = RsaCrypto.f_getKeyString(privateKey);//私钥值
11 String enString = RsaCrypto.enCryptoRSA(publicKeyString, Str); // 公钥加密,私钥解密
12 String dnString = RsaCrypto.deCryptoRSA(privateKeyString, enString);
13 System.out.println("原来的:" + Str + "解密后的 :" + dnString);
14 System.out.println("加密后的token" + enString);
1 /**
2 * 将密钥转换成Base64
3 * @param key 公钥、私钥对象
4 * @return 密钥转换成Base64String
5 * @throws UnsupportedEncodingException 编码错误
6 */
7 public static String f_getKeyString(Key key) throws UnsupportedEncodingException
8 {
9 byte[] keyBytes = key.getEncoded();
10 String s = Usual.mEmpty;
11 // s = (new BASE64Encoder()).encode(keyBytes);
12 // s = new String(Base64.encodeBase64(keyBytes), Usual.mUTF8Name);
13 s = new String(Base64.encode(keyBytes), Usual.mUTF8Name);
14 return s;
15 }
1 /**
2 * RSA 加密
3 *
4 * @param publicKeyString 公钥的Base64string
5 * @param provider 算法实现类库。为"",使用JDK的实现算法。"BC"使用的是bouncycastle
6 * @return 加密后的密文 Base64string
7 * @throws NoSuchAlgorithmException
8 * @throws NoSuchProviderException
9 * @throws NoSuchPaddingException
10 * @throws InvalidKeyException
11 * @throws IllegalBlockSizeException
12 * @throws BadPaddingException
13 * @throws UnsupportedEncodingException
14 * @throws InvalidKeySpecException
15 */
16 public static String enCryptoRSA(String publicKeyString, String text, String provider)
17 throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException,
18 IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidKeySpecException
19 {
20 // 私钥解密
21 PublicKey publicKey = f_getPublicKey(publicKeyString);
22 //加密
23 return enCryptoRSA(provider, text, publicKey);
24
25 }
1 /**
2 * RSA解密,扩展支持BC
3 *
4 * @param privateKeyString 私钥的Base64String
5 * @param enString 加密后的密文,并转成Base64String
6 * @return
7 * @throws BadPaddingException
8 * @throws IllegalBlockSizeException
9 * @throws NoSuchPaddingException
10 * @throws NoSuchProviderException
11 * @throws InvalidKeySpecException
12 * @throws NoSuchAlgorithmException
13 * @throws UnsupportedEncodingException
14 * @throws InvalidKeyException
15 */
16 public static String deCryptoRSA(String privateKeyString, String enString) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
17 {
18 if (Security.getProvider(new BouncyCastleProvider().getName()) == null)
19 {
20 Security.addProvider(new BouncyCastleProvider());
21 }
22 return deCryptoRSA(privateKeyString, enString, "BC");
23 }
标签:
原文地址:http://www.cnblogs.com/zhangyu1024/p/5193375.html