Java tutorial
/* * Copyright (C) 2010-2101 Alibaba Group Holding Limited. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.otter.node.etl.common.io; import org.apache.commons.lang.StringUtils; import com.alibaba.otter.node.etl.common.io.compress.Compressor; import com.alibaba.otter.node.etl.common.io.compress.impl.gzip.GzipCompressor; import com.alibaba.otter.node.etl.common.io.crypto.AESUtils; import com.alibaba.otter.node.etl.common.io.signature.ChecksumException; import com.alibaba.otter.node.etl.common.io.signature.ChecksumUtils; /** * ?? * * @author jianghang 2011-10-13 ?07:37:46 * @version 4.0.0 */ public class EncryptUtils { private static final Compressor COMPRESSOR = new GzipCompressor(); public static EncryptedData encrypt(byte[] input) { // ? byte[] compData = COMPRESSOR.compress(input); // AESUtils aes = new AESUtils(); aes.generateSecretKey(); // ? byte[] encryptData = aes.encrypt(compData); return new EncryptedData(encryptData, aes.getSecretyKeyString(), ChecksumUtils.checksum(encryptData)); } public static byte[] decrypt(EncryptedData encode) { String destCrc = ChecksumUtils.checksum(encode.getData()); //?sign if (false == StringUtils.equals(encode.getCrc(), destCrc)) { throw new ChecksumException(String.format("orig: %s, parsed: %s not match", encode.getCrc(), destCrc)); } // AESUtils aes = new AESUtils(); aes.setSecretKeyString(encode.getKey()); // ? return COMPRESSOR.decompress(aes.decrypt(encode.getData())); } }