如何实现jsp的url加密
发布网友
发布时间:2022-04-25 22:48
我来回答
共1个回答
热心网友
时间:2022-06-18 09:05
加密参数是jsp参数传递中的一种安全措施,加密方法如下:
建议使用对称加密如:DES或者是PBE算法。
加密算法如下:
public static String encrypt(String key, String plainText) throws Exception {
String encryptTxt = "";
try {
byte[] salt = new byte[8];
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(key.getBytes());
byte[] digest = md.digest();
for (int i = 0; i < 8; i++) {
salt[i] = digest[i];
}
PBEKeySpec pbeKeySpec = new PBEKeySpec(key.toCharArray());
SecretKeyFactory keyFactory = SecretKeyFactory
.getInstance("PBEWithMD5AndDES");
SecretKey skey = keyFactory.generateSecret(pbeKeySpec);
PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.ENCRYPT_MODE, skey, paramSpec);
byte[] cipherText = cipher.doFinal(plainText.getBytes());
String saltString = new String(Base64.encode(salt));
String ciphertextString = new String(Base64.encode(cipherText));
return saltString + ciphertextString;
} catch (Exception e) {
throw new Exception("Encrypt Text Error:" + e.getMessage(), e);
}
使用方法:
把jsp?后面的参数作为参数传入encrypt();然后就可以返回加密串了。