JAVA 和ANDROID AES加密

 最近项目 服务端使用的是PHP的openssl的AES加密,Android使用openssl要用到JNI, 比较麻烦。 其实java也有自己AES加密, 效果跟openssl的AES加密一样的 。 需要的第三方jar包 bcprov

java:

package com.qiang.kotlin;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.AlgorithmParameters;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Base64;
import java.util.Random;

public class Demo2 {


    public static void main(String[] args) throws Exception {

        String str =  encrypt("888888","1111400887638100","2222400887638100");
        System.out.println(str);;
    }

    /***
     *
     * @param data
     * @param sessionKey key 必须16位
     * @param iv  偏移值必须16位
     * @return
     * @throws Exception
     */
    public static String encrypt(String data,String sessionKey,String iv) throws Exception {


        byte[] dataByte = data.getBytes();
        byte[] keyByte = sessionKey.getBytes();
        System.out.println("keyByte.length = "+keyByte.length);
        byte[] ivByte = iv.getBytes();
        System.out.println("ivByte.length = "+ivByte.length);


        String encryptedData = null;
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        //指定算法,模式,填充方式,创建一个Cipher
        Cipher cipher =Cipher.getInstance("AES/CBC/PKCS7Padding");

        //生成Key对象
        Key sKeySpec = new SecretKeySpec(keyByte, "AES");

        //把向量初始化到算法参数
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(ivByte));

        //指定用途,密钥,参数 初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, params);

        //指定加密
        byte[] result = cipher.doFinal(dataByte);

        //对结果进行Base64编码,否则会得到一串乱码,不便于后续操作
        Base64.Encoder encoder = Base64.getEncoder();
        encryptedData = encoder.encodeToString(result);


        return encryptedData;
    }
}

android 用kotlin实现  由于Base64 android o 才能 使用android.util.Base64 



import java.security.AlgorithmParameters

import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec

import java.security.Security

import android.util.Base64
import com.dcit.dcsdk.ext.logd


object AesUtils {

    //密钥
    val KEY = "Dcit400887638100"
    //偏移量
    val IV = "Dcit400887638100"


    @Throws(Exception::class)
    fun encrypt(psd: String): String {

        val dataByte = psd.toByteArray()
        val keyByte = KEY.toByteArray()
        val ivByte = IV.toByteArray()

        Security.addProvider(org.bouncycastle.jce.provider.BouncyCastleProvider())
        //指定算法,模式,填充方式,创建一个Cipher
        val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
        //生成Key对象
        val sKeySpec = SecretKeySpec(keyByte, "AES")
        //把向量初始化到算法参数
        val params = AlgorithmParameters.getInstance("AES")
        params.init(IvParameterSpec(ivByte))
        //指定用途,密钥,参数 初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, params)

        //指定加密
        val bytes = cipher.doFinal(dataByte)
        val result = String(Base64.decode(bytes, Base64.DEFAULT))
        logd("encrypt resultStr = $result ")

        return  result
    }
}

 

上一篇:The 2019 Asia Yinchuan First Round Online Programming - A Caesar Cipher | 水题


下一篇:模块