DES加解密【JAVA后台】

package com.*.*.web.utils;

import java.security.SecureRandom;
import java.util.Scanner;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.binary.Base64;


/**
 * <b>类说明:DES</b>
 * <p>
 * <b>详细描述:</b>
 * @since 2015年8月31日 下午17:00:16
 */
public class DESCryptUtil {
	
	private static final String DES = "DES";
	
	public static final String desKey = "9AAB1D2EE004AAC4";

    public static String doEncrypt(String plainMessage, String hexDesKey) throws Exception {
		byte desKey[] = hexDesKey.getBytes();
		byte desPlainMsg[] = plainMessage.getBytes();
		return Base64.encodeBase64URLSafeString(desCrypt(desKey, desPlainMsg, Cipher.ENCRYPT_MODE));
	}
    
    public static String doDecrypt(String hexEncryptMessage, String hexDesKey) throws Exception{
    	if (hexEncryptMessage == null) {
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DES加密解密算法是一种对称加密算法,在Java中可以使用JDK自带的Cipher类实现。下面是一个简单的示例代码: ```java import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.NoSuchAlgorithmException; import java.util.Base64; public class DesDemo { private static final String KEY_ALGORITHM = "DES"; private static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding"; public static String encrypt(String content, String password) throws Exception { SecretKey secretKey = generateKey(password); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedBytes = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encryptedBytes); } public static String decrypt(String content, String password) throws Exception { SecretKey secretKey = generateKey(password); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] encryptedBytes = Base64.getDecoder().decode(content); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); return new String(decryptedBytes, StandardCharsets.UTF_8); } private static SecretKey generateKey(String password) throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM); keyGenerator.init(56); return new SecretKeySpec(password.getBytes(StandardCharsets.UTF_8), KEY_ALGORITHM); } public static void main(String[] args) throws Exception { String content = "Hello, World!"; String password = "12345678"; String encryptedContent = encrypt(content, password); String decryptedContent = decrypt(encryptedContent, password); System.out.println("明文:" + content); System.out.println("密文:" + encryptedContent); System.out.println("解密后:" + decryptedContent); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值