Data update

This commit is contained in:
Ingy döt Net 2023-12-16 21:33:55 -08:00
parent 35bcdeebf8
commit 74c69a0df6
2427 changed files with 31826 additions and 3468 deletions

View file

@ -0,0 +1,133 @@
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_set>
#include <vector>
const std::string ADFGVX = "ADFGVX";
const std::string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
std::random_device random;
std::mt19937 mersenne_twister(random());
std::vector<std::vector<char>> initialise_polybius_square() {
std::vector<char> letters(ALPHABET.begin(), ALPHABET.end());
std::shuffle(letters.begin(), letters.end(), mersenne_twister);
std::vector<std::vector<char>> result = { 6, std::vector<char>(6, 0) };
for ( int32_t row = 0; row < 6; ++row ) {
for ( int32_t column = 0; column < 6; ++column ) {
result[row][column] = letters[6 * row + column];
}
}
return result;
}
// Create a key using a word from the dictionary 'unixdict.txt'
std::string create_key(const uint64_t& size) {
if ( size < 7 || size > 12 ) {
throw std::invalid_argument("Key should contain between 7 and 12 letters, both inclusive.");
}
std::vector<std::string> candidates;
std::fstream file_stream;
file_stream.open("../unixdict.txt");
std::string word;
while ( file_stream >> word ) {
if ( word.length() == size &&
word.length() == std::unordered_set<char>{ word.begin(), word.end() }.size() ) {
std::transform(word.begin(), word.end(), word.begin(), [](const char& ch){ return std::toupper(ch); });
if ( word.find_first_not_of(ALPHABET) == std::string::npos ) {
candidates.emplace_back(word);
}
}
}
std::shuffle(candidates.begin(), candidates.end(), mersenne_twister);
std::string key = candidates[0];
return key;
}
std::string encrypt(const std::string& plain_text,
const std::vector<std::vector<char>>& polybius,
const std::string& key) {
std::string code = "";
for ( const char& ch : plain_text ) {
for ( int32_t row = 0; row < 6; ++row ) {
for ( int32_t column = 0; column < 6; ++column ) {
if ( polybius[row][column] == ch ) {
code += ADFGVX[row];
code += ADFGVX[column];
}
}
}
}
std::string encrypted = "";
for ( const char& ch : key ) {
for ( uint64_t i = key.find(ch); i < code.length(); i += key.length() ) {
encrypted += code[i];
}
encrypted += " ";
}
return encrypted;
}
std::string decrypt(const std::string& encrypted_text,
const std::vector<std::vector<char>>& polybius,
const std::string& key) {
const uint64_t space_count = std::count(encrypted_text.begin(), encrypted_text.end(), ' ');
const uint64_t code_size = encrypted_text.length() - space_count;
std::vector<std::string> blocks;
std::stringstream stream(encrypted_text);
std:: string word;
while ( stream >> word ) {
blocks.emplace_back(word);
}
std::string code = "";
for ( int32_t i = 0; code.length() < code_size; ++i ) {
for ( const std::string& block : blocks ) {
if ( code.length() < code_size ) {
code += block[i];
}
}
}
std::string plain_text = "";
for ( uint64_t i = 0; i < code_size - 1; i += 2 ) {
int32_t row = ADFGVX.find(code[i]);
int32_t column = ADFGVX.find(code[i + 1]);
plain_text += polybius[row][column];
}
return plain_text;
}
int main() {
const std::vector<std::vector<char>> polybius = initialise_polybius_square();
std::cout << "The 6 x 6 Polybius square:" << std::endl;
std::cout << " | A D F G V X" << std::endl;
std::cout << "--------------" << std::endl;
for ( int32_t row = 0; row < 6; ++row ) {
std::cout << ADFGVX[row] << "|";
for ( int32_t column = 0; column < 6; ++column ) {
std::cout << " " << polybius[row][column];
}
std::cout << std::endl;
}
std::cout << std::endl;
const std::string key = create_key(9);
std::cout << "The key is " << key << std::endl << std::endl;
const std::string plain_text = "ATTACKAT1200AM";
std::cout << "Plain text: " << plain_text <<std::endl << std::endl;
const std::string encrypted_text = encrypt(plain_text, polybius, key);
std::cout << "Encrypted: " << encrypted_text << std::endl << std::endl;
const std::string decrypted_text = decrypt(encrypted_text, polybius, key);
std::cout << "Decrypted: " << decrypted_text << std::endl;
}

