计算机系统应用教程网站

网站首页 > 技术文章 正文

常用的加解密技术有哪些?利用Java、Python实现AES、DES、3DES

btikc 2025-01-20 11:07:57 技术文章 19 ℃ 0 评论

对称加密:使用单个密钥进行加密和解密的技术。常见的对称加密算法包括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]  # 去掉填充字符

以上就是关于对称加密,使用单个密钥进行加密和解密的技术,下一篇讲继续讲非对称加密:使用一对密钥进行加密和解密的技术。其中,一个密钥用于加密,另一个密钥用于解密。

Tags:

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

欢迎 发表评论:

最近发表
标签列表