1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.security.MessageDigest; import java.util.Base64;
public class CryptoUtil {
public static byte[] aesEncrypt(byte[] data, String key) throws Exception { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("UTF-8"), "AES")); return cipher.doFinal(data); }
public static byte[] aesDecrypt(byte[] data, String key) throws Exception { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("UTF-8"), "AES")); return cipher.doFinal(data); }
public static byte[] aesCbcEncrypt(byte[] data, String key, byte[] iv) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("UTF-8"), "AES"), new javax.crypto.spec.IvParameterSpec(iv)); return cipher.doFinal(data); }
public static String md5(String input) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] digest = md.digest(input.getBytes("UTF-8")); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b & 0xff)); } return sb.toString(); }
public static byte[] xorCrypt(byte[] data, byte[] key) { byte[] result = new byte[data.length]; for (int i = 0; i < data.length; i++) { result[i] = (byte) (data[i] ^ key[i % key.length]); } return result; }
public static byte[] readBody(javax.servlet.http.HttpServletRequest request) throws Exception { int len = request.getContentLength(); byte[] buf = len > 0 ? new byte[len] : new byte[4096]; java.io.InputStream in = request.getInputStream(); java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream(); int n; while ((n = in.read(buf)) != -1) { out.write(buf, 0, n); } return out.toByteArray(); } }
|