网站首页 > java教程 正文
对称加密:使用单个密钥进行加密和解密的技术。常见的对称加密算法包括AES、DES、3DES等
1.下面是使用Java实现AES加密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESEncrypt {
    // 加密
    public static byte[] encrypt(byte[] content, byte[] keyBytes) throws Exception {
        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(content);
    }
    // 解密
    public static byte[] decrypt(byte[] content, byte[] keyBytes) throws Exception {
        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(content);
    }
    public static void main(String[] args) throws Exception {
        //加密
        byte[] key = "1234567890123456".getBytes();  // 密钥需要为16位字节
        String content = "Hello, World!";  // 加密内容
        byte[] resultEn=AESEncrypt.encrypt(content.getBytes(),key);//加密
        String byte2Base64 = Base64.getEncoder().encodeToString(resultEn);  // 转为base64格式
        System.out.println(byte2Base64); //加密后的base64字符串
        //注意跟加密必须使用同一key
        //解密
        byte[] base642Byte=Base64.getDecoder().decode(byte2Base64); //将base64加密后的结果base64解码
        byte[] resultDe=AESEncrypt.decrypt(base642Byte,key); //解密
        System.out.println(new String(resultDe));
    }
}2.下面是使用Java实现DES加密的示例代码:
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.util.Base64;
public class DESEncrypt {
    // 加密
    public static byte[] encrypt(byte[] content, byte[] keyBytes) throws Exception {
        DESKeySpec key = new DESKeySpec(keyBytes);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generateSecret(key));
        return cipher.doFinal(content);
    }
    // 解密
    public static byte[] decrypt(byte[] content, byte[] keyBytes) throws Exception {
        DESKeySpec key = new DESKeySpec(keyBytes);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, keyFactory.generateSecret(key));
        return cipher.doFinal(content);
    }
    public static void main(String[] args) throws Exception {
        //加密
        byte[] key = "1234567890123456".getBytes();  // 密钥需要为16位字节
        String content = "Hello, World!";  // 加密内容
        byte[] resultEn=DESEncrypt.encrypt(content.getBytes(),key);//加密
        String byte2Base64 = Base64.getEncoder().encodeToString(resultEn);  // 转为base64格式
        System.out.println(byte2Base64); //加密后的base64字符串
        //注意跟加密必须使用同一key
        //解密
        byte[] base642Byte=Base64.getDecoder().decode(byte2Base64); //将base64加密后的结果base64解码
        byte[] resultDe=DESEncrypt.decrypt(base642Byte,key); //解密
        System.out.println(new String(resultDe));
    }
}
3.下面是使用Java实现3DES加密的示例代码:
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.util.Base64;
public class TripleDESEncrypt {
    // 加密
    public static byte[] encrypt(byte[] content, byte[] keyBytes) throws Exception {
        DESedeKeySpec key = new DESedeKeySpec(keyBytes);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generateSecret(key));
        return cipher.doFinal(content);
    }
    // 解密
    public static byte[] decrypt(byte[] content, byte[] keyBytes) throws Exception {
        DESedeKeySpec key = new DESedeKeySpec(keyBytes);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.DECRYPT_MODE, keyFactory.generateSecret(key));
        return cipher.doFinal(content);
    }
    public static void main(String[] args) throws Exception {
        //加密
        byte[] key = "123456789012345678901234".getBytes();  // 密钥需要为24位字节
        String content = "Hello, World!";  // 加密内容
        byte[] resultEn=TripleDESEncrypt.encrypt(content.getBytes(),key);//加密
        String byte2Base64 = Base64.getEncoder().encodeToString(resultEn);  // 转为base64格式
        System.out.println(byte2Base64); //加密后的base64字符串
        //注意跟加密必须使用同一key
        //解密
        byte[] base642Byte=Base64.getDecoder().decode(byte2Base64); //将base64加密后的结果base64解码
        byte[] resultDe=TripleDESEncrypt.decrypt(base642Byte,key); //解密
        System.out.println(new String(resultDe));
    }
}
使用Python语言实现AES、DES、3DES加密算法:
4.下面是使用Python实现AES加密的示例代码:
import base64
from Crypto.Cipher import AES
def encrypt(key, content):
    key_bytes = key.encode()  # 密钥需要为16、24或32位字节
    cipher = AES.new(key_bytes, AES.MODE_ECB)  # 创建AES加密器
    content_padded = content + (16 - len(content) % 16) * chr(16 - len(content) % 16)  # PKCS#7填充
    cipher_text = cipher.encrypt(content_padded.encode())  # 加密
    return base64.b64encode(cipher_text).decode()  # 转为base64格式并解码为字符串
