从来没有接触过java加密,近期在工作中遇到java加密的问题,由于对java加密不是很清楚,在写程序的过程中挺纠结的,最后终于搞定,现在把示例程序的代码贴上来,供大家看看。有什么问题请尽管拍砖。
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
/**
* 对字符串进行加密和解密
* @author
*
*/
public class Encryptions {
/**
* 对字符串进行加密,并返回加密后的字符串。<br>
* 加密注意事项:<br>
* 如果用ECB保密模式,iv字符串必须为null,否则会造成加密出错的问题。<br>
* @param target 要加密的字符串
* @param keyString 密钥
* @param algorithm 加密算法名称
* @param mode 保密模式
* @param padding 加密填充模式
* @param iv 向量字符串
* @return 加密后的字符串
*/
public String encrypt(String target, String keyString, String algorithm, String mode, String padding, String iv) {
SecretKeySpec keySpec = new SecretKeySpec(keyString.getBytes(), algorithm);
String encryptStr = null;
String algorithmStr = null;
if (mode != null && padding !=null && !mode.isEmpty() && !padding.isEmpty()) {
StringBuilder algorithmBuilder = new StringBuilder(CommonConst.STTING_BUILDER_CAPACITY);
algorithmBuilder.append(algorithm);
algorithmBuilder.append(CommonConst.SLASH);
algorithmBuilder.append(mode);
algorithmBuilder.append(CommonConst.SLASH);
algorithmBuilder.append(padding);
algorithmStr = algorithmBuilder.toString();
} else {
algorithmStr = algorithm;
}
try {
Cipher cipher = Cipher.getInstance(algorithmStr);
if (iv != null) {
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
} else {
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
}
byte[] encryptBytes = cipher.doFinal(target.getBytes());
encryptStr = Base64.encode(encryptBytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return encryptStr;
}
/**
* 对加密后的字符串进行解密,并返回解密后的字符串<br>
* 加密注意事项:<br>
* 如果用ECB保密模式,iv字符串必须为null,否则会造成加密出错的问题。<br>
* @param target 加密后的字符串(待解密字符串)
* @param keyString 密钥
* @param algorithm 加密算法名称
* @param mode 保密模式
* @param padding 填充模式
* @param iv 向量字符串
* @return 解密后的字符串
*/
public String decrypt(String target, String keyString, String algorithm, String mode, String padding, String iv) {
SecretKeySpec keySpec = new SecretKeySpec(keyString.getBytes(), algorithm);
String encryptStr = null;
String algorithmStr = null;
if (mode != null && padding !=null && !mode.isEmpty() && !padding.isEmpty()) {
StringBuilder algorithmBuilder = new StringBuilder(CommonConst.STTING_BUILDER_CAPACITY);
algorithmBuilder.append(algorithm);
algorithmBuilder.append(CommonConst.SLASH);
algorithmBuilder.append(mode);
algorithmBuilder.append(CommonConst.SLASH);
algorithmBuilder.append(padding);
algorithmStr = algorithmBuilder.toString();
} else {
algorithmStr = algorithm;
}
try {
Cipher cipher = Cipher.getInstance(algorithmStr);
if (iv != null) {
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
} else {
cipher.init(Cipher.DECRYPT_MODE, keySpec);
}
byte[] encryptBytes = cipher.doFinal(Base64.decode(target));
encryptStr = new String(encryptBytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return encryptStr;
}
最后的测试程序:
public static void main(String[] args) {
Encryptions demo = new Encryptions();
String before = "http://www.ostools.net/apidocs/apidoc?api=jdk_6u30";
System.out.println("before:" + before);
String after = demo.encrypt(before, "1234456iuyrdhfgd", "AES", "PCBC", "PKCS5Padding","1234456iuyrdhfgd");
System.out.println("after:" + after);
System.out.println("before:" + demo.decrypt(after, "1234456iuyrdhfgd", "AES", "PCBC", "PKCS5Padding", "1234456iuyrdhfgd"));
}
测试结果:
before:http://www.ostools.net/apidocs/apidoc?api=jdk_6u30
after:gn5zPbyJrX1aJzwqy7JltBgjFe0o39SvrGr6z1eyQ94Nm92U71N180UCMphJBToUXav97F+bJWObkk/IXaYldg==
before:http://www.ostools.net/apidocs/apidoc?api=jdk_6u30
转载于:https://blog.51cto.com/zhaojiahong/1253493