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();
        }
    
    }
  • 相关阅读:
    sqlite 一些常用的句子
    SnackBar使用
    semaphore demo 并行 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Expanded, SingleChildScrollView, CustomScrollView, container, height, width
    2个监听器+ dialog + replysubject + extends
    Transparent PageRoute in Flutter for displaying a (semi-) transparent page
    股票价格指数+加权
    Dojo
    iOS各版本图标尺寸汇总
    Xcode6 storyboard new push segue 后的视图控制器没有navigation item bug.
  • 原文地址:https://www.cnblogs.com/lxaic/p/5646383.html
Copyright © 2011-2022 走看看