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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
| import org.apache.commons.lang.StringUtils; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.spec.AlgorithmParameterSpec;
public class EncodeUtil {
public static void main(String[] args) throws Exception { String key128 = "c4b84456c1379bec99c4d1b7e9f13173"; String key256 = "c4b84456c1379bec99c4d1b7e9f13173c4b84456c1379bec99c4d1b7e9f13173"; byte[] iv = "abcdefgh12345678".getBytes("UTF-8"); String content_str = "helloworld 你好"; byte[] contentbytes = content_str.getBytes("utf-8");
byte[] encryptbytes = EncodeUtil.aesEncryptToECB(contentbytes,key128); byte[] decryptbytes = EncodeUtil.aesDecryptToECB(encryptbytes,key128); System.out.println(new String(decryptbytes,"utf-8"));
encryptbytes = EncodeUtil.aesEncryptToECB(contentbytes,key256); decryptbytes = EncodeUtil.aesDecryptToECB(encryptbytes,key256); System.out.println(new String(decryptbytes,"utf-8"));
String encryptString = EncodeUtil.aesEncryptToECB(content_str,key128); String decryptString = EncodeUtil.aesDecryptToECB(encryptString,key128); System.out.println(decryptString);
encryptString = EncodeUtil.aesEncryptToECB(content_str,key256); decryptString = EncodeUtil.aesDecryptToECB(encryptString,key256); System.out.println(decryptString);
encryptbytes = EncodeUtil.aesEncryptToCBC(contentbytes,key128,iv); decryptbytes = EncodeUtil.aesDecryptToCBC(encryptbytes,key128,iv); System.out.println(new String(decryptbytes,"utf-8"));
encryptbytes = EncodeUtil.aesEncryptToCBC(contentbytes,key256,iv); decryptbytes = EncodeUtil.aesDecryptToCBC(encryptbytes,key256,iv); System.out.println(new String(decryptbytes,"utf-8"));
encryptString = EncodeUtil.aesEncryptToCBC(content_str,key128,iv); decryptString = EncodeUtil.aesDecryptToCBC(encryptString,key128,iv); System.out.println(decryptString);
encryptString = EncodeUtil.aesEncryptToCBC(content_str,key256,iv); decryptString = EncodeUtil.aesDecryptToCBC(encryptString,key256,iv); System.out.println(decryptString);
}
public static String base64Encode(byte[] bytes) { return new BASE64Encoder().encode(bytes); }
public static byte[] base64Decode(String base64Code) throws Exception { return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code); }
public static void checkkey(byte[] key) throws Exception {
if(key.length != 16 && key.length != 32) { throw new Exception("密钥长度错误,必须是16后者32位"); } }
public static byte[] aesEncryptToECB(byte[] content, String encryptKey) throws Exception { byte[] key = org.apache.commons.codec.binary.Hex.decodeHex(encryptKey.toCharArray()); checkkey(key); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES")); return cipher.doFinal(content); }
public static String aesEncryptToECB(String content, String encryptKey) throws Exception { byte[] key = org.apache.commons.codec.binary.Hex.decodeHex(encryptKey.toCharArray()); checkkey(key); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES")); return base64Encode(cipher.doFinal(content.getBytes("utf-8"))); }
public static byte[] aesDecryptToECB(byte[] content, String decryptKey) throws Exception { byte[] key = org.apache.commons.codec.binary.Hex.decodeHex(decryptKey.toCharArray()); checkkey(key); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES")); byte[] decryptBytes = cipher.doFinal(content); return decryptBytes; }
public static String aesDecryptToECB(String content, String decryptKey) throws Exception { byte[] key = org.apache.commons.codec.binary.Hex.decodeHex(decryptKey.toCharArray()); checkkey(key); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES")); byte[] decryptBytes = cipher.doFinal(base64Decode(content)); return new String(decryptBytes,"utf-8"); }
public static byte[] aesEncryptToCBC(byte[] content, String encryptKey,byte[] iv) throws Exception { byte[] key = org.apache.commons.codec.binary.Hex.decodeHex(encryptKey.toCharArray()); checkkey(key); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"),paramSpec); return cipher.doFinal(content); }
public static byte[] aesDecryptToCBC(byte[] content, String decryptKey,byte[] iv) throws Exception { byte[] key = org.apache.commons.codec.binary.Hex.decodeHex(decryptKey.toCharArray()); checkkey(key); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"),paramSpec); return cipher.doFinal(content); }
public static String aesEncryptToCBC(String content, String encryptKey,byte[] iv) throws Exception { byte[] key = org.apache.commons.codec.binary.Hex.decodeHex(encryptKey.toCharArray()); checkkey(key); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"),paramSpec); return base64Encode(cipher.doFinal(content.getBytes("utf-8"))); }
public static String aesDecryptToCBC(String content, String decryptKey,byte[] iv) throws Exception { byte[] key = org.apache.commons.codec.binary.Hex.decodeHex(decryptKey.toCharArray()); checkkey(key); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"),paramSpec); byte[] decryptBytes = cipher.doFinal(base64Decode(content)); return new String(decryptBytes,"utf-8"); } }
|