/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab1;
/**
*
* @author OZZY
*/
public class EncString {
private StringBuffer str;
private int key;
private static String text[] = {"abcdefghijklmnopqrstuvwxyz", "GOYDSIPELUAVCRJWXZNHBQFTMK"};
private String keyStr;
/**
*
* @param str - шифруемая строка
* @param key - ключ, число позиций для сдвига
* @param keyStr - ключ (лозунг), строка для алгоритма Виженера
*/
public EncString(StringBuffer str, int key, String keyStr) {
this.str = str;
this.key = key;
this.keyStr = keyStr;
}
public void encryptShift() {
for (int i = 0; i < str.length(); i++) {
str.setCharAt(i, (char) (str.charAt(i) + key));
}
outstr("Зашифрованная строка (алгоритм сдвига): ");
}
public void decipherShift() {
for (int i = 0; i < str.length(); i++) {
str.setCharAt(i, (char) (str.charAt(i) - key));
}
outstr("Расшифрованная строка (алгоритм сдвига): ");
}
public void encryptReplace() {
for (int i = 0; i < str.length(); i++) {
int index = text[0].indexOf(str.charAt(i));
str.setCharAt(i, text[1].charAt(index));
}
outstr("Зашифрованная строка (алгоритм замены): ");
}
public void decipherReplace() {
for (int i = 0; i < str.length(); i++) {
int index = text[1].indexOf(str.charAt(i));
str.setCharAt(i, text[0].charAt(index));
}
outstr("Расшифрованная строка (алгоритм замены): ");
}
public void encryptVizhener () {
for (int i = 0; i < str.length(); i++) {
int index = text[0].indexOf(keyStr.charAt(i));
index += text[0].indexOf(str.charAt(i));
index %= 25;
str.setCharAt(i, text[1].charAt(index));
System.out.println(index);
}
outstr("Зашифрованная строка (алгоритм Виженера): ");
}
public void decipherVizhener () {
for (int i = 0; i < str.length(); i++) {
int index = text[1].indexOf(str.charAt(i)) + 25;
index -= text[0].indexOf(keyStr.charAt(i));
index %= 25;
str.setCharAt(i, text[0].charAt(index));
}
outstr("Расшифрованная строка (алгоритм Виженера): ");
}
private void outstr(String msg) {
System.out.println(msg);
System.out.println(str);
System.out.println();
}
}
// МАЙН
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab1;
import java.util.Random;
/**
*
* @author OZZY
*/
public class Main {
/**
* @param args the command line arguments
*/
private static Random random = new Random ();
static int generateRandom(int n) {
return Math.abs(random.nextInt()) % n;
}
public static void main(String[] args) {
EncString eStr = new EncString(new StringBuffer("thisi"), 2, "sesame");
eStr.encryptShift();
eStr.decipherShift();
eStr.encryptReplace();
eStr.decipherReplace();
eStr.encryptVizhener();
eStr.decipherVizhener();
}
}