Android加密与解密建议
创始人
2024-10-08 12:02:49
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

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...