标签 类库 下的文章

安卓端JAVA AES加密对应处理类

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.wiggins.teaching.utils;

import android.util.Base64;
import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESHelper {
    private static final String CipherMode = "AES/ECB/PKCS5Padding";
    private static final String password = "Coding@xxx.China";

    public AESHelper() {
    }

    private static SecretKeySpec createKey(String password) {
        byte[] data = null;
        if(password == null) {
            password = "";
        }

        StringBuffer sb = new StringBuffer(16);
        sb.append(password);

        while(sb.length() < 16) {
            sb.append("0");
        }

        if(sb.length() > 16) {
            sb.setLength(16);
        }

        try {
            data = sb.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException var4) {
            var4.printStackTrace();
        }

        return new SecretKeySpec(data, "AES");
    }

    public static byte[] encrypt(byte[] content, String password) {
        try {
            SecretKeySpec e = createKey(password);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(1, e);
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception var5) {
            var5.printStackTrace();
            return null;
        }
    }

    public static String encrypt(String content, String password) {
        byte[] data = null;

        try {
            data = content.getBytes("UTF-8");
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        data = encrypt(data, password);
        String result = Base64.encodeToString(data, 2);
        return result;
    }

    public static String encrypt(String content) {
        byte[] data = null;

        try {
            data = content.getBytes("UTF-8");
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        data = encrypt(data, "sgg45747ss223455");
        String result = Base64.encodeToString(data, 2);
        return result;
    }

    public static byte[] decrypt(byte[] content, String password) {
        try {
            SecretKeySpec e = createKey(password);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(2, e);
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception var5) {
            var5.printStackTrace();
            return null;
        }
    }

    public static String decrypt(String content, String password) {
        byte[] data = null;

        try {
            data = Base64.decode(content, 2);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        data = decrypt(data, password);
        if(data == null) {
            return null;
        } else {
            String result = null;

            try {
                result = new String(data, "UTF-8");
            } catch (UnsupportedEncodingException var5) {
                var5.printStackTrace();
            }

            return result;
        }
    }

    public static String decrypt(String content) {
        byte[] data = null;

        try {
            data = Base64.decode(content, 2);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        data = decrypt(data, "sgg45747ss223455");
        if(data == null) {
            return null;
        } else {
            String result = null;

            try {
                result = new String(data, "UTF-8");
            } catch (UnsupportedEncodingException var4) {
                var4.printStackTrace();
            }

            return result;
        }
    }

    public static String byte2hex(byte[] b) {
        StringBuffer sb = new StringBuffer(b.length * 2);
        String tmp = "";

        for(int n = 0; n < b.length; ++n) {
            tmp = Integer.toHexString(b[n] & 255);
            if(tmp.length() == 1) {
                sb.append("0");
            }

            sb.append(tmp);
        }

        return sb.toString().toUpperCase();
    }

    private static byte[] hex2byte(String inputString) {
        if(inputString != null && inputString.length() >= 2) {
            inputString = inputString.toLowerCase();
            int l = inputString.length() / 2;
            byte[] result = new byte[l];

            for(int i = 0; i < l; ++i) {
                String tmp = inputString.substring(2 * i, 2 * i + 2);
                result[i] = (byte)(Integer.parseInt(tmp, 16) & 255);
            }

            return result;
        } else {
            return new byte[0];
        }
    }
}

APP开发AES加密 PHP后台类库

<?php
namespace icecry;
/**
 * aes 加密 解密类库
 * @by singwa
 */
class Aes {

    private $key = null;

    /**
     *
     * @param $key         密钥
     * @return String
     */
    public function __construct() {
        $this->key = 'Coding@xxx.China';
    }

    /**
     * 加密
     * @param String input 加密的字符串
     * @param String key   解密的key
     * @return HexString
     */
    public function encrypt($input = '') {
        $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $input = $this->pkcs5_pad($input, $size);
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, $this->key, $iv);

        $data = mcrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $data = base64_encode($data);

        return $data;
    }

    /**
     * 填充方式 pkcs5
     * @param String text          原始字符串
     * @param String blocksize   加密长度
     * @return String
     */
    private function pkcs5_pad($text, $blocksize) {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    /**
     * 解密
     * @param String input 解密的字符串
     * @param String key   解密的key
     * @return String
     */
    public function decrypt($sStr) {
        $decrypted= mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$this->key,base64_decode($sStr), MCRYPT_MODE_ECB);
        $dec_s = strlen($decrypted);
        $padding = ord($decrypted[$dec_s-1]);
        $decrypted = substr($decrypted, 0, -$padding);

        return $decrypted;
    }

}