【问题标题】:Key for Encrypting and Decrypting a Text File in JavaJava中用于加密和解密文本文件的密钥
【发布时间】:2015-10-08 02:38:29
【问题描述】:

我正在使用 Java 开发一个加密/解密程序。我正在使用一个名为“secret1234”的密钥来加密和解密来自文本文件的消息。该程序使用字符串作为键。但是,我有兴趣使用数字(整数)作为密钥,以便生成随机数。那么,如何修改这个程序,让密钥是整数而不是字符串来加密和解密消息呢?

这是程序:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class Program {

    public static void main(String[] args) {

        try {
            String key = "secret1234";// This is the key

            FileInputStream fis = new FileInputStream("text.txt");//text.txt is a text file with a short message                                        
            FileOutputStream fos = new FileOutputStream("encryptedText.txt");
            encrypt(key, fis, fos);

            FileInputStream fis2 = new FileInputStream("encryptedText.txt");
            FileOutputStream fos2 = new FileOutputStream("decryptedText.txt");
            decrypt(key, fis2, fos2);

        } catch (Throwable e) {
            e.printStackTrace();
        }

    }

    public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable {
        encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
    }

    public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable {
        encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
    }

    public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {

        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        SecretKey desKey = skf.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES");

        if (mode == Cipher.ENCRYPT_MODE) {
            cipher.init(Cipher.ENCRYPT_MODE, desKey);
            CipherInputStream cis = new CipherInputStream(is, cipher);
            doCopy(cis, os);
        } else if (mode == Cipher.DECRYPT_MODE) {
            cipher.init(Cipher.DECRYPT_MODE, desKey);
            CipherOutputStream cos = new CipherOutputStream(os, cipher);
            doCopy(is, cos);
        }
    }

    public static void doCopy(InputStream is, OutputStream os) throws IOException {
        byte[] bytes = new byte[64];
        int numBytes;
        while ((numBytes = is.read(bytes)) != -1) {
            os.write(bytes, 0, numBytes);
        }
            os.flush();
            os.close();
            is.close();
    }

}

【问题讨论】:

  • 您可以获取字符串的 SHA-2 哈希,并使用它来获取字节。但我怀疑你会以这种方式创建一个非常强大的密码。

标签: java string encryption integer


【解决方案1】:

在您的代码中使用它来生成密钥:

String key = String.valueOf(SecureRandom.getInstance("SHA1PRNG").nextInt());

【讨论】:

  • 是否可以获得特定范围的随机数?假设是 0 到 300 之间的数字。
  • 在 nextInt() 方法中传递 300 作为 nextInt(300)
猜你喜欢
  • 2014-10-24
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
相关资源
最近更新 更多