Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
33
Task/Vigen-re-cipher/Java/vigen-re-cipher-1.java
Normal file
33
Task/Vigen-re-cipher/Java/vigen-re-cipher-1.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
public class VigenereCipher {
|
||||
public static void main(String[] args) {
|
||||
String key = "VIGENERECIPHER";
|
||||
String ori = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
String enc = encrypt(ori, key);
|
||||
System.out.println(enc);
|
||||
System.out.println(decrypt(enc, key));
|
||||
}
|
||||
|
||||
static String encrypt(String text, final String key) {
|
||||
String res = "";
|
||||
text = text.toUpperCase();
|
||||
for (int i = 0, j = 0; i < text.length(); i++) {
|
||||
char c = text.charAt(i);
|
||||
if (c < 'A' || c > 'Z') continue;
|
||||
res += (char)((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
|
||||
j = ++j % key.length();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static String decrypt(String text, final String key) {
|
||||
String res = "";
|
||||
text = text.toUpperCase();
|
||||
for (int i = 0, j = 0; i < text.length(); i++) {
|
||||
char c = text.charAt(i);
|
||||
if (c < 'A' || c > 'Z') continue;
|
||||
res += (char)((c - key.charAt(j) + 26) % 26 + 'A');
|
||||
j = ++j % key.length();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
58
Task/Vigen-re-cipher/Java/vigen-re-cipher-2.java
Normal file
58
Task/Vigen-re-cipher/Java/vigen-re-cipher-2.java
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import com.google.common.collect.Streams;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import static java.nio.charset.StandardCharsets.US_ASCII;
|
||||
|
||||
public class VigenereCipher {
|
||||
private final static int LOWER = 'A';
|
||||
private final static int UPPER = 'Z';
|
||||
private final static int SIZE = UPPER - LOWER + 1;
|
||||
private final Supplier<Stream<Character>> maskStream;
|
||||
|
||||
public VigenereCipher(final String key) {
|
||||
final String mask = new String(key.getBytes(US_ASCII)).toUpperCase();
|
||||
maskStream = () ->
|
||||
Stream.iterate(0, i -> (i+1) % mask.length()).map(mask::charAt);
|
||||
}
|
||||
|
||||
private String transform(final String text, final boolean encode) {
|
||||
final Stream<Integer> textStream = text.toUpperCase().chars().boxed()
|
||||
.filter(i -> i >= LOWER && i <= UPPER);
|
||||
return Streams.zip(textStream, maskStream.get(), (c, m) ->
|
||||
encode ? c + m - 2 * LOWER : c - m + SIZE)
|
||||
.map(c -> Character.toString(c % SIZE + LOWER))
|
||||
.collect(Collectors.joining());
|
||||
}
|
||||
|
||||
public String encrypt(final String plaintext) {
|
||||
return transform(plaintext,true);
|
||||
}
|
||||
|
||||
public String decrypt(final String ciphertext) {
|
||||
return transform(ciphertext,false);
|
||||
}
|
||||
}
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class VigenereCipherTest {
|
||||
private static final VigenereCipher Vigenere = new VigenereCipher("VIGENERECIPHER");
|
||||
|
||||
@Test
|
||||
@DisplayName("encipher/decipher round-trip succeeds")
|
||||
void vigenereCipherTest() {
|
||||
final String input = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
final String expectEncrypted = "WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY";
|
||||
final String expectDecrypted = "BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH";
|
||||
|
||||
final String ciphertext = Vigenere.encrypt(input);
|
||||
assertEquals(expectEncrypted, ciphertext);
|
||||
|
||||
final String plaintext = Vigenere.decrypt(ciphertext);
|
||||
assertEquals(expectDecrypted, plaintext);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue