gpt4 book ai didi

java - 加密代码

转载 作者:行者123 更新时间:2023-12-01 10:30:27 24 4
gpt4 key购买 nike

我正在尝试编写一段简单的代码来获取文档并对其进行加密然后解密。我的加密工作正常,但由于某种原因它使用不同的数组来解密。这是我的代码。

import java.util.ArrayList;

public class EncryptAlpha
{
private static int key;
public static ArrayList<Integer> num = new ArrayList<Integer>();

/**
* Constructs a EncryptAlpha object and sets
* it encryption key to k
* @param k
*/
public static int EncryptAlpha(int k)
{
ArrayList<Integer> num = new ArrayList<Integer>();
for(int i=0; i<10;i++)
{
k= 1+(int)(Math.random()*(100-1)+1);
num.add(k);
System.out.println(num);
}
for(int n=0; n<num.size();n++)
{
key=num.get(n);
if(n==num.size())
n=0;
}
return key;
}


/**
* This method takes a String object and converts it into an
* ArrayList of integers, where each integer is the sum of
* consecutive pairs of characters from str where the sum is
* one character's integer number value plus 1000 times the
* integer value of the next character. If there are an odd
* number of characters a ' ' is added as the last character
* @param str theString to be converted
* @return the ArrayList of integer values
*/
private ArrayList<Integer> convert(String str)
{
ArrayList<Integer> converted = new ArrayList<Integer>();

ArrayList<Integer> num = new ArrayList<Integer>();
for(int i=0; i<10;i++)
{
int k= 1+(int)(Math.random()*(100-1)+1);
num.add(k);
}
for(int i=0; i<str.length(); i+=2)
{
char t1, t2 = ' ';
t1 = str.charAt(i);
if(i<str.length()-1)
t2 = str.charAt(i+1);
int x = (int)t1 + 1000*(int)t2;
converted.add(x);
}

return converted;
}

/**
* This method takes an ArrayList of integers where each integer
* is the sum of consecutive pairs of characters from the original
* String where the sum is one character's integer number value
* plus 1000 times the integer value of the next character.
* @param converted the ArrayList to convert back into a String
* @return the 'original' String
*/
private String deconvert(ArrayList<Integer> converted)
{
String str = new String();
for(int temp : converted)
{
char t1, t2;
t1 = (char)(temp%1000);
t2 = (char)(temp/1000);
str = str + t1 + t2;
}

return str;
}


/**
* Converts the String str into an ArrayList of integers
* @param str a message to be encrypted
* @return the converted ArrayList<Integer>
*/
public ArrayList<Integer> encrypt(String str)
{
ArrayList<Integer> converted = convert(str);
for(int i=0; i<converted.size(); i++)
{
int temp = converted.get(i)*EncryptAlpha(key);
converted.set(i,temp);
}
return converted;
}

/**
* Converts the ArrayList of integers converted back into
* the original message from which it was created.
* @param converted the ArrayList to be decrypted
* @return the original message
*/
public String decrypt(ArrayList<Integer> converted)
{
String str = new String();
for(int i=0; i<converted.size(); i++)
{
for(int n=0; n<num.size();n++)
{
key=num.get(n);
}
int temp = converted.get(i)/key;
converted.set(i, temp);
}
return deconvert(converted);
}


public static void main(String[] args)
{
// Brownie points for anyone who knows why the name of
// EncryptAlpha object is enigma.
EncryptAlpha enigma = new EncryptAlpha();
String message = "I really like robots.";

// Test the private methods
ArrayList<Integer> converted = enigma.convert(message);
System.out.println(converted);
String original = enigma.deconvert(converted);
System.out.println(original);
System.out.println();

// Test the public methods
ArrayList<Integer> encrypted = enigma.encrypt(message);
System.out.println(encrypted);
original = enigma.decrypt(encrypted);
System.out.println(original);
}

问题是,我使用一个数组来存储字母移位的值,但由于某种原因,加密和解密使用两个不同的数组。我该如何解决这个问题?

最佳答案

如果我确实很好地理解了您的“问题”,那么您对字段/局部变量有误解,这是您的代码的注释版本:

public class EncryptAlpha 
{
private static int key;

// let's call this one "num1"
public static ArrayList<Integer> num = new ArrayList<Integer>();

// ...

public static int EncryptAlpha(int k)
{
ArrayList<Integer> num = new ArrayList<Integer>(); // this is *not* num1

这称为变量阴影,请参阅 this question例如。您可能想删除局部变量声明(即 ArrayList<Integer> num = new ArrayList<Integer>(); )

一些阅读:

关于java - 加密代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35118153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com