def decrypt(key, content):
    key_bytes = key.encode()  # 密钥需要为16、24或32位字节
    cipher = AES.new(key_bytes, AES.MODE_ECB)  # 创建AES加密器
    cipher_text = base64.b64decode(content.encode())  # 解码为字节数组并解码为base64格式
    content_padded = cipher.decrypt(cipher_text).decode()  # 解密
    padding_length = ord(content_padded[-1])  # 获取填充字符的数量
    return content_padded[:-padding_length]  # 去掉填充字符
下面是使用Python实现DES加密的示例代码:
import base64
from Crypto.Cipher import DES
def encrypt(key, content):
    key_bytes = key.encode()  # 密钥需要为8位字节
    cipher = DES.new(key_bytes, DES.MODE_ECB)  # 创建DES加密器
    content_padded = content + (8 - len(content) % 8) * chr(8 - len(content) % 8)  # PKCS#5填充
    cipher_text = cipher.encrypt(content_padded.encode())  # 加密
    return base64.b64encode(cipher_text).decode()  # 转为base64格式并解码为字符串
def decrypt(key, content):
    key_bytes = key.encode()  # 密钥需要为8位字节
    cipher = DES.new(key_bytes, DES.MODE_ECB)  # 创建DES加密器
    cipher_text = base64.b64decode(content.encode())  # 解码为字节数组并解码为base64格式
    content_padded = cipher.decrypt(cipher_text).decode()  # 解密
    padding_length = ord(content_padded[-1])  # 获取填充字符的数量
    return content_padded[:-padding_length]  # 去掉填充字符
下面是使用Python实现3DES加密的示例代码:
import base64
from Crypto.Cipher import DES3
def encrypt(key, content):
    key_bytes = key.encode()  # 密钥需要为24位字节
    cipher = DES3.new(key_bytes, DES3.MODE_ECB)  # 创建3DES加密器
    content_padded = content + (8 - len(content) % 8) * chr(8 - len(content) % 8)  # PKCS#5填充
    cipher_text = cipher.encrypt(content_padded.encode())  # 加密
    return base64.b64encode(cipher_text).decode()  # 转为base64格式并解码为字符串
def decrypt(key, content):
    key_bytes = key.encode()  # 密钥需要为24位字节
    cipher = DES3.new(key_bytes, DES3.MODE_ECB)  # 创建3DES加密器
    cipher_text = base64.b64decode(content.encode())  # 解码为字节数组并解码为base64格式
    content_padded = cipher.decrypt(cipher_text).decode()  # 解密
    padding_length = ord(content_padded[-1])  # 获取填充字符的数量
    return content_padded[:-padding_length]  # 去掉填充字符
以上就是关于对称加密,使用单个密钥进行加密和解密的技术,下一篇讲继续讲非对称加密:使用一对密钥进行加密和解密的技术。其中,一个密钥用于加密,另一个密钥用于解密。
猜你喜欢
- 2025-05-24 Synchronized的实现原理详解(看这篇就够了)
 - 2025-05-24 Java设计模式:解锁实际开发中的“密码锁”
 - 2025-05-24 京东大佬问我,加密后的数据如何进行模糊查询?我的回答满分
 - 2025-05-24 产品经理需要了解的接口知识
 - 2025-05-24 【密码学】为什么不推荐在对称加密中使用CBC工作模式
 - 2025-05-24 在 Spring Boot3 开发中,是不是也为选择合适的加密算法而头疼?
 - 2025-05-24 ssl 加密证书
 - 2025-05-24 Nacos 中的配置文件如何实现加密传输
 - 2025-05-24 加解密的艺术
 - 2025-05-24 实现前端传递参数进行加密,Java后端接收数据解密
 
欢迎 你 发表评论:
- 最近发表
 
- 标签列表
 - 
- java反编译工具 (77)
 - java反射 (57)
 - java接口 (61)
 - java随机数 (63)
 - java7下载 (59)
 - java数据结构 (61)
 - java 三目运算符 (65)
 - java对象转map (63)
 - Java继承 (69)
 - java字符串替换 (60)
 - 快速排序java (59)
 - java并发编程 (58)
 - java api文档 (60)
 - centos安装java (57)
 - java调用webservice接口 (61)
 - java深拷贝 (61)
 - 工厂模式java (59)
 - java代理模式 (59)
 - java.lang (57)
 - java连接mysql数据库 (67)
 - java重载 (68)
 - java 循环语句 (66)
 - java反序列化 (58)
 - java时间函数 (60)
 - java是值传递还是引用传递 (62)
 
 

本文暂时没有评论,来添加一个吧(●'◡'●)