Spoiler: show
- Κώδικας: Επιλογή όλων
package cryptography;
public class Encrypt
{
private char[][] alphabet={
{'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'},
{'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','A'},
{'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B'},
{'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C'},
{'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D'},
{'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E'},
{'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F'},
{'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G'},
{'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H'},
{'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I'},
{'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J'},
{'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K'},
{'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L'},
{'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M'},
{'O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N'},
{'P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O'},
{'Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P'},
{'R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q'},
{'S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R'},
{'T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S'},
{'U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T'},
{'V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U'},
{'W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V'},
{'X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W'},
{'Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X'},
{'Z','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'},
};
private String key;
public Encrypt(String key)
{
this.key=key.toUpperCase();//Kanei ola ta grammata kefalaia
}
public String encrypt(String plaintext) //throws ArrayIndexOutOfBoundsException
{
/*Prosessing the plaintext*/
plaintext=proccessPlaintext(plaintext);
int keylen=key.length();//getting the length of the key
int plainlen=plaintext.length();//getting the length of the plaintext
String tempkey="";//temporary key
if(plainlen>=keylen)
{
int repeat=plainlen/keylen;//searching how many time to repeat the key
/*
At most times because numbers cannod divide without having a modulo
the key we repeat the kay as times as we get from (plaintext/key)+1
*/
for(int i=0;i<repeat+1;i++)
{
tempkey+=key;
}
tempkey=tempkey.substring(0,plainlen);
}
else
{
throw new ArrayIndexOutOfBoundsException("Το κείμενο πρέπει να είναι μεγαλύτερο από το κλειδί");
}
char c[]=new char[plainlen];
for(int j=0;j<plainlen;j++)
{
/*I am scanning the plaintext and the key char by char and convert it into integer
Then I try to transform the integer as a index of the matrix alphabet so I want the
latin capital letters of Unicode (space [65-122]) into the space of [0-24] (including 0 and 24)*/
c[j]=alphabet[Math.abs(tempkey.codePointAt(j)-65)][Math.abs(plaintext.codePointAt(j)-65)];
}
return new String(c,0,c.length);
}
public String proccessPlaintext(String plaintext)
{
String [] pl=plaintext.split(" ");
String nplaintext="";
for(int i=0;i<pl.length;i++)
{
nplaintext+=pl[i];
}
return nplaintext.toUpperCase();
}
}