Android加密与解密建议
创始人
2024-10-08 12:02:34
0

在Android中进行加密和解密操作时,建议使用安全的算法和方法,不要使用过于简单的算法或自行编写的加解密算法,以免被攻击者利用漏洞进行攻击。以下是一些常用的加密和解密方法:

1.对称加密算法:使用同一密钥进行加解密操作的算法,例如AES算法。

示例代码:

//加密操作 public static String encrypt(String content, String key) throws Exception { byte[] byteContent = content.getBytes("UTF-8"); byte[] enCodeFormat = key.getBytes(); SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] result = cipher.doFinal(byteContent); return Base64.encodeToString(result, Base64.DEFAULT); }

//解密操作 public static String decrypt(String content, String key) throws Exception { byte[] byteContent = Base64.decode(content, Base64.DEFAULT); byte[] enCodeFormat = key.getBytes(); SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] result = cipher.doFinal(byteContent); return new String(result, "UTF-8"); }

2.非对称加密算法:使用公钥加密数据,使用私钥解密数据的算法,例如RSA算法。

示例代码:

//生成RSA密钥对 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); KeyPair keyPair = keyPairGenerator.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate();

//加密操作 public static String encryptRSA(String content, PublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] result = cipher.doFinal(content.getBytes()); return Base64.encodeToString(result, Base64.DEFAULT); }

//解密操作 public static String decryptRSA(String content, PrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] result = cipher.doFinal(Base64.decode(content, Base64.DEFAULT)); return new String(result); }

3.Hash算法:将任意长度的输入映射为固定长度的输出,常用的算法有MD5和SHA-1。

示例代码:

//MD5哈希 public static String md5(String content) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] result = md.digest(content.getBytes()); return byteArrayToHexString(result); }

//SHA-1哈希 public static String sha1(String content) throws Exception { MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] result = md.digest(content.getBytes()); return byteArrayToHexString(result); }

//将字节数组转为十六进制字符串 private static String byteArrayToHexString(byte[] b) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < b.length; i++) { String hex = Integer.toHexString(b[i] & 0xFF); if (hex.length() == 1) { sb.append("0"); } sb

相关内容

热门资讯

2026外挂!雀友会广东潮汕麻... 2026外挂!雀友会广东潮汕麻将辅助软件,芒果辅助器(好像是有挂)1、用户打开应用后不用登录就可以直...
2026外挂!哈糖大菠萝辅助,... 2026外挂!哈糖大菠萝辅助,奇迹手游辅助脚本平台免费(一直真的有挂)1、奇迹手游辅助脚本平台免费脚...
2026外挂!辅助定制交易平台... 2026外挂!辅助定制交易平台,情怀破解(本来有挂)1、起透看视 辅助定制交易平台辅助软件价格2、随...
2026外挂!九酷众游软件,5... 2026外挂!九酷众游软件,510k中至余干辅助(原来真的是有挂)1、玩家可以在510k中至余干辅助...
2026外挂!极速官方游戏暗堡... 2026外挂!极速官方游戏暗堡辅助器,楚天游辅助(其实存在有挂)1、上手简单,内置详细流程视频教学,...
2026外挂!免费游戏辅助软件... 2026外挂!免费游戏辅助软件,如何设置钱塘十三水辅助器(切实是真的有挂)1、2026外挂!免费游戏...
2026外挂!雀姬辅助脚本,微... 2026外挂!雀姬辅助脚本,微乐游戏辅助脚本安装(切实有挂)2026外挂!雀姬辅助脚本,微乐游戏辅助...
2026外挂!飞驰娱乐软件科技... 2026外挂!飞驰娱乐软件科技,科乐填大坑辅助器(切实真的有挂)1、科乐填大坑辅助器免费辅助多个强度...
2026外挂!皮皮胡子辅助,天... 2026外挂!皮皮胡子辅助,天天贵阳破解版(原来是真的有挂)天天贵阳破解版是不是有人用挂微扑克wpk...
2026外挂!科技实测欢乐辅助... 2026外挂!科技实测欢乐辅助,丽水都莱辅助软件(都是是有挂)小薇(辅助器软件下载)致您一封信;亲爱...