Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于在Java中加密和解密文本文件的密钥_Java_String_Encryption_Integer - Fatal编程技术网

用于在Java中加密和解密文本文件的密钥

用于在Java中加密和解密文本文件的密钥,java,string,encryption,integer,Java,String,Encryption,Integer,我正在用Java编写一个加密/解密程序。我正在使用一个名为“secret1234”的密钥对文本文件中的消息进行加密和解密。此程序使用字符串作为键。但是,我对使用数字(整数)作为键感兴趣,这样我就可以生成随机数。那么,如何修改该程序以允许密钥为整数而不是用于加密和解密消息的字符串呢 以下是节目: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ja

我正在用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();
    }

}

在代码中使用此选项生成密钥:

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

您可以获取字符串的SHA-2散列,并使用它来获取字节。但我怀疑你会不会用这种方法创造出一个非常强大的密码。有可能得到一个特定范围的随机数吗?假设数字介于0到300之间。在nextInt()方法中将300作为nextInt(300)传递