DES加密遇到的问题(Java)

错误:java.io.IOException: javax.crypto.BadPaddingException: Given final block not properly padded.

一开始以为是下面这段代码的问题,换成另一段代码。

SecureRandom ran = new SecureRandom();
DESKeySpec dKey = new DESKeySpec(password.getBytes());
SecretKeyFactory kF = SecretKeyFactory.getInstance("DES");
SecretKey skey = kF.generateSecret(dKey);
SecureRandom ran = SecureRandom.getInstance("SHA1PRNG");
ran.setSeed(password.getBytes());
KeyGenerator kgen = KeyGenerator.getInstance("DES");
kgen.init(56,ran);
SecretKey skey = kgen.generateKey();

结果发现问题没有解决。问题在下面这段代码:

Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, skey, ran);
byte[] buffer = new byte[1024];
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(destFile);
CipherInputStream cin = new CipherIntputStream(in,cipher);
int r = 0;
while ((r = in.read(buffer))!=-1) {
      cin.write(buffer, 0, r);
}

改正之后的代码:

Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, skey, ran);
byte[] buffer = new byte[1024];
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(destFile);
CipherOutputStream cot = new CipherOutputStream(out,cipher);
int r = 0;
while ((r = in.read(buffer))!=-1) {
      cot.write(buffer, 0, r);
}

最后发现是模式与输出不同,加密模式应该用CipherInputStream,解密模式用CipherOutputStream,输入流与输出流要考虑清楚。直接复制加密代码作为解密代码时,要考虑清楚要更改的地方。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值