| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import static java.lang.Character.isLowerCase;
- import static java.lang.Character.isUpperCase;
- import static java.lang.Character.toLowerCase;
-
- public class ROT13 {
-
- Character cs;
- Character cf;
-
- ROT13(Character cs, Character cf) {
- this.cs= cs;
- this.cf=cf;
- }
-
- ROT13() {
- this.cs='a';
- this.cf='m';
- }
-
-
- public String crypt(String text) throws UnsupportedOperationException {
- return encrypt(encrypt(text));
- }
-
- public String encrypt(String text) {
- String result = "";
- int rotator = cf-cs;
- boolean isUppercase = false;
- for(int i=0; i<text.length(); i++){
- isUppercase = Character.isUpperCase(text.charAt(i));
- int temp = Character.toLowerCase(text.charAt(i));
- if (temp<97|temp>122){
- } else if(temp+rotator < 122){
- temp = (int)text.charAt(i)+rotator;
- } else {
- temp = 96 + ((temp+rotator)-122);
- }
- if(isUppercase){temp=Character.toUpperCase(temp);}
- result+= (char)temp;
- }
- return result;
- }
-
- public String decrypt(String text) {
-
- String result = "";
- int rotator = cf-cs;
- boolean isUppercase = false;
- for(int i=0; i<text.length(); i++){
- isUppercase = Character.isUpperCase(text.charAt(i));
- int temp = Character.toLowerCase(text.charAt(i));
- if (temp<97|temp>122){
- } else if(temp-rotator > 97){
- temp = (int)text.charAt(i)-rotator;
- } else {
- temp = 122 - (96-(temp-rotator));
- }
- if(isUppercase){temp=Character.toUpperCase(temp);}
- result+= (char)temp;
- }
- return result;
- }
-
- public static String rotate(String s, Character c) {
- String result = "";
- String reference = s+s;
- int rotator = Character.toLowerCase(c)-'a';
- boolean isUppercase = false;
- for(int i=0; i<s.length(); i++){
- Character temp = reference.charAt(i+rotator);
- result+= temp;
- }
- return result;
- }
-
- }
|