欢迎访问宙启技术站
智能推送

Java函数如何实现基本的加密和解密功能?

发布时间:2023-06-17 05:55:12

Java中常见的加密算法有对称加密算法和非对称加密算法。对称加密算法指的是加密和解密使用同一把密钥,双方在通信前都需要知道密钥,相对容易实现,加密解密速度快,但密钥需保密,因此更适合私有网络或者局域网等网络环境;非对称加密算法是指加解密使用不同的密钥,公钥放在公开的地方供大家使用,私钥只有拥有者才能使用,更适合互联网等公共网络环境。

以下为实现基本的加密和解密功能的方法:

1. 对称加密算法实现方法:

首先需要引入Java的加密库,如下:

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

、然后定义加解密方法:

public static byte[] encrypt(byte[] content, String password) {

 try {

     KeyGenerator kgen = KeyGenerator.getInstance("AES");

     kgen.init(128, new SecureRandom(password.getBytes()));

     SecretKey secretKey = kgen.generateKey();

     byte[] enCodeFormat = secretKey.getEncoded();

     SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");

     Cipher cipher = Cipher.getInstance("AES");// 创建密码器

     byte[] byteContent = content;

     cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化

     byte[] result = cipher.doFinal(byteContent);

     return result; // 加密

 } catch (NoSuchAlgorithmException e) {

     return null;

 } catch (NoSuchPaddingException e) {

     return null;

 } catch (InvalidKeyException e) {

     return null;

 } catch (IllegalBlockSizeException e) {

     return null;

 } catch (BadPaddingException e) {

     return null;

 }

}

public static byte[] decrypt(byte[] content, String password) {

 try {

     KeyGenerator kgen = KeyGenerator.getInstance("AES");

     kgen.init(128, new SecureRandom(password.getBytes()));

     SecretKey secretKey = kgen.generateKey();

     byte[] enCodeFormat = secretKey.getEncoded();

     SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");

     Cipher cipher = Cipher.getInstance("AES");// 创建密码器

     cipher.init(Cipher.DECRYPT_MODE, key);// 初始化

     byte[] result = cipher.doFinal(content);

     return result; // 解密

 } catch (NoSuchAlgorithmException e) {

     return null;

 } catch (NoSuchPaddingException e) {

     return null;

 } catch (InvalidKeyException e) {

     return null;

 } catch (IllegalBlockSizeException e) {

     return null;

 } catch (BadPaddingException e) {

     return null;

 }

}

其中,encrypt和decrypt方法用的均是AES对称加密算法,password为密钥字符串,加密后返回result为加密结果,解密后返回result为明文。

2. 非对称加密算法实现方法:

非对称加密算法常用的是RSA算法,首先需要引入Java的加密库,如下:

import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import javax.crypto.Cipher;

定义加解密方法如下:

public static byte[] encrypt(byte[] content, PublicKey publicKey) { 

 try { 

     Cipher cipher = Cipher.getInstance("RSA");

     cipher.init(Cipher.ENCRYPT_MODE, publicKey);

     byte[] result = cipher.doFinal(content);

     return result; // 加密

 } catch (Exception e) {

     return null;

 }

}

public static byte[] decrypt(byte[] content, PrivateKey privateKey) { 

 try {

     Cipher cipher = Cipher.getInstance("RSA");

     cipher.init(Cipher.DECRYPT_MODE, privateKey);

     byte[] result = cipher.doFinal(content);

     return result; // 解密

 } catch (Exception e) {

     return null;

 }

}

其中,encrypt和decrypt方法用的均是RSA非对称加密算法,publicKey和privateKey分别代表公钥和私钥,加密后返回result为加密结果,解密后返回result为明文。

以上为实现基本的加密和解密功能的方法,但需要注意的是,这些加密和解密算法只能提供基本保护,对于高级的安全保护,需要使用更加高级的加密算法和技术。