【问题标题】:is this possible to encrypt and decrypt a zip file using DES algorithm in java这可以在java中使用DES算法加密和解密zip文件吗
【发布时间】:2012-07-26 13:01:19
【问题描述】:

我必须使用 DES 算法加密和解密 zip 文件,方法是使用存储在文本文件中的密钥。加密和解密算法都从文本文件中获取密钥来执行相应的功能。

有没有可用于在java中做DES算法的内置包...

请指导我摆脱这个问题...

【问题讨论】:

  • 为什么不给zip设置密码而不是加解密呢?
  • 别忘了 DES 坏了。使用(至少)triple des,或者更好的是,像 AES 这样的现代算法。

标签: java des


【解决方案1】:

你可以使用 javax.crypto 包中的东西:

        // read the key
    FileInputStream fis = new FileInputStream(keyFile);
    byte[] keyBytes = new byte[fis.available()];
    fis.read(keyBytes);
    SecretKeySpec spec = new SecretKeySpec(keyBytes, "DES");

    // encrypt
    Cipher encCipher = Cipher.getInstance("DES");
    encCipher.init(Cipher.ENCRYPT_MODE, spec);

    CipherInputStream cipherIn = new CipherInputStream(new FileInputStream(zipFile), encCipher);
    FileChannel out = new FileOutputStream(encZipFile).getChannel();
    out.transferFrom(Channels.newChannel(cipherIn), 0, Long.MAX_VALUE);

    // decrypt
    Cipher decCipher = Cipher.getInstance("DES");
    decCipher.init(Cipher.DECRYPT_MODE, spec);

    cipherIn = new CipherInputStream(new FileInputStream(encZipFile), decCipher);
    out = new FileOutputStream(decZipFile).getChannel();
    out.transferFrom(Channels.newChannel(cipherIn), 0, Long.MAX_VALUE);

【讨论】:

    【解决方案2】:

    这是可能的。你最好选择 bouncy castly。他们为此提供了 API。

    【讨论】:

      猜你喜欢
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多