-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncriptar.java
104 lines (90 loc) · 2.71 KB
/
Encriptar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package criptofinal;
/**
*
* @author danielsalnikov
*/
public class Encriptar {
protected final char[] letras = {' ', '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'};
protected final int[] clave = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26};
protected String frase;
private final int base = 179131;
private final int llavePublica = 19;
public Encriptar(String f) {
if (f.length() % 2 == 1) {
f = f + "x";
}
this.frase = f.toLowerCase();
}
public int[] traduccion(String f) {
int l;
l = f.length();
char z;
int[] vecNum = new int[l];
for (int i = 0; i < l; i++) {
z = f.charAt(i);
for (int j = 0; j < 27; j++) {
if (z == letras[j]) {
vecNum[i] = clave[j];
}
}
}
return vecNum;
}
public int[] claveNum(int[] v) {
String[] claveNum = new String[v.length];
String[] claveNume = new String[(v.length) / 2];
int vecClave[] = new int[(v.length) / 2];
for (int i = 0; i < v.length; i++) {
if (v[i] < 10) {
claveNum[i] = "0" + Integer.toString(v[i]);
} else {
claveNum[i] = Integer.toString(v[i]);
}
}
int k = 0;
for (int j = 0; j < v.length / 2; j++) {
claveNume[j] = claveNum[k] + claveNum[k + 1];
k += 2;
}
for (int s = 0; s < v.length / 2; s++) {
vecClave[s] = Integer.parseInt(claveNume[s]);
}
return vecClave;
}
public int modular(int x, int y, int z) {
long res = 1;
long bas = x;
while (y > 0) {
if ((y % 2) == 1) {
res = (res * bas) % z;
}
bas = (bas * bas) % z;
y = Math.floorDiv(y, 2);
}
return (int) res;
}
public int[] encriptarTexto(int[] v) {
int vecEncr[] = new int[v.length];
for (int i = 0; i < v.length; i++) {
vecEncr[i] = modular(v[i], this.llavePublica, this.base);
}
return vecEncr;
}
public String getFrase() {
return frase;
}
public void setFrase(String frase) {
this.frase = frase;
}
public int getBase() {
return base;
}
public int getLlavePublica() {
return llavePublica;
}
}