Andrews AES encryption

One: What is AES encryption
AES Advanced Encryption Standard (English: Advanced Encryption Standard, abbreviation: AES), also known as Rijndael cipher In cryptography, a block encryption standard adopted by the US federal government.
This standard is used to replace the original DES, it has been widely analyzed and multi- used around the world.
AES is an iterative cryptographic symmetric key packet, AES algorithm encryption strength, high efficiency, easy to use, the actual development is recommended to select the AES algorithm.
 
Two: AES encryption is achieved Andrews
Andrews the AES encryption scheme is as follows:
Andrews which offers two API: SecretKeySpec and Cipher
Plaintext and ciphertext, provide encryption and decryption functions by Ciper
 
III: Application in the project below to see
/ ** 
 * ON 2019/9/23/023 the Created by leilei TUA. 
 * / 
Public class AesUtils { 
    public static String VIPARA Final = "1234567890123456"; 
    public static String Final BM = "UTF-. 8"; 
    public static String password Final = "1234567890123456"; // password secret key generated keyword 


    / ** 
     * byte array converted to uppercase hexadecimal character string 
     * 
     * @param B 
     * @return 
     * / 
    Private static string byte2HexStr (byte [] B) { 
        the StringBuilder new new SB = the StringBuilder (); 
        for (int I = 0; I <to b.length; I ++) { 
            String Integer.toHexString S = (B [I] & 0xFF); 
            IF (s.length () ==. 1) { 
                sb.append ( "0"); 
            }

            sb.append(s.toUpperCase());
        }

        return sb.toString();
    }

    /**
     * 16进制字符串转字节数组
     *
     * @param s
     * @return
     */
    private static byte[] str2ByteArray(String s) {
        int byteArrayLength = s.length() / 2;
        byte[] b = new byte[byteArrayLength];
        for (int i = 0; i < byteArrayLength; i++) {
            byte b0 = (byte) Integer.valueOf(s.substring(i * 2, i * 2 + 2), 16)
                    .intValue();
            b[i] = b0;
        }

        return b;
    }


    /**
     * AES 加密
     *
     * @param content  明文
     * @param
     * @return
     */

    public static String aesEncrypt(String content) {
        try {
            IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
            SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            byte[] encryptedData = cipher.doFinal(content.getBytes(bm));
            // return new String(encryptedData,bm);
            return Base64.encode(encryptedData);
//          return byte2HexStr(encryptedData);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * AES 解密
     *
     * @param content  密文
     * @param password 生成秘钥的关键字
     * @return
     */

    public static String aesDecrypt(String content, String password) {
        try {
            byte[] byteMi = Base64.decode(content);
//          byte[] byteMi=  str2ByteArray(content);
            IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
            SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
            byte[] decryptedData = cipher.doFinal(byteMi);
            return new String(decryptedData, "utf-8");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

}

 Note: In the ios and after the stage

VIPARA (offset)
password (key) had to be multi-terminal unified standard decryption
public static final String VIPARA = "" ; // attention to the need 16 characters 
public static final String password = "" ; // password key is generated secret key. Note requires 16 characters

 Application project is about json transmission (String string as long as you can)

Map<String, String> map = new HashMap<>();
map.put("order_id", order_id);
map.put("input_password", payWord);
String aesStr = AesUtils.aesEncrypt(JsonUtil.getInstance().toJson(map));

  Leilei tua

 

Guess you like

Origin www.cnblogs.com/widgetbox/p/11611201.html