I have to write codes to simulate the operation of three encryption methods: - Caesar cipher , Columnar Transposition and RSA . I created an interface called Encryption
public interface Encryption {
public String encrypt(String key, String message);
}
and made two classes: CaesarCipherEncryptor and
public class CaeserCipherEncryptor implements Encryption {
private static final CaeserCipherEncryptor INSTANCE = new CaeserCipherEncryptor();
private CaeserCipherEncryptor(){ }
public static CaeserCipherEncryptor getInstance(){
return INSTANCE;
}
public String encrypt(String key, String message) {
int noOfShifts = Integer.parseInt(key);
String cipherText = "";
for(char c:message.toCharArray()){
if((Character.isUpperCase(c) && ((c+noOfShifts) > 90)) || (Character.isLowerCase(c) && ((c+noOfShifts) > 122)))
c = (char)((c + noOfShifts) - 26);
else
c = (char)(c + noOfShifts);
cipherText += c;
}
return cipherText;
}
}
ColumnarTranspositionEncryptor implements it.
import java.util.ArrayList;
public class ColumnarTranspositionEncryptor implements Encryption {
private static final ColumnarTranspositionEncryptor INSTANCE = new ColumnarTranspositionEncryptor();
public static ColumnarTranspositionEncryptor getInstance(){
return INSTANCE;
}
private ColumnarTranspositionEncryptor(){}
@Override
public String encrypt(String key, String message) {
int rowLength = key.length();
key.toUpperCase();
ArrayList<Integer> finalOrder = new ArrayList<Integer>();
for(int i=0;i<rowLength;i++){
int rank=0;
char c = key.charAt(i);
for(int j=0;j<rowLength;j++){
if(c>key.charAt(j))
rank++;
}
finalOrder.add((rank+1));
}
ArrayList<String> rowsOfString=new ArrayList<String>();
for(;message.length()>rowLength;message=message.substring(rowLength)){
rowsOfString.add(message.substring(0, rowLength));
}
for(;message.length()>0 && message.length()<rowLength;){
char c=65;
message += (char)(c+new java.util.Random().nextInt(26));
}
rowsOfString.add(message);
String cipherText = "";
for(int i=1;i<=finalOrder.size();i++){
int j=finalOrder.indexOf(i);
for(String s:rowsOfString){
cipherText += s.charAt(j);
}
}
System.out.println("Alphabetical order and columns:");
for(int i:finalOrder)
System.out.print(i + " ");
System.out.println();
System.out.println();
for(String s:rowsOfString){
for(int i=0;i<s.length();i++)
System.out.print(s.charAt(i) + " ");
System.out.println();
}
return cipherText;
}
}
These two classes were made by Singeltons, as they are ordinary algorithms. However, as indicated in the wikipedia link, in RSA encryption, I need to save the state of a specific user in n, phiOfN, publicKey and privateKey objects to simulate the encryption procedure. Please suggest a more generalized solution for structuring the RSAEncryptor class. The following code is all I could work out: |.
import java.util.ArrayList;
import java.math.BigInteger;
public class RSAUser {
private int p,q,n,phiOfN, publicExponent, privateExponent;
private static int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
public RSAUser(int p,int q){
this.p = p;
this.q = q;
n = p * q;
int phiOfN = (p - 1) * (q - 1);
int min = Math.min(p, q);
this.publicExponent = 2;
ArrayList<Integer> x = new ArrayList<Integer>();
for (; publicExponent < min - 1; publicExponent++) {
if (RSAUser.gcd(publicExponent, phiOfN) == 1)
x.add(publicExponent);
}
this.publicExponent = x.get(new java.util.Random().nextInt(x.size()));
privateExponent = phiOfN / publicExponent;
for (int i = 1;; i++) {
long product = phiOfN * i + 1;
if (product % publicExponent == 0) {
privateExponent = (int)product / publicExponent;
break;
}
}
System.out.println("Public Key:"+this.getPublicKey()+"\nPrivate Key:"+this.getPrivateKey());
}
public String getPublicKey(){
return "("+Integer.toString(n)+","+Integer.toString(publicExponent)+")";
}
public String getPrivateKey(){
return "("+Integer.toString(n)+","+Integer.toString(privateExponent)+")";
}
}
public class RSAEncryptor implements Encryption {
private static RSAEncryptor instance = new RSAEncryptor();
private RSAEncryptor(){
}
public static RSAEncryptor getInstance(){
return instance;
}
@Override
public String encrypt(String key, String message) {
key.trim();
String[] keys = key.split(",");
String n = keys[0].substring(1).trim();
String publicExponent = keys[1].trim();
publicExponent = publicExponent.substring(0, publicExponent.length()-1);
BigInteger m = new BigInteger(message);
return m.pow(Integer.parseInt(publicExponent)).mod(new BigInteger(n)).toString(10);
}
}
? OO? , Advance.