- 2023-03-21 13:16:52
- 8941 热度
- 0 评论
所谓加密,从应用角度就是使用指定的密钥将指定的数据进行加密。
我们不用关心具体算法的实现,只要关心应用本身需求和加密方法的使用即可。
如下是DES加密方法是用的示例,注意使用DES的话密钥长度应该是8的倍数。
package com.util; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; public class DESUtil { private static final String PASSWORD_CRYPT_KEY = "01234567";// 示例密钥 private final static String DES = "DES"; public static void main(String[] args) { System.out.println(encrypt("cuisuqiang", "12345678")); System.out.println(decrypt(encrypt("cuisuqiang", "12345678"),"12345678")); } /** * 加密 * @param src 数据 * @param key 密钥,长度必须是8的倍数 * @return 返回加密后的数据 */ private static byte[] encrypt(byte[] src, byte[] key) throws Exception { SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(DES); cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(src); } /** * 解密 * @param src 数据 * @param key 密钥,长度必须是8的倍数 * @return 返回解密后的原始数据 */ private static byte[] decrypt(byte[] src, byte[] key) throws Exception { SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(DES); cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(src); } /** * 密码加密 */ public final static String encrypt(String data, String key) { try { return byte2hex(encrypt(data.getBytes(), key.getBytes())); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 密码解密 */ public final static String decrypt(String data, String key) { try { return new String(decrypt(hex2byte(data), key.getBytes())); } catch (Exception e) { e.printStackTrace(); } return null; } // 字节码转换成16进制字符串 private static String byte2hex(byte bytes[]) { StringBuffer retString = new StringBuffer(); for (int i = 0; i < bytes.length; ++i) { retString.append(Integer.toHexString(0x0100 + (bytes[i] & 0x00FF)).substring(1).toUpperCase()); } return retString.toString(); } // 将16进制字符串转换成字节码 private static byte[] hex2byte(String hex) { byte[] bts = new byte[hex.length() / 2]; for (int i = 0; i < bts.length; i++) { bts[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2),16); } return bts; } }
以上代码仅供参考
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- JDBC(22)
- IDEA(22)
- mongoDB(22)
- MVC(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- SpringBoot(17)
- Docker(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- ORA(15)
- JPA(15)
- Apache(15)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- Data(12)
- JSON(12)
- OAuth2(12)
- stream(11)
- int(11)
- Myeclipse(11)
- not(10)
- Bug(10)
- maven(9)
- Map(9)
- Hystrix(9)
- ast(9)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- Swagger(8)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- windows(7)
- too(7)
- HTML(7)
- RabbitMQ(6)
- star(6)
- and(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- Freemarker(6)
- read(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- Server(6)
- nginx(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- script(5)
- Syntaxhighlighter(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)
- Servlet(5)