java使用des算法_Java使用DES算法加密处理

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

定义所需要用到的变量

private static final String ENCRYPTION_KEY = "HI, I'M UPYOU.";

private static final String CHARSET_NAME = "UTF-8";

private static final String ALGORITHM_NAME = "DES";private static Key key;

将ENCRYPTION_KEY作为一个加密密钥,将ENCRYPTION_KEY转成Key类型。

static { try { // 生成 ALGORITHM_NAME 算法对象 KeyGenerator edsAlgorithmInstance = KeyGenerator.getInstance(ALGORITHM_NAME); // 使用SHA1安全策略 SecureRandom sha1PRNG = SecureRandom.getInstance("SHA1PRNG"); // 设置密钥 ENCRYPTION_KEY sha1PRNG.setSeed(ENCRYPTION_KEY.getBytes()); // 初始化KeyGenerator对象 edsAlgorithmInstance.init(sha1PRNG); //生成 key key = edsAlgorithmInstance.generateKey(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } }

创建对字符串加密方法 encrypetString

public static String encryptString(){}

encrypetString方法中操作

需要使用BASE64Encod

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用DES算法对学号姓名进行加密Java代码: ```java import javax.crypto.Cipher; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.IvParameterSpec; import javax.crypto.SecretKeyFactory; import javax.crypto.SecretKey; public class DESUtil { private static final String KEY = "12345678"; // 密钥 private static final String IV = "12345678"; // 初始向量 public static String encrypt(String source) throws Exception { DESKeySpec keySpec = new DESKeySpec(KEY.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(keySpec); IvParameterSpec iv = new IvParameterSpec(IV.getBytes("UTF-8")); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, iv); byte[] encrypted = cipher.doFinal(source.getBytes("UTF-8")); return bytesToHexString(encrypted); } public static String decrypt(String encrypted) throws Exception { byte[] enc = hexStringToBytes(encrypted); DESKeySpec keySpec = new DESKeySpec(KEY.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(keySpec); IvParameterSpec iv = new IvParameterSpec(IV.getBytes("UTF-8")); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, iv); byte[] decrypted = cipher.doFinal(enc); return new String(decrypted, "UTF-8"); } private static String bytesToHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(bytes[i] & 0xFF); if (hex.length() < 2) { sb.append('0'); } sb.append(hex); } return sb.toString(); } private static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } hexString = hexString.toUpperCase(); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } return d; } private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } } ``` 以上代码使用Java标准库中的DES算法实现加密和解密。加密和解密过程中使用了PKCS5填充,以保证数据块长度为8的倍数。需要注意的是,由于DES算法已经被认为是不安全的算法,因此在实际应用中需要使用更加安全的算法来进行数据加密和解密。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值