Android中的AES加密算法解析

概述:

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。AES加密是可逆的,加密解密需要同一把钥匙。

使用,看代码:

public class AESActivity extends AppCompatActivity {

    private EditText editText ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.esk_activity_layout);
        editText = (EditText) findViewById( R.id.asx_action_et) ;
    }

    /**
     *生产成钥匙
     * @param view
     */
    public void yaoshi(View view) throws Exception {
        //通过AES的key生成器生成一个密钥(加密解密都是通过这个钥匙)
        SecretKey sk = KeyGenerator.getInstance("AES").generateKey() ;
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) , "key.sys") ;
        FileOutputStream fos = new FileOutputStream( file ) ;
        ObjectOutputStream obo = new ObjectOutputStream( fos ) ;
        obo.writeObject( sk ) ;
        obo.flush() ;
    }

    /**
     * 加密
     * @param view
     */
    public void encode(View view) throws Exception {
        //得到加密的key对象
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"key.sys");
        SecretKey key = (SecretKey) new ObjectInputStream(new FileInputStream(file)).readObject();
        //获取加密工具
        Cipher cipher = Cipher.getInstance("AES");
        //初始化加密工具
        cipher.init(Cipher.ENCRYPT_MODE,key);
        //放入我们要加密的内容 并加密
        byte[] bytes = cipher.doFinal(editText.getText().toString().getBytes());
        //得到的字节在进行Base64换算
        byte[] base = Base64.encode(bytes,Base64.DEFAULT);
        String text = new String(base);
        editText.setText(text);
    }

    /**
     * 解密
     * @param view
     */
    public void deCode(View view) throws Exception {
        //获取AESkey
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"key.sys");
        SecretKey key = (SecretKey) new ObjectInputStream(new FileInputStream(file)).readObject();
        //获取加密工具
        Cipher cipher = Cipher.getInstance("AES");
        //初始化加密工具
        cipher.init(Cipher.DECRYPT_MODE,key);
        //还原文本框的base64文本为密文
        byte[] bytes = Base64.decode(editText.getText().toString(),Base64.DEFAULT);
        //把密文解密为明文
        byte[] bytes1  =cipher.doFinal(bytes);
        //把明文设置到文本框中
        editText.setText(new String(bytes1));
    }
}
xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/asx_action_et"
        android:layout_width="match_parent"
        android:layout_height="100dp"/>

    <Button
        android:onClick="yaoshi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="生成钥匙"/>

    <Button
        android:onClick="encode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ESK加密"/>

    <Button
        android:onClick="deCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ESKj解密"/>

</LinearLayout>

AES 加密算法是可逆的,加密解密需要同一把秘钥。

首先需要把秘钥生成,使用秘钥生成器:

SecretKey sk = KeyGenerator.getInstance("AES").generateKey() ;

然后获取加密解密工具,获取后使用秘钥初始化加密工具:

//获取加密工具
Cipher cipher = Cipher.getInstance("AES");
//初始化加密工具
cipher.init(Cipher.ENCRYPT_MODE,key);
然后使用加密工具对想要加密的内容进行加密:

//放入我们要加密的内容 并加密
byte[] bytes = cipher.doFinal(editText.getText().toString().getBytes());

解密也是一样,加密解密使用同一把秘钥,即使用加密时的sk ,然后过程与加密一样

然后获取加密解密工具,获取后使用秘钥初始化加密工具:

//获取加密工具
Cipher cipher = Cipher.getInstance("AES");
//初始化加密工具
cipher.init(Cipher.ENCRYPT_MODE,key);
然后使用加密工具对想要加密的内容进行加密:

//放入我们要加密的内容 并加密
byte[] bytes = cipher.doFinal(editText.getText().toString().getBytes());



猜你喜欢

转载自blog.csdn.net/xiaol206/article/details/71967797
今日推荐