package emails; import sun.misc.BASE64Decoder; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; /** * AES加解密 * * Created by yyh on 2015/10/9. */ public class AESUtils { /** * 密钥算法 */ private static final String ALGORITHM = "AES"; /** * 加解密算法/工作模式/填充方式 */ private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding"; /** * SecretKeySpec类是KeySpec接口的实现类,用于构建秘密密钥规范 */ private SecretKeySpec key; public AESUtils(String hexKey) { key = new SecretKeySpec(hexKey.getBytes(), ALGORITHM); } /** * AES加密 * @param data * @return * @throws Exception */ public String encryptData(String data) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM_STR); // 创建密码器 cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 return Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes())); } /** * AES解密 * @param base64Data * @return * @throws Exception */ public String decryptData(String base64Data) throws Exception{ Cipher cipher = Cipher.getInstance(ALGORITHM_STR); cipher.init(Cipher.DECRYPT_MODE, key); return new String(cipher.doFinal(Base64.getDecoder().decode(base64Data))); } /** * hex字符串 转 byte数组 * @param s * @return */ private static byte[] hex2byte(String s) { if (s.length() % 2 == 0) { return hex2byte (s.getBytes(), 0, s.length() >> 1); } else { return hex2byte("0"+s); } } private static byte[] hex2byte (byte[] b, int offset, int len) { byte[] d = new byte[len]; for (int i=0; i<len*2; i++) { int shift = i%2 == 1 ? 0 : 4; d[i>>1] |= Character.digit((char) b[offset+i], 16) << shift; } return d; } public static void main(String[] args) throws Exception { AESUtils util = new AESUtils("46EBA22EF5204DD5B110A1F730513965"); // 密钥 System.out.println("cardNo:"+util.encryptData("1234")); // 加密 System.out.println("exp:"+util.decryptData("13tczfN0O+DoPU3BxWECYw==")); // 解密 } }
二、shell 脚本AES加密、解密
加密
echo -n "server: port: 8081 spring: jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 default-property-inclusion: non_null canal.conf: mode: tcp #tcp kafka rocketMQ rabbitMQ flatMessage: true zookeeperHosts: 55.51.44.150:2181 syncBatchSize: 1000 retries: 0 timeout: accessKey: secretKey: consumerProperties: canal.tcp.server.host: canal.tcp.zookeeper.hosts: 55.51.44.150:2181 canal.tcp.batch.size: 500 canal.tcp.username: canal.tcp.password: srcDataSources: defaultDS: url: jdbc:mysql://canal2canal.mysql.dbdns.cmbchina.cn:6446/canal2canal?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowMultiQueries=true username: somdbdev password: B#6GHoFsT canalAdapters: - instance: DMU02es groups: - groupId: g1 outerAdapters: - name: es hosts: 55.11.58.210:8200 properties: mode: rest security.auth: 4316:q1CLjhL4jRn838p" | openssl enc -aes-128-ecb -a -e -pass pass:"46EBA22EF5204DD5" -nosalt; 解密,把内容输入到文件 echo "8osi06GicNq2izhE9KQ09t2Vr5EGcUnIPJc+DrYWoFUYSQ7PSDxUl+nHQnPOtTF2 3hSq+Rrxdgw0133Ms3A+DFCgpDcLZ6mZVP3kKiOi2Abw4r0mznH7Ef3h38pxf5/Z wZDxLxITlAdsGibO/F+bD8lzMd10smqZXx3CnzKV504teCCjg7Xqzt/X7fZ8nUI8 Rt0qZr8A7w79yqyboj8923Sd8ONw16LC48b/bRALPnz8LkcUfg3dfN/Jqw4GPSmN yNf9tsyWcv4kA5USWNliqLgGY6N4DTwptGGFR+MZC1xGj1bmPOH9KrFixoDs9MQM /dpax7rKyd90G309XKXMs+ST1ekYqGzxFKgPJ8cHiIIfvkWkzQ+Q7cEu4W4l2o/i cbAMYK9aRmUFe/yf29WAcqrgF/gwaNKj2n9S+JXApaPb61iAQ8QGj/d+DH627WmS 6nuGPm9/omZmknN2zuTIH9O4RCXKbpPknNy4E0kBkQoZCiBFRHj8ESiGty/VeVTb sX5uinxCW3sHy6zHT2F9wWUTYQWaD1bnxtEAciRQZ3iae0u1TT7Yhoxz9WCatUPj fxLBgNL/rElxt9or+b9eUbec6Aejk2kYL/46MQnf5QfBVAeTxVoGIpZu3LErXV1x D9qI6uW6rAzUpH6tl2nJrYIvBS5q/LGFJBMTiuqHA+J8kflyLJDqyXbYI5w803iO d0O3kv9kSKE9tEbLzLssPuVMsMDBRhmTi+4Vvh7QPOAMGklvuR6KyBCU/fnskDC5 4DNK9quIY0JM55fU/31ybhILWp11dLpJhRt8K8bMEv1WBwdjxDtTEcDAwjnmc+kW rGdqLaWGLn8VUIBO3WCKVTDpfZ+5/+MMyyYEs9V4jDhGc96zX6WfwGeUXsLmM5jx gX/FcIjOTo4Mh1k2mnw3pS8r+PIAYfsZpj/3tF9kl7zytlj6xABQEwvBtP9Os69/ N04pUdtCsn06LI/2sc+J17vdUhK3hvrUGyiPx61WVyehXFg4rk7PuVOsv9MRKiWl C49rZOvUWjBtYchq+abEGxY52ZM+S2kM7ZsX/cUB29D7bo1L4XUqsJxutdwwRsIa hDlIQM+ItzuFdeQMZT5uY6U1n6EdGCQX6PQ0rcCLEGyoYaoJ5qVUu1h+8RdYkePh 7v1vN8KoCbSP1NR6HmikNqdN6IiDzM4FEhZxRHhrSNRy1NWTNIq7WEjVXcwK+NCf D+WQoGsJ6eGhry+PPyf7CTofzJoRYbUtKgu6V1K+Zh9ckoWfSAeyDVGcHrU8hiQH NtLMKhomoxUk9iNUsnYtIetv+3qyvK8eyP0/UVaJnnCIdPeAeE8L+c10vX8MhSod oZua1wAwBQxRUTu9TDNKQw==" | openssl enc -aes-128-ecb -a -d -pass pass:"46EBA22EF5204DD5" -nosalt > test.text
标签:AES,加密,String,解密,tcp,Cipher,return,byte From: https://www.cnblogs.com/karinemo/p/17213396.html