3DES加密解密

/*字符串 DESede(3DES) 加密*/
import java.io.IOException;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DES3 {

	private static final String Algorithm = "DESede"; // 定义 加密算法,可用

	// DES,DESede,Blowfish
	// keybyte为加密密钥,长度为24字节
	// src为被加密的数据缓冲区(源)
	public static byte[] encryptMode(byte[] keybyte, byte[] src) {
		try {
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
			// 加密
			Cipher c1 = Cipher.getInstance(Algorithm);
			c1.init(Cipher.ENCRYPT_MODE, deskey);
			return c1.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

	// keybyte为加密密钥,长度为24字节 每8个是一组
	// src为加密后的缓冲区
	public static byte[] decryptMode(byte[] keybyte, byte[] src) {
		try {
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
			// 解密
			Cipher c1 = Cipher.getInstance(Algorithm);
			c1.init(Cipher.DECRYPT_MODE, deskey);
			return c1.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

	// 转换成十六进制字符串
	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs = hs + "0" + stmp;
			} else {
				hs = hs + stmp;
			}
			if (n < b.length - 1) {
				hs = hs + ":";
			}
		}
		return hs.toUpperCase();
	}

	public static void main(String[] args) throws IOException {
		final byte[] keyBytes = "ceshi123456rewqdsf434368".getBytes(); // 24字节的密钥
		String szSrc = "abcd_123";
		System.out.println("加密前的字符串:" + szSrc);
		byte[] encoded = encryptMode(keyBytes, szSrc.getBytes());
		String encode = new BASE64Encoder().encode(encoded);
		System.out.println("加密后的字符串:" + new String(encode));
		byte[] decodeBuffer = new BASE64Decoder().decodeBuffer("3YV86nsmJEVYa6dW8mIEYA==");
		byte[] srcBytes = decryptMode(keyBytes, decodeBuffer);
		System.out.println("解密后的字符串:" + (new String(srcBytes)));
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python实现3DES加密解密的代码示例如下: ```python import pyDes # 定义密钥,长度必须为8个字符(64位) key = b'12345678' # 定义初始化向量,长度为8个字符(64位) iv = b'abcdefgh' # 创建加密器 k = pyDes.triple_des(key, pyDes.CBC, iv, pad=None, padmode=pyDes.PAD_PKCS5) # 要加密的明文 data = b'Hello World' # 加密 cipher_text = k.encrypt(data) # 解密 plain_text = k.decrypt(cipher_text) print('加密前的明文:', data) print('加密后的密文:', cipher_text) print('解密后的明文:', plain_text) ``` 3DES的原理是:对明文进行三次加密,每次加密的密钥不同。加密时,首先使用第一个密钥对明文进行加密,然后使用第二个密钥对加密后的数据进行解密,最后再使用第三个密钥对解密后的数据进行加密。解密时,则按照相反的方式进行操作。 另外,cryptography库也提供了3DES的实现方式,代码示例如下: ```python from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import base64 key = b'8C7BD6A28C7BD6A28C7BD6A28C7BD6A28C7BD6A28C7BD6A2' # 48位 key = base64.b16decode(key) iv = b'3BF23BF23BF23BF2' # 16位 iv = base64.b16decode(iv) # 需要加密的内容 message = "a secret message" cipher = Cipher(algorithms.TripleDES(key), modes.CBC(iv)) # 加密 encryptor = cipher.encryptor() cipher_bytes = encryptor.update(message.encode()) encryptor.finalize() cipher_hex = cipher_bytes.hex().upper() # 解密 cipher_bytes = bytes.fromhex(cipher_hex) decryptor = cipher.decryptor() plain_text_bytes = decryptor.update(cipher_bytes) decryptor.finalize() plain_text_str = plain_text_bytes.decode() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值