java中安全加密/解密

//常见的加密协议
1,base64加密,从现在的加密技术来看,这种加密技术都不好意思说算是加密技术。

2,对称加密(DES):全称:Data Encryption Standard。采用单钥密码系统的加密方法,同一个密码可以同时作用信息的加密和解密。解密和解密都是同一个密码,因此传输这个
密码的过程必须相当安全。优点:计算量小,加密速度快,加密效率高。缺点:很容易被暴力破解

3,3DES3DES,也就是"triple DES",中文名“三重数据加密算法”,对每一个数据模块应用三次DES加密算法。只是增加了DES的长度来避免暴力破解。并不是全新的算法。

4,AESAES,全称:Advanced Encryption Standard,中文高级加密标准。非常的强大,是美国的一种区块加密标准。强安全性,高性能,高效率,易用,灵活等

5,非对称加密。非对称加密方式需要两个密钥来进行加密和解密。分别是公钥和私钥,并且公钥和私钥必须是一对的才能解密,反之,亦然。这是已经是非常安全的加密方式了
5.1,RSA加密。
5.2, DH算法。
5.3, 数字签名证书(更加高级的加密方式)

实例:
实现过程:
生成密钥对->获取公玥->转成PublicKey对象->公玥加密->转成base64码
生成密钥对->获取私玥->PrivateKey对象->私玥解密->转成bytes

//RSA加密类RSAUtil
public class RSAUtil{
//生成密钥对
public static KeyPair getKeyPair() throws Exception {
	KeyPairGenerater keyPairGenerater = new KeyPairGenterater();
	keyPairGenerater.initialize(2048);
	KeyPair keyPair = keyPairGenerater.generateKeyPair();
	return keyPair;
}
//获取公钥(base64编码)
public static String getPublicKey(KeyPair keyPair){
	PublicKey publicKey =keyPair.getPublic();
	byte[] bytes = new publicKey.getEncoded();
	return byte2Base64(bytes);
}
//获取私钥(base64编码)
public static String getPrivateKey(KeyPair keyPair){
	PrivateKey publicKey =keyPair.getPrivate();
	byte[] bytes = new publicKey.getEncoded();
	return byte2Base64(bytes);		
}
//将Base64编码后的公钥转换成PublicKey对象
Public static PublicKey string2PublicKey(String pubstr) throws Exception{
	byte[] keyBytes = base642Byte(pubstr);
	X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
	KeyFactory keyFactory = keyFactory.getInstance("RSA");
	PublicKey publicKey = keyFacTory.generatePublic(keySpec);
	return publicKey;
}
//将Base64编码后的私钥转换成PrivateKey对象
Public static PublicKey string2PublicKey(String pubstr) throws Exception{
	byte[] keyBytes = base642Byte(pubstr);
	PKCS8EncodeKeySpec keySpec = new PKCS8EncodeKeySpec(keyBytes);
	KeyFactory keyFactory = keyFactory.getInstance("RSA");
	PrivateKey privateKey = keyFacTory.generatePrivate(keySpec);
	return privateKey;
}
//公钥加密
public static byte[] publicEncrypt(byte[] content,PublicKey publicKey) throws Exception{
	Ciper ciper = Ciper.getInstance("RSA");
	ciper.init(Ciper.ENCRYPT_MODE,publicKey);
	byte[] bytes =ciper.doFinal(content);
	return bytes;
}
//私钥解密
public static byte[] privateEncrypt(byte[] content,PrivateKey privateKey) throws Exception{
	Ciper ciper = Ciper.getInstance("RSA");
	ciper.init(Ciper.ENCRYPT_MODE,privateKey);
	byte[] bytes =ciper.doFinal(content);
	return bytes;
}
//字节数组转Base64编码	
public static String byte2Base64(byte[] bytes){
	BASE64Encoder encoder=new BASE64Encoder();
	return encoder.encode(bytes);
}
//Base64编码转字节数组
public static String Base642byte(String base64Key) throws IOExcption{
	BASE64Decoder decoder=new BASE64Decoder();
	return decoder.encode(base64Key);
}

}

