Reputation: 27
I am trying to do a affine methods but my output is totally wrong and I have no clue how to fix it, can anyone look at it and tell me what is wrong
public void Affine(int a, int b){
StringBuilder builder = new StringBuilder();
int len = jMesaj.getText().length(); //length of the input
for (int i = 0; i < len; i++){
char currentChar = jMesaj.getText().charAt(i);
char finalChar = ((char)(currentChar +(a*i + b)% 26));
builder.append(finalChar);
}
String result = builder.toString();
builder.delete( 0 , builder.length() -1 );
jEncryptionResult.setText(result); //display result in jTextArea
}
Upvotes: 1
Views: 825
Reputation: 27
/** * Class : AffineCipher * * @author : Oracle * * Written : Aug 26, 2013 9:01:28 PM * * Compiler : NetBeans 7.3.1 * * Platform : Windows 7 Ultimate */
package enigma;
public class AffineCipher {
public String Encryption(String plainText, int a , int b){
StringBuilder builder = new StringBuilder();
int len = plainText.length();
for (int i = 0; i < len; i++){
char currentChar = plainText.toUpperCase().charAt(i);
int IntcurrentChar = (int) currentChar - 65;
int finalCharInt = (a* IntcurrentChar + b) % 26;
int FinalCharint = finalCharInt + 65;
char finalChar = (char) FinalCharint;
builder.append(finalChar);
}
String result = builder.toString().toLowerCase();
return result;
}//end String Encryption
public String Encrypt(String plainText, int a, int b ){
int len = plainText.length();
StringBuilder sb = new StringBuilder();
String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i = 0 ; i < len ; i++){
int charPos = alphabet.indexOf(plainText.charAt(i));
/*
*Read the character from input and get the position of the it in string
*/
int keyVal = (a* charPos+b) % 26;
/*
* keyVal store the position of the character that replace the one from the input
*/
char finalChar = alphabet.charAt(keyVal);
sb.append(finalChar);
}
String result = sb.toString();
return result;
}
Upvotes: 0
Reputation: 5402
The char is a value between 0-65536 (2 bytes, 2^16). It's a little bit messy with the Unicode stuff, at least if you want to use esötéric çhåräcters in your seçret cömmünìcåtiön.
If you know your alphabet on beforehand a simpler implementation would be to use
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] alphabet = str.toCharArray(); //make sure it's sorted for binarySearch to work!
and then lookup the positions of your incoming chars through java.utils.Array.binarySearch(alphabet, char)
, make your maths on the returned int
array position (modulo the length of the alphabet
array) and look up the encrypted letter from this position put it in your out-message StringBuilder
.
You could also create two HashMaps<char,char>
, one for encryption and the other for decryption.
Upvotes: 0
Reputation: 17422
It seems to me you are assuming that 'A' = 1, 'B' = 2, etc. Also, the affine algorithm is slightly different. if you're using 26 as module it makes me thing that you are cyphering from 'A' to 'Z'. If that's true then try adding an offset in this line, something like this:
char finalChar = (char)((a*(currentChar - 'A') + b) % 26 + 'A');
Upvotes: 1