zoukankan      html  css  js  c++  java
  • Java AES128加密解密 示例源码(AES/ECB/PKCS7Padding)

      【实例简介】

    等同于 C# 的AES 加密解密方法ECB/PKCS7Padding,如下:

                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                aes.Key = Encoding.Default.GetBytes(key);
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;

    【实例截图】 

    【核心代码】

    import java.security.Security;
     
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
     
    import org.apache.commons.codec.binary.Base64;
    import org.apache.commons.codec.binary.Hex;
     
    import javax.crypto.Cipher;
    import javax.crypto.Mac;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
     
    import javax.crypto.Cipher;
    import javax.crypto.Mac;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
     
     
    import javax.crypto.Cipher;
    import javax.crypto.Mac;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;/**
     *
     * @author Ajit
     *
     *         AES 128 bit ECB PKCS7 padding example
     *
     */
     
    public class AES128ECBwithPKCS7 {
     
        //add new bouncycastle ciphers
        static {
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        }
     
        public static void main(String[] args) {
     
            // encryption key should be multiple of 16 character long
            String key = "abcdefghijklmnop";
            String data = "0123456789";
     
            String encrypted = AES128ECBwithPKCS7.encrypt(data, key);
            System.out.println("加密后数据: "   encrypted);
            String decrypted = AES128ECBwithPKCS7.decrypt(AES128ECBwithPKCS7.encrypt(data, key), key);
            System.out.println("解密后数据: "   decrypted);
        }
     
        /**
         * encrypt input text
         *
         * @param input
         * @param key
         * @return
         */
        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/PKCS7Padding");
                cipher.init(Cipher.ENCRYPT_MODE, skey);
                crypted = cipher.doFinal(input.getBytes());
            } catch (Exception e) {
                System.out.println(e.toString());
                e.printStackTrace();
            }
     
            //return new String(Base64.encodeBase64(crypted));
            return new String(Hex.encodeHex(crypted)).toUpperCase();
    //        return new String(Base64.encodeBase64(crypted));
        }
     
        /**
         * decrypt input text
         *
         * @param input
         * @param key
         * @return
         */
        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/PKCS7Padding");
                cipher.init(Cipher.DECRYPT_MODE, skey);
    //            output = cipher.doFinal(Base64.decodeBase64(input));
                output = cipher.doFinal(Hex.decodeHex(input.toCharArray()));
            } catch (Exception e) {
                System.out.println(e.toString());
            }
            return new String(output);
        }
     
    }

    --------------------------------------------------------------------------------------------------------------------------热爱编程,热爱制作!

  • 相关阅读:
    jmeter性能测试
    Robotframework 之常用断言关键字
    chrome浏览器的调试器乱码问题
    YDKJS:作用域与闭包
    JavaScript事件循环(Event Loop)机制
    第一次react-native项目实践要点总结
    jsonp 演示实例 —— 基于node
    Jquery属性获取——attr()与prop()
    实现跨域的N种方法
    了解闭包
  • 原文地址:https://www.cnblogs.com/varchar-pig/p/14224511.html
Copyright © 2011-2022 走看看