public class AffinLin
{
String toChipher = null;
int a = 1, b = 0;
public AffinLin (int a, int b, String toChipher)
{
this.a = a;
this.b = b;
this.toChipher = toChipher;
System.out.println (toChipher);
for (int i = 0; i < toChipher.length (); ++i)
System.out.print (chipherChar (a, b, toChipher.charAt (i)));
System.out.print ("\n");
}
private char chipherChar (int a, int b, char character)
{
int returnValue = ((int)character);
returnValue -= ((int)'a');
returnValue = ((a * returnValue) + b) % 26;
returnValue += ((int)'a');
return ((char)returnValue);
}
public static void main (String args [])
{
int a = 1;
int b = 0;
if (args.length != 3)
{
System.out.println ("Usage java AffinLin a b Text");
System.exit (1);
}
if (args [2].matches (".*[^a-z].*"))
{
System.out.println ("Sorry Text to Chipher are not allowed to contain any characters except [a-z]");
System.exit (1);
}
try
{
a = Integer.parseInt (args[0]);
}
catch (NumberFormatException nfexcp)
{
System.out.println ("a was not a number but: "+a);
System.exit (1);
}
try
{
b = Integer.parseInt (args[1]);
}
catch (NumberFormatException nfexcp)
{
System.out.println ("b was not a number but: "+b);
System.exit (1);
}
new AffinLin (a, b, args [2]);
}
}