Skip to content

Commit

Permalink
change comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
panxw committed Jul 2, 2015
1 parent 68c67ca commit 4c1994d
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 123 deletions.
246 changes: 123 additions & 123 deletions JavaAlgorithmHelper/src/com/java/alogrithm/helper/AESHelper.java
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;
}

}
11 changes: 11 additions & 0 deletions JavaAlgorithmHelper/src/com/java/alogrithm/utils/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

/**
* Base64字符串与字节码转换工具
Expand Down Expand Up @@ -144,6 +145,16 @@ public static String filter(String str) {
}


public static void main(String[] args) {
try {
System.out.println(encode("asdssdfafasdfsw12345?@!#~#@#%#$%&%^*&&(&*)_()+()+sdfadsfsdfsfasd-*/878ssdfasdfasdfsadfsdafsadfasdfasdf".getBytes("UTF-8")));
System.out.println(new String(decode("YXNkc3NkZmFmYXNkZnN3MTIzNDU/QCEjfiNAIyUjJCUmJV4qJiYoJiopXygp KygpK3NkZmFkc2ZzZGZzZmFzZC0qLzg3OHNzZGZhc2RmYXNkZnNhZGZzZGFm c2FkZmFzZGZhc2Rm"), "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// private static final char last2byte = (char) Integer
// .parseInt("00000011", 2);
//
Expand Down

0 comments on commit 4c1994d

Please sign in to comment.