Java可以使用javax.crypto包的类来进行AES加密解密。具体实现步骤如下: 1. 生成一个AES密钥 ``` KeyGenerator keygen = KeyGenerator.getInstance("AES"); keygen.init(128); SecretKey key = keygen.generateKey(); ``` 2. 创建Cipher对象,并初始化为加密模式或解密模式 ``` Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); ``` 3. 加密/解密数据 ``` byte[] ciphertext = cipher.doFinal(plaintext); byte[] plaintext = cipher.doFinal(ciphertext); ``` 完整代码示例: ``` import javax.crypto.*; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; public class AESUtil { private static final String CHARSET_NAME = "UTF-8"; private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding"; /** * 随机生成AES密钥 * * @param keySize 密钥长度,单位:位 * @return 生成的密钥 * @throws NoSuchAlgorithmException 异常 */ public static byte[] generateKey(int keySize) throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(keySize); SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } /** * AES加密 * * @param plaintext 明文 * @param key 密钥 * @param iv 初始化向量 * @return 密文 * @throws NoSuchPaddingException 异常 * @throws NoSuchAlgorithmException 异常 * @throws InvalidAlgorithmParameterException 异常 * @throws InvalidKeyException 异常 * @throws BadPaddingException 异常 * @throws IllegalBlockSizeException 异常 */ public static byte[] encrypt(byte[] plaintext, byte[] key, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance(AES_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(iv)); return cipher.doFinal(plaintext); } /** * AES解密 * * @param ciphertext 密文 * @param key 密钥 * @param iv 初始化向量 * @return 明文 * @throws NoSuchPaddingException 异常 * @throws NoSuchAlgorithmException 异常 * @throws InvalidAlgorithmParameterException 异常 * @throws InvalidKeyException 异常 * @throws BadPaddingException 异常 * @throws IllegalBlockSizeException 异常 */ public static byte[] decrypt(byte[] ciphertext, byte[] key, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance(AES_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(iv)); return cipher.doFinal(ciphertext); } /** * 生成随机的初始化向量 * * @param ivSize 初始化向量长度,单位:位 * @return 生成的初始化向量 */ public static byte[] generateIv(int ivSize) { byte[] iv = new byte[ivSize / 8]; new SecureRandom().nextBytes(iv); return iv; } /** * 将字节数组转换为十六进制字符串 * * @param bytes 字节数组 * @return 十六进制字符串 */ public static String toHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(bytes.length * 2); for (byte b : bytes) { sb.append(String.format("%02x", b & 0xff)); } return sb.toString(); } /** * 将十六进制字符串转换为字节数组 * * @param hexString 十六进制字符串 * @return 字节数组 */ public static byte[] toByteArray(String hexString) { int len = hexString.length() / 2; byte[] bytes = new byte[len]; for (int i = 0; i < len; i++) { int index = i * 2; bytes[i] = (byte) Integer.parseInt(hexString.substring(index, index + 2), 16); } return bytes; } public static void main(String[] args) throws Exception { // 随机生成16字节的AES密钥和16字节的初始化向量 byte[] key = generateKey(128); byte[] iv = generateIv(128); String plaintextStr = "hello, AES!"; byte[] plaintext = plaintextStr.getBytes(CHARSET_NAME); // AES加密 byte[] ciphertext = encrypt(plaintext, key, iv); String ciphertextHex = toHexString(ciphertext); System.out.println("密文:" + ciphertextHex); // AES解密 byte[] ciphertextBytes = toByteArray(ciphertextHex); byte[] decrypted = decrypt(ciphertextBytes, key, iv); String decryptedStr = new String(decrypted, CHARSET_NAME); System.out.println("明文:" + decryptedStr); } } 相关问题:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值