Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.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_Encryption - Fatal编程技术网

Java 如何进行加密和解密?

Java 如何进行加密和解密?,java,encryption,Java,Encryption,假设值为: int p=9; // (any odd no) int q=5; // (any odd no) int e=3; int n=p*q; 我通过以下代码读取文本文件并将内容转换为字节数组: while ((eof_line = br.readLine()) != null) { byte_array = eof_line.getBytes(); String decoded = new String(byte_array, "UTF

假设值为:

int p=9; // (any odd no)
int q=5; // (any odd no)
int e=3;
int n=p*q;
我通过以下代码读取文本文件并将内容转换为字节数组:

        while ((eof_line = br.readLine()) != null) {
        byte_array = eof_line.getBytes();
        String decoded = new String(byte_array, "UTF-8");
        // pow=byte_array.to;
        for (i = 0; i < byte_array.length; i++) {
            double input = byte_array[i];
            double t1 = Math.pow(e, -1);
            double t2 = n % 2;
            double encrypt = (Math.pow(input, e)) * t2;//formula
            double p_k = t1 * t2;
            double decrypt = (Math.pow(en, p_k)) * t2;//formula
            }}
while((eof_line=br.readLine())!=null){
字节数组=eof_line.getBytes();
已解码字符串=新字符串(字节数组,“UTF-8”);
//pow=字节_数组.to;
对于(i=0;i

这里我的输入是byte[],解密值是double。所以我想将decrypt(double)转换为原始文本文件内容。如何继续?

最后……我找到了解决办法

     public String[] OpenFile() throws IOException {
    sp1.setVisible(true);
    String k = t2.getText();
    e = Integer.valueOf(k);
    d = (Math.pow(e, -1)) * (n % 2);
    System.out.println("d=" + d);
    FileReader fr = new FileReader(path);
    BufferedReader br = new BufferedReader(fr);
    String eof_line;
    int numberOfLines = 0;
    while ((eof_line = br.readLine()) != null) {
        arr = eof_line.toCharArray();
        byte_array = eof_line.getBytes();
        for (i = 0; i < byte_array.length; i++) {
            ta1.append(Integer.toString(byte_array[i]));
            double input = byte_array[i];
            double t1 = Math.pow(e, -1);
            double t2 = n % 2;
            double en = (Math.pow(input, e)) * t2;
            p_key = t1 * t2;
            double de = (Math.pow(en, p_key)) * t2;
            b = (char) java.lang.Math.round(de);
            ta2.append("" + b);
            }
            br.close();
            return null;
          }
public String[]OpenFile()引发IOException{
sp1.setVisible(true);
字符串k=t2.getText();
e=整数。值(k);
d=(数学功率(e,-1))*(n%2);
System.out.println(“d=“+d”);
FileReader fr=新的FileReader(路径);
BufferedReader br=新的BufferedReader(fr);
字符串eof_线;
int numberOfLines=0;
而((eof_line=br.readLine())!=null){
arr=eof_line.toCharArray();
字节数组=eof_line.getBytes();
对于(i=0;i
终于……我找到了解决办法

     public String[] OpenFile() throws IOException {
    sp1.setVisible(true);
    String k = t2.getText();
    e = Integer.valueOf(k);
    d = (Math.pow(e, -1)) * (n % 2);
    System.out.println("d=" + d);
    FileReader fr = new FileReader(path);
    BufferedReader br = new BufferedReader(fr);
    String eof_line;
    int numberOfLines = 0;
    while ((eof_line = br.readLine()) != null) {
        arr = eof_line.toCharArray();
        byte_array = eof_line.getBytes();
        for (i = 0; i < byte_array.length; i++) {
            ta1.append(Integer.toString(byte_array[i]));
            double input = byte_array[i];
            double t1 = Math.pow(e, -1);
            double t2 = n % 2;
            double en = (Math.pow(input, e)) * t2;
            p_key = t1 * t2;
            double de = (Math.pow(en, p_key)) * t2;
            b = (char) java.lang.Math.round(de);
            ta2.append("" + b);
            }
            br.close();
            return null;
          }
public String[]OpenFile()引发IOException{
sp1.setVisible(true);
字符串k=t2.getText();
e=整数。值(k);
d=(数学功率(e,-1))*(n%2);
System.out.println(“d=“+d”);
FileReader fr=新的FileReader(路径);
BufferedReader br=新的BufferedReader(fr);
字符串eof_线;
int numberOfLines=0;
而((eof_line=br.readLine())!=null){
arr=eof_line.toCharArray();
字节数组=eof_line.getBytes();
对于(i=0;i
我想我理解,但您最好编辑您的问题,将输入和预期输出的示例包括在内,以确保您得到有用的答案。看起来您的空格键在代码上到处都是。介意清理一下吗?预期的加密和解密公式会给出相同的结果。然后将解密值转换为文本。事实上,在重读你的问题时,我想我误解了你被要求做的事情。你能检查一下要求吗。他们是要求你将文本转换为一个数字,并使用公式对其进行编码/解码……还是要求你将文本转换为编码/解码的数字序列单独?我的意思是将文本转换成一系列数字,使用双精度和字节数组进行单独编码/解码。那一次我无法完成任务。给出想法。我想我理解,但你最好编辑你的问题,包括输入和预期输出的示例,以确保你得到有用的answers.看起来你的空格键在你的代码上到处都是。介意清理一下吗?预期的加密和解密公式会给出相同的结果。然后将解密值转换为文本。事实上,在重读你的问题时,我想我误解了你被要求做的事情。你能检查一下要求吗?他们是在问y吗你想把文本转换成一个数字,然后用公式进行编码/解码……还是他们想让你把文本转换成一个数字序列,然后你单独进行编码/解码?我想把文本转换成一个数字序列,然后用双字节数组进行编码/解码。那一次我没法完成任务。给出想法。由于加密通常使用任意精度整数(不涉及小数)完成,您应该使用
biginger
而不是
double
,因为加密通常使用任意精度整数(不涉及小数)完成您应该使用
biginger
而不是
double