Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class main {
- public static void main(String[] args) {
- char[] alpha = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
- Scanner scan = new Scanner(System.in);
- System.out.println("Podaj klucz oraz tekst do zaszyfrowania: ");
- String a = scan.nextLine();
- String b = scan.nextLine();
- System.out.println("Klucz: " + a + " " + a.length());
- System.out.println("Tekst: " + b + " " + b.length());
- int temp = (b.length() / a.length()) + 1;
- char[] text = b.toCharArray();
- char[] key = a.toCharArray();
- int[] valueofletter;
- valueofletter = new int[a.length()];
- int[] value;
- value = new int[a.length()];
- char[] passwd;
- passwd = new char[a.length() * temp];
- for (int i = 0; i < key.length; i++) {
- int n = 0;
- for (char x : alpha) {
- if (key[i] == x) {
- valueofletter[i] = n + 1;
- n = 0;
- } else {
- n++;
- }
- }
- }
- System.out.print("Warosc liczbowa klucza: ");
- for (int i = 0; i < value.length; i++) {
- for (int j = i + 1; j < value.length; j++) {
- if (valueofletter[i] == valueofletter[j]) {
- value[j] += 1;
- } else {
- if (valueofletter[i] < valueofletter[j]) {
- value[j] += 1;
- } else {
- value[i] += 1;
- }
- }
- }
- }
- for (int x : value) {
- System.out.print(x + " ");
- }
- System.out.println("\n");
- char[][] table;
- table = new char[temp][a.length()];
- int n = 0;
- for (int i = 0; i < temp; i++) {
- for (int j = 0; j < a.length(); j++) {
- if (n < text.length) {
- table[i][j] = text[n];
- n++;
- } else {
- table[i][j] = 'X';
- }
- }
- }
- for (int i = 0; i < temp; i++) {
- for (int j = 0; j < a.length(); j++) {
- System.out.print(table[i][j] + " ");
- }
- System.out.println(" ");
- }
- n = 0;
- int z = 0;
- for (int i = 0; i < value.length; i++) {
- int q = z;
- for (int j = 0; j < temp; j++) {
- if (n < passwd.length) {
- passwd[n] = table[j][q];
- n++;
- if (q == 0) {
- q = value.length - 1;
- } else {
- q--;
- }
- }
- }
- z++;
- }
- System.out.println("\n");
- String x = String.valueOf(passwd);
- System.out.print(x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement