网站首页 > 技术文章 正文
对称加密:使用单个密钥进行加密和解密的技术。常见的对称加密算法包括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-01-20 如何给U盘加密?不用装任何工具
- 2025-01-20 软考网络工程师备考-13:网络安全与应用1
- 2025-01-20 等保2.0数据库测评 - Oracle 11g数据库
- 2025-01-20 securecrt中文版
- 2025-01-20 加密算法的分类和简析
- 2025-01-20 VPN协议大揭秘 PPTP与L2TP退场 SSTP与IKEv2成为安全新宠
- 2025-01-20 MiFare RFID的四个系列卡片
- 2025-01-20 C#常用的加密算法:MD5、Base64、SHA1、DES、AES、RSA
- 2025-01-20 AES和DES算法的区别,分别有哪些优缺点
- 2025-01-20 分组密码及其模式(ECB, CBC, CFB, OFB, CTR)详解与应用指南
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- oraclesql优化 (66)
- 类的加载机制 (75)
- feignclient (62)
- 一致性hash算法 (71)
- dockfile (66)
- 锁机制 (57)
- javaresponse (60)
- 查看hive版本 (59)
- phpworkerman (57)
- spark算子 (58)
- vue双向绑定的原理 (68)
- springbootget请求 (58)
- docker网络三种模式 (67)
- spring控制反转 (71)
- data:image/jpeg (69)
- base64 (69)
- java分页 (64)
- kibanadocker (60)
- qabstracttablemodel (62)
- java生成pdf文件 (69)
- deletelater (62)
- com.aspose.words (58)
- android.mk (62)
- qopengl (73)
- epoch_millis (61)
本文暂时没有评论,来添加一个吧(●'◡'●)