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

相关内容

热门资讯

总算了解!wepoker透视脚... 您好,wepoker透视脚本免费下载pc这款游戏可以开挂的,确实是有挂的,需要了解加微【136704...
透视模拟器!sohoo竞技联盟... 透视模拟器!sohoo竞技联盟辅助器(透视)都是存在有挂(详细辅助解说技巧)进入游戏-大厅左侧-新手...
我来教大家!pokemmo脚本... 这是一款非常优秀的wepoker透视底牌脚本 ia辅助检测软件,能够让你了解到wepoker透视底牌...
透视真的!pokemmo脚本最... 透视真的!pokemmo脚本最新版(透视)一贯有挂(详细辅助力荐教程)辅助器中分为三种模型:软件透明...
揭秘真相!红龙poker辅助器... 揭秘真相!红龙poker辅助器免费观看,wepokerplus辅助,详细教程(有挂方法)1、点击下载...
透视有挂!pokemmo辅助官... 透视有挂!pokemmo辅助官网(透视)切实是真的有挂(详细辅助科技教程)1、该软件可以轻松地帮助玩...
总算了解!aapoker发牌逻... 总算了解!aapoker发牌逻辑,拱趴大菠萝万能挂,力荐教程(有挂技巧)这是由厦门游乐互动科技有限公...
透视挂透视!德扑圈透视挂(透视... 透视挂透视!德扑圈透视挂(透视)确实存在有挂(详细辅助德州教程)1、玩家可以在软件透明挂俱乐部对游戏...
指导大家!wepoker透视辅... 指导大家!wepoker透视辅助下载,wepoker游戏的安装教程,靠谱教程(有挂软件);实战中需综...
透视科技!哈糖大菠萝破解器(透... 透视科技!哈糖大菠萝破解器(透视)一贯有挂(详细辅助攻略教程);1、系统规律教程、辅助透视等服务,为...