二维码
微世推网

扫一扫关注

当前位置: 首页 » 快闻头条 » 测评资讯 » 正文

常用的加解密技术有哪些?利用Java_Python实

放大字体  缩小字体 发布日期:2023-01-31 18:20:18    作者:郭树沂    浏览次数:166
导读

对称加密:使用单个密钥进行加密和解密得技术。常见得对称加密算法包括AES、DES、3DES等1.下面是使用Java实现AES加密得示例代码:import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import java.u

对称加密:使用单个密钥进行加密和解密得技术。常见得对称加密算法包括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 base64from Crypto.Cipher import AESdef 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 base64from Crypto.Cipher import DESdef 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 base64from Crypto.Cipher import DES3def 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] # 去掉填充字符

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

 
(文/郭树沂)
免责声明
• 
本文仅代表发布者:郭树沂个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,需自行承担相应责任。涉及到版权或其他问题,请及时联系我们删除处理邮件:weilaitui@qq.com。
 

Copyright©2015-2025 粤公网安备 44030702000869号

粤ICP备16078936号

微信

关注
微信

微信二维码

WAP二维码

客服

联系
客服

联系客服:

24在线QQ: 770665880

客服电话: 020-82301567

E_mail邮箱: weilaitui@qq.com

微信公众号: weishitui

韩瑞 小英 张泽

工作时间:

周一至周五: 08:00 - 24:00

反馈

用户
反馈