
Android--AES加密解密
������Cipher������������������������������������������������������������ ���������������������Cipher������������������������������ ������������������������������������������������������ ECB������������������������������������������������������ CBC������������������������������������������������������������������������������������������ CFB��������������������������������������������������������� OFB������������������������������������������������������������������������������
发布日期:2021-05-10 12:49:08
浏览次数:20
分类:精选文章
本文共 6782 字,大约阅读时间需要 22 分钟。
AES������������������
AES���������������������
AES���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������AES���������������������������������������
AES������������������
AES������������������������������������������������������
AES���������������������������16������������������������������������������ECB���������������������16���������������������
AES���������������
������������������������AES������������������������
package com.example.xiaobai.aes;import java.io.UnsupportedEncodingException;import java.security.InvalidAlgorithmParameterException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.security.spec.InvalidKeySpecException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto NoSuchPaddingException;import javax.crypto SecretKey;import javax.crypto SecretKeyFactory;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.PBEKeySpec;import javax.crypto.spec.SecretKeySpec;public class AES { private final String Algorithm = "PBEWITHSHAANDTWOFISH-CBC"; private final int IterationCount = 10000; private final int KeyLength = 128; private char[] HumanPassphrase = { 'P', 'e', 'r', ' ', 'v', 'a', 'l', 'l', 'u', 'm', ' ', 'd', 'u', 'c', 'e', 's', ' ', 'L', 'a', 'b', 'a', 'n', 't' }; private byte[] Salt = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF }; private IvParameterSpec Iv; private SecretKeyFactory KeyFactory; private SecretKey SecretKey; private SecretKeySpec SecretKeySpec; public AES() { try { KeyFactory = SecretKeyFactory.getInstance(Algorithm); SecretKey = KeyFactory.generateSecretKey(HumanPassphrase, Salt, IterationCount, KeyLength); } catch (NoSuchAlgorithmException e) { // ������������������������ Log.e("AES������", "������" + Algorithm + "������������"); } catch (InvalidKeySpecException e) { // ��������������������� Log.e("AES������", "������������" + Algorithm + "������"); } try { byte[] KeyByteArray = SecretKey.getEncoded(); SecretKeySpec = new SecretKeySpec(KeyByteArray, "AES"); Iv = new IvParameterSpec("1234567890123456".getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { Log.e("AES������", "������������������", e); } } public byte[] Encrypt(byte[] Plaintext, String CipherMode) { try { Cipher cipher = Cipher.getInstance(CipherMode); cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec, Iv); return cipher.doFinal(Plaintext); } catch (NoSuchAlgorithmException e) { // ��������������������� Log.e("AES������", "������������" + CipherMode + "���������"); } catch (NoSuchPaddingException e) { Log.e("AES������", "���������������������"); } catch (InvalidKeyException e) { Log.e("AES������", "������������"); } catch (InvalidAlgorithmParameterException e) { Log.e("AES������", "������������������"); } catch (IllegalBlockSizeException e) { Log.e("AES������", "������������������"); } catch (BadPaddingException e) { Log.e("AES������", "���������������"); } return null; } public byte[] Decrypt(byte[] Ciphertext, String CipherMode) { try { Cipher cipher = Cipher.getInstance(CipherMode); cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec, Iv); return cipher.doFinal(Ciphertext); } catch (NoSuchAlgorithmException e) { Log.e("AES������", "������������" + CipherMode + "���������"); } catch (NoSuchPaddingException e) { Log.e("AES������", "���������������������"); } catch (InvalidKeyException e) { Log.e("AES������", "������������"); } catch (InvalidAlgorithmParameterException e) { Log.e("AES������", "������������������"); } catch (IllegalBlockSizeException e) { Log.e("AES������", "������������������"); } catch (BadPaddingException e) { Log.e("AES������", "���������������"); } return null; }}## ������������```javaimport android.os.Bundle;import android.support.v7.app.AppCompatActivity;import java.io.UnsupportedEncodingException;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { String Plaintext = "hello AES!"; byte[] Bytes = Plaintext.getBytes("UTF-8"); AES AESUtil = new AES(); String Encrypted = AESUtil.Encrypt(Bytes, "AES/CBC/PKCS7Padding"); Log.i("AES������������", Encrypted); String Decrypted = AESUtil.Decrypt(AESUtil.Base64Decode(Encrypted), "AES/CBC/PKCS7Padding"); Log.i("AES������������", Decrypted); } catch (NoSuchAlgorithmException e) { Log.e("AES������", "������������", e); } catch (UnsupportedEncodingException e) { Log.e("AES������", "���������������", e); } }}public class Base64 { private static byte[] decode(String s, OutputStream os) throws IOException { int len = s.length(); int[] decoded = new int[4 * (len / 3) + 3]; int index = 0; for (int i = 0; i < len; i++) { switch (s.charAt(i)) { case 'A'-'Z': case 'a'-'z': case '0'-'9': case '+': case '/': case '=': break; default: throw new RuntimeException("������������"); } } return null; }}
���������������������������������������AES���������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年04月06日 21时54分36秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
我用wxPython搭建GUI量化系统之Pandas特性的财务选股工具
2021-05-10
我用wxPython搭建GUI量化系统之财务选股工具添加日历和排序
2021-05-10
搭建量化系统|wxPython布局管理实现多只股票走势对比界面
2021-05-10
2019年达观杯文本智能信息抽取挑战赛 四到十名队伍分享
2021-05-10
一文掌握Python正则表达式
2021-05-10
selenium+python之切换窗口
2021-05-10
Unknown database ‘modd‘
2021-05-10
重载和重写的区别:
2021-05-10
finally,final,finalize() 的区别
2021-05-10
搭建Vue项目步骤
2021-05-10
docker镜像命令
2021-05-10
docker容器命令
2021-05-10
CentOS7使用keepalive实现nginx的高可用
2021-05-10
Centos7.4下mysql5.6开启ssl
2021-05-10
oracle查看字符集后修改oracle服务端和客户端字符集的步骤
2021-05-10
Oracle闪回查询总结
2021-05-10
Linux环境编程--03 文件IO
2021-05-10
使用C语言获取文件夹地址的方法收藏
2021-05-10