-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
panxw
committed
Jul 2, 2015
1 parent
68c67ca
commit 4c1994d
Showing
2 changed files
with
134 additions
and
123 deletions.
There are no files selected for viewing
246 changes: 123 additions & 123 deletions
246
JavaAlgorithmHelper/src/com/java/alogrithm/helper/AESHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,123 @@ | ||
package com.java.alogrithm.helper; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.NoSuchPaddingException; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
import com.java.alogrithm.utils.Base64; | ||
|
||
|
||
/** | ||
* AES算法加密,传输,解密过程示例(AES可以使用128、192、和256位密钥,并且用128位分组加密和解密数据) | ||
* 默认只能用16位密钥加密,但用security包下的java包换掉jre中对应的后,可任意密钥加解密。 | ||
* | ||
* @author steven-pan | ||
* | ||
*/ | ||
public class AESHelper { | ||
|
||
private static Cipher cipher = null; // 私鈅加密对象Cipher | ||
|
||
public static void main(String args[]) { | ||
System.out.println("AES加解密测试:"); | ||
|
||
String password = "c8a9229820ffa315bc6a17a9e43d01a9"; | ||
String content = "6222001521522152212"; | ||
// 加密(传输) | ||
System.out.println("加密前:" + content); | ||
byte[] encryptResult = encrypt(content, password); | ||
|
||
// 以HEX进行传输 | ||
String codedtextb = Base64.encode(encryptResult);// data transfer as text | ||
System.out.println("Base64 format:" + codedtextb); | ||
encryptResult = Base64.decode(codedtextb); | ||
|
||
// 解密 | ||
String decryptResultb = decrypt(encryptResult, password); | ||
System.out.println("解密后:" + decryptResultb); | ||
} | ||
|
||
static { | ||
try { | ||
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); | ||
} catch (NoSuchAlgorithmException e) { | ||
e.printStackTrace(); | ||
} catch (NoSuchPaddingException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* 加密 | ||
* | ||
* @param message | ||
* @return | ||
*/ | ||
public static byte[] encrypt(String message, String passWord) { | ||
// if (passWord == null) { | ||
// System.out.print("passWord为空null"); | ||
// return null; | ||
// } | ||
// // 判断passWord是否为16位 | ||
// if (passWord.length() != 16) { | ||
// System.out.print("Key长度不是16位"); | ||
// return null; | ||
// } | ||
|
||
try { | ||
/* AES算法 */ | ||
SecretKey secretKey = new SecretKeySpec(passWord.getBytes(), "AES");// 获得密钥 | ||
/* 获得一个私鈅加密类Cipher,DESede-》AES算法,ECB是加密模式,PKCS5Padding是填充方式 */ | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey); // 设置工作模式为加密模式,给出密钥 | ||
byte[] resultBytes = cipher.doFinal(message.getBytes("UTF-8")); // 正式执行加密操作 | ||
return resultBytes; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 解密 | ||
* | ||
* @param message | ||
* @return | ||
* @throws Exception | ||
*/ | ||
public static String decrypt(byte[] messageBytes, String passWord) { | ||
String result = ""; | ||
try { | ||
/* AES算法 */ | ||
SecretKey secretKey = new SecretKeySpec(passWord.getBytes(), "AES");// 获得密钥 | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey); // 设置工作模式为解密模式,给出密钥 | ||
byte[] resultBytes = cipher.doFinal(messageBytes);// 正式执行解密操作 | ||
result = new String(resultBytes, "UTF-8"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* 去掉加密字符串换行符 | ||
* | ||
* @param str | ||
* @return | ||
*/ | ||
public static String filter(String str) { | ||
String output = ""; | ||
StringBuffer sb = new StringBuffer(); | ||
for (int i = 0; i < str.length(); i++) { | ||
int asc = str.charAt(i); | ||
if (asc != 10 && asc != 13) { | ||
sb.append(str.subSequence(i, i + 1)); | ||
} | ||
} | ||
output = new String(sb); | ||
return output; | ||
} | ||
|
||
} | ||
package com.java.alogrithm.helper; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.NoSuchPaddingException; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
import com.java.alogrithm.utils.Base64; | ||
|
||
|
||
/** | ||
* AES算法加密,传输,解密过程示例(AES可以使用128、192、和256位密钥,并且用128位分组加密和解密数据) | ||
* 默认只能用16个字节(128)位密钥,但使用本项目security目录下的jar包替换java jre目录下的文件后,则可以使用最高32字节(256位)密钥 了。 | ||
* | ||
* @author steven-pan | ||
* | ||
*/ | ||
public class AESHelper { | ||
|
||
private static Cipher cipher = null; // 私鈅加密对象Cipher | ||
|
||
public static void main(String args[]) { | ||
System.out.println("AES加解密测试:"); | ||
|
||
String password = "c8a9229820ffa315bc6a17a9e43d01a9"; | ||
String content = "6222001521522152212"; | ||
// 加密(传输) | ||
System.out.println("加密前:" + content); | ||
byte[] encryptResult = encrypt(content, password); | ||
|
||
// 以HEX进行传输 | ||
String codedtextb = Base64.encode(encryptResult);// data transfer as text | ||
System.out.println("Base64 format:" + codedtextb); | ||
encryptResult = Base64.decode(codedtextb); | ||
|
||
// 解密 | ||
String decryptResultb = decrypt(encryptResult, password); | ||
System.out.println("解密后:" + decryptResultb); | ||
} | ||
|
||
static { | ||
try { | ||
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); | ||
} catch (NoSuchAlgorithmException e) { | ||
e.printStackTrace(); | ||
} catch (NoSuchPaddingException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* 加密 | ||
* | ||
* @param message | ||
* @return | ||
*/ | ||
public static byte[] encrypt(String message, String passWord) { | ||
// if (passWord == null) { | ||
// System.out.print("passWord为空null"); | ||
// return null; | ||
// } | ||
// // 判断passWord是否为16位 | ||
// if (passWord.length() != 16) { | ||
// System.out.print("Key长度不是16位"); | ||
// return null; | ||
// } | ||
|
||
try { | ||
/* AES算法 */ | ||
SecretKey secretKey = new SecretKeySpec(passWord.getBytes(), "AES");// 获得密钥 | ||
/* 获得一个私鈅加密类Cipher,DESede-》AES算法,ECB是加密模式,PKCS5Padding是填充方式 */ | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey); // 设置工作模式为加密模式,给出密钥 | ||
byte[] resultBytes = cipher.doFinal(message.getBytes("UTF-8")); // 正式执行加密操作 | ||
return resultBytes; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 解密 | ||
* | ||
* @param message | ||
* @return | ||
* @throws Exception | ||
*/ | ||
public static String decrypt(byte[] messageBytes, String passWord) { | ||
String result = ""; | ||
try { | ||
/* AES算法 */ | ||
SecretKey secretKey = new SecretKeySpec(passWord.getBytes(), "AES");// 获得密钥 | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey); // 设置工作模式为解密模式,给出密钥 | ||
byte[] resultBytes = cipher.doFinal(messageBytes);// 正式执行解密操作 | ||
result = new String(resultBytes, "UTF-8"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* 去掉加密字符串换行符 | ||
* | ||
* @param str | ||
* @return | ||
*/ | ||
public static String filter(String str) { | ||
String output = ""; | ||
StringBuffer sb = new StringBuffer(); | ||
for (int i = 0; i < str.length(); i++) { | ||
int asc = str.charAt(i); | ||
if (asc != 10 && asc != 13) { | ||
sb.append(str.subSequence(i, i + 1)); | ||
} | ||
} | ||
output = new String(sb); | ||
return output; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters