AES加密解密

一、demo

package com.example.logindemo;

import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;

public class User {
    private String mId;
    private String mPwd;
    private static final String masterPassword = "FORYOU"; // AES加密算法的种子
    private static final String JSON_ID = "user_id";
    private static final String JSON_PWD = "user_pwd";
    private static final String TAG = "User";

    public User(String id, String pwd) {
        this.mId = id;
        this.mPwd = pwd;
    }

    public User(JSONObject json) throws Exception {
        if (json.has(JSON_ID)) {
            String id = json.getString(JSON_ID);
            String pwd = json.getString(JSON_PWD);
            // 解密后存放
            mId = AESUtils.decrypt(masterPassword, id);
            mPwd = AESUtils.decrypt(masterPassword, pwd);
        }
    }

    public JSONObject toJSON() throws Exception {
        // 使用AES加密算法加密后保存
        String id = AESUtils.encrypt(masterPassword, mId);
        String pwd = AESUtils.encrypt(masterPassword, mPwd);
        Log.i(TAG, "加密后:" + id + "  " + pwd);
        JSONObject json = new JSONObject();
        try {
            json.put(JSON_ID, id);
            json.put(JSON_PWD, pwd);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }

    public String getId() {
        return mId;
    }

    public String getPwd() {
        return mPwd;
    }
}

二、加密与解密

加密:

public static String encrypt(String text) throws Exception {  
        // 私钥 AES固定格式为128/192/256bits.即:16/24/32bytes。DES固定格式为128bits,即8bytes。  
        String key = "aaaaaaaaaaaaaaaa";  
        // 初始化向量参数,AES 为16bytes. DES 为8bytes  
        String iv ="bbbbbbbbbbbbbbbb";   

        // 两个参数,第一个为私钥字节数组, 第二个为加密方式AES或者DES  
        Key keySpec = new SecretKeySpec(key.getBytes(), "AES");   

        IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());  
        // 实例化加密类,参数为加密方式,要写全  
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");  
        // 初始化,此方法可以采用三种方式,按服务器要求来添加。(1)无第三个参数(2)第三个参数为SecureRandom  
        //(3)采用此代码中的IVParameterSpec  
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);  

                // random = new SecureRandom();中random对象,随机数。(AES不可采用这种方法)  
        // cipher.init(Cipher.ENCRYPT_MODE, keySpec);  
        // SecureRandom random = new SecureRandom();  
        // cipher.init(Cipher.ENCRYPT_MODE, keySpec, random);  
        byte[] bytes = cipher.doFinal(text.getBytes());// 加密操作,返回加密后的字节数组,然后需要编码。主要编解码方式有Base64, HEX, UUE,  
        // 7bit等等。此处看服务器需要什么编码方式  
        String result = Base64.encodeToString(bytes, Base64.DEFAULT);  
        return result;  

    }

解密:

public static String decrypt(String text) throws Exception {  

        String keySpec = "aaaaaaaaaaaaaaaa";  
        String iv = "bbbbbbbbbbbbbbbb";  
        byte[] textBytes = Base64.decode(text.getBytes(), Base64.DEFAULT);  

        IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());  

        Key key = new SecretKeySpec(keySpec.getBytes(), "AES");  

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");  

        cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); // 与加密时不同MODE:Cipher.DECRYPT_MODE  

        String result = cipher.doFinal(textBytes);  

        return result;  

}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaAES加密解密是一种常用的对称加密算法,它可以将明文加密成密文,并且可以使用相同的密钥进行解密。在Java中,常用的JavaAES加密解密实现方式有两种:一种是基于Java自带的Cipher类实现的,另一种是基于第三方库实现的。 如果使用Java自带的Cipher类,可以按照以下步骤实现JavaAES加密解密: 1. 生成密钥,可以使用KeyGenerator类生成密钥。 2. 创建Cipher对象,设置加密模式和填充方式。 3. 初始化Cipher对象,设置加密或解密模式,以及密钥。 4. 调用Cipher的doFinal方法进行加密或解密操作。 以下是JavaAES加密解密的代码示例: ```java import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import java.nio.charset.StandardCharsets; import java.security.NoSuchAlgorithmException; public class JavaAESTest { public static void main(String[] args) throws Exception { String plaintext = "Hello, World!"; //明文 String password = "123456"; //密钥 //生成密钥 SecretKey secretKey = getSecretKey(password); //加密 byte[] ciphertext = encrypt(plaintext.getBytes(StandardCharsets.UTF_8), secretKey); System.out.println("加密后:" + new String(ciphertext, StandardCharsets.UTF_8)); //解密 byte[] decrypted = decrypt(ciphertext, secretKey); System.out.println("解密后:" + new String(decrypted, StandardCharsets.UTF_8)); } private static SecretKey getSecretKey(String password) throws NoSuchAlgorithmException { //生成AES算法的KeyGenerator对象 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); //指定密钥长度为128位 //生成一个128位的随机安全密钥 return keyGenerator.generateKey(); } private static byte[] encrypt(byte[] plaintext, SecretKey secretKey) throws Exception { //创建Cipher对象,设置加密模式和填充方式 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); //初始化Cipher对象 //加密并返回结果 return cipher.doFinal(plaintext); } private static byte[] decrypt(byte[] ciphertext, SecretKey secretKey) throws Exception { //创建Cipher对象,设置解密模式和填充方式 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey); //初始化Cipher对象 //解密并返回结果 return cipher.doFinal(ciphertext); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值