zoukankan      html  css  js  c++  java
  • AES加密

    import java.io.UnsupportedEncodingException;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    
    import org.apache.commons.codec.binary.Base64;
    import org.apache.commons.lang.StringUtils;
    
    
    public class Security {
        public static final String KEY = "1234567XDE234lom";
    //    private final static Log log = LogFactory.getLog(Security.class);
        public static String encrypt(String input, String key) {
    
            byte[] crypted = null;
    
            try {
                SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, skey);
                crypted = cipher.doFinal(input.getBytes());
            } catch (Exception e) {
    
            }
    
            return new String(Base64.encodeBase64(crypted));
    
        }
    
        public static String decrypt(String input, String key) {
    
            byte[] output = null;
    
            try {
    
                SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
    
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    
                cipher.init(Cipher.DECRYPT_MODE, skey);
    
                output = cipher.doFinal(Base64.decodeBase64(input));
    
            } catch (Exception e) {
    
                System.out.println(e.toString());
    
            }
    
            return new String(output);
    
        }
    
        public static String encryptWithUTF8(String input, String key) {
    
            byte[] crypted = null;
    
            try {
                SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, skey);
                crypted = cipher.doFinal(input.getBytes("UTF-8"));
            } catch (Exception e) {
    
            }
    
            return new String(Base64.encodeBase64(crypted));
    
        }
        
        /**
         * Encrypt with any of tools.
         * 
         * @param string
         *            -Need encrypt string.
         * @param encryptName
         *            -Encrypt tools,can be null or "",default SHA-256.
         * @author EX-ZHOUKAI003
         * @date 2013.12.09
         * **/
        public static String encryptWithAny(String string, String encryptName) {
            MessageDigest md;
            StringBuffer sb = new StringBuffer();
    
            if (StringUtils.isBlank(encryptName)) {
                encryptName = "SHA-256";
            }
    
            try {
                md = MessageDigest.getInstance(encryptName);
                md.update(string.getBytes("UTF-8"));
                for (byte b : md.digest()) {
                    sb.append(String.format("%02X", b));
                }
            } catch (NoSuchAlgorithmException e) {
            } catch (UnsupportedEncodingException e) {
            }
    
            return sb.toString();
        }
    
    }
  • 相关阅读:
    jfixed使固定行列可编辑表格
    Base 64 加密、解密
    去除富文本格式
    阿里移动安全挑战赛第二题研究小结
    CyanogenMod源码下载、编译、刷机过程记录(HTC buzz)
    编译民间安卓源码mokesoures4.2.2报错内容【解决了!】
    Linux下通过NDK的初步使用【详细步骤+截图】
    Linux下实现JNI期间发生的错误……【未解决】
    C++调用Java方法时jvm.dll相关错误 【每次记录完后都被瞬间解决了……】
    JNI学习中——JVM错误:EXCEPTION_ACCESS_VIOLATION (0xc0000005)
  • 原文地址:https://www.cnblogs.com/lxaic/p/5646383.html
Copyright © 2011-2022 走看看