用DES方式,密钥为16位0101010101010101加密和解密举例说明

在Java中使用DES加密和解密,你可以使用 Cipher 类。以下是一个简单的例子,使用16位密钥进行加密和解密:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class DesExample {
    public static void main(String[] args) throws Exception {
        String key = "0101010101010101"; // 16位密钥
        String originalText = "Hello, World!";

        // 加密
        String encryptedText = encrypt(originalText, key);
        System.out.println("Encrypted Text: " + encryptedText);

        // 解密
        String decryptedText = decrypt(encryptedText, key);
        System.out.println("Decrypted Text: " + decryptedText);
    }

    private static String encrypt(String text, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        SecretKey secretKey = generateKey(key);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    private static String decrypt(String encryptedText, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        SecretKey secretKey = generateKey(key);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }

    private static SecretKey generateKey(String key) throws Exception {
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        return keyFactory.generateSecret(desKeySpec);
    }
}

这个例子中,encrypt 方法使用密钥对明文进行加密,decrypt 方法使用相同的密钥对密文进行解密。注意,由于DES的安全性问题,不推荐在实际应用中使用。如果可能的话,考虑更安全的加密算法,如AES。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现DES加密解密的示例代码,其中包括了64密钥加密解密功能: ```java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class DesUtil { private static final String ALGORITHM = "DES"; private static final String TRANSFORMATION = "DES/ECB/PKCS5Padding"; /** * 加密 * * @param key 密钥,长度为8个字符 * @param data 待加密数据 * @return 加密后的数据,使用Base64编码 * @throws Exception */ public static String encrypt(String key, String data) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encryptedData = cipher.doFinal(data.getBytes()); return Base64.getEncoder().encodeToString(encryptedData); } /** * 解密 * * @param key 密钥,长度为8个字符 * @param encryptedData 加密后的数据,使用Base64编码 * @return 解密后的数据 * @throws Exception */ public static String decrypt(String key, String encryptedData) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedData)); return new String(decryptedData); } public static void main(String[] args) { String key = "12345678"; // 密钥,长度为8个字符 String data = "Hello, world!"; // 待加密数据 try { String encryptedData = encrypt(key, data); System.out.println("加密后的数据:" + encryptedData); String decryptedData = decrypt(key, encryptedData); System.out.println("解密后的数据:" + decryptedData); } catch (Exception e) { e.printStackTrace(); } } } ``` 需要注意的是,DES算法已经被认为不安全,因为其密钥长度过短。在实际应用中,可以使用更加安全的AES算法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值