View file

@ -0,0 +1,110 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public final class ADFGVXCipher {
public static void main(String[] args) throws IOException {
final char[][] polybius = initialisePolybiusSquare();
System.out.println("The 6 x 6 Polybius square:");
System.out.println(" | A D F G V X");
System.out.println("--------------");
for ( int row = 0; row < 6; row++ ) {
System.out.print(ADFGVX.charAt(row) + "|");
for ( int column = 0; column < 6; column++ ) {
System.out.print(" " + polybius[row][column]);
}
System.out.println();
}
System.out.println();
final String key = createKey(9);
System.out.println("The key is " + key);
System.out.println();
final String plainText = "ATTACKAT1200AM";
System.out.println("Plain text: " + plainText);
System.out.println();
final String encryptedText = encrypt(plainText, polybius, key);
System.out.println("Encrypted: " + encryptedText);
System.out.println();
final String decryptedText = decrypt(encryptedText, polybius, key);
System.out.println("Decrypted: " + decryptedText);
}
private static String encrypt(String plainText, char[][] polybius, String key) {
String code = "";
for ( char ch : plainText.toCharArray() ) {
for ( int row = 0; row < 6; row++ ) {
for ( int column = 0; column < 6; column++ ) {
if ( polybius[row][column] == ch ) {
code += ADFGVX.charAt(row) + "" + ADFGVX.charAt(column);
}
}
}
}
String encrypted = "";
for ( char ch : key.toCharArray() ) {
for ( int i = key.indexOf(ch); i < code.length(); i += key.length() ) {
encrypted += code.charAt(i);
}
encrypted += " ";
}
return encrypted;
}
private static String decrypt(String encryptedText, char[][] polybius, String key) {
final int codeSize = encryptedText.replace(" ", "").length();
String code = "";
for ( int i = 0; code.length() < codeSize; i++ ) {
for ( String block : encryptedText.split(" ") ) {
if ( code.length() < codeSize ) {
code += block.charAt(i);
}
}
}
String plainText = "";
for ( int i = 0; i < codeSize - 1; i += 2 ) {
int row = ADFGVX.indexOf(code.substring(i, i + 1));
int column = ADFGVX.indexOf(code.substring(i + 1, i + 2));
plainText += polybius[row][column];
}
return plainText;
}
// Create a key using a word from the dictionary 'unixdict.txt'
private static String createKey(int size) throws IOException {
if ( size < 7 || size > 12 ) {
throw new AssertionError("Key should contain between 7 and 12 letters, both inclusive.");
}
List<String> candidates = Files.lines(Path.of("unixdict.txt"))
.filter( word -> word.length() == size )
.filter( word -> word.chars().distinct().count() == word.length() )
.filter( word -> word.chars().allMatch(Character::isLetterOrDigit) )
.collect(Collectors.toList());
Collections.shuffle(candidates);
return candidates.get(0).toUpperCase();
}
private static char[][] initialisePolybiusSquare() {
List<String> letters = Arrays.asList("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split(""));
Collections.shuffle(letters);
char[][] result = new char[6][6];
for ( int row = 0; row < 6; row++ ) {
for ( int column = 0; column < 6; column++ ) {
result[row][column] = letters.get(6 * row + column).charAt(0);
}
}
return result;
}
private static final String ADFGVX = "ADFGVX";
}

View file

@ -1,7 +1,7 @@
import "random" for Random
import "/ioutil" for FileUtil
import "/seq" for Lst
import "/str" for Char, Str
import "./ioutil" for FileUtil
import "./seq" for Lst
import "./str" for Char, Str
var rand = Random.new()
var adfgvx = "ADFGVX"