전체 알파벳을 암호화하고 사용자가 제출 한 구문을 암호화 한 다음 다시 원래 구문으로 해독하는 난수를 사용하는 기본적인 암호화 프로그램을 만들려고합니다. 그러나 매우 어렵습니다. 누구든지 내 실수를 지적하는 데 도움이 될 수 있습니다! 같은 문자에 두 글자를 코딩해서는 안됩니다. 즉, a와 b는 둘 다 c와 일치해서는 안됩니다.자바 기본 암호화 프로그램
public class MainClass {
public static final int ALPHASIZE = 26;
public static final char[] Lalpha =
{ 'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'
};
public static final char[] Ualpha =
{'A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
};
protected static char[] encryptU = new char[ALPHASIZE];
protected static int[] decrypt = new int[ALPHASIZE];
protected static char[] encryptL = new char[ALPHASIZE];
Random rgenerator = new Random();
public MainClass(){
int randNum = rgenerator.nextInt(ALPHASIZE);
for(int i=0; i<ALPHASIZE ; i++)
{
//makes sure that it won't assign a letter to itself or to one that has already been assigned
do {
randNum = rgenerator.nextInt(26);
} while (randNum%26==0 &&Arrays.asList(encryptU).contains(Ualpha[randNum]));
encryptU[i] = Ualpha[randNum];
encryptL[i] = Lalpha[randNum];
decrypt[i] = randNum;
}
}
public String encrypt(String secret)
{
System.out.println(Arrays.toString(encryptU));
int position = 0;
char[] mess = secret.toCharArray();
for(int i = 0 ; i<mess.length;i++)
{
if(Character.isUpperCase(mess[i]))
{
for(int j = 0; j < encryptU.length; j++) {
if(mess[i]==Ualpha[j]) {
position = j;
}
mess[i] = encryptU[position];
}
}
if(Character.isLowerCase(mess[i]))
{
for(int j = 0; j < encryptU.length; j++) {
if(mess[i]==Lalpha[j]) {
position = j;
}
mess[i] = encryptL[position];
}
}
}
return new String(mess);
}
public String decrypt(String secret)
{
char[] mess = secret.toCharArray();
for(int i = 0 ; i<mess.length;i++)
{
if(Character.isUpperCase(mess[i]))
{
for(int j = 0; j<ALPHASIZE; j++){
if(mess[i]==encryptU[j]){
mess[i] = Ualpha[j];
}
}
}
if(Character.isLowerCase(mess[i]))
{
for(int j = 0; j<ALPHASIZE; j++){
if(mess[i]==encryptL[j]){
mess[i] = Lalpha[j];
}
}
}
}
return new String(mess);
}
}
많은 코드가 있습니다. 특정 문제가있는 작은 청크를 게시하면 더 나은 응답을 얻을 수 있습니다. 그것은 컴파일합니까? 같은 글자에 실제로 두 글자를 코딩하고 있습니까? –
@ John 원래 질문을 다시 올릴 수 있습니까? 그것을 삭제하면 협업 포럼의 목적을 상실합니다. 오, 최선의 대답을 받아들이는 것을 잊지 마라! – arshajii