Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
ab98335814
|
|||
|
09eac66309
|
|||
|
d710375595
|
|||
|
8e41b0a2ad
|
|||
|
1943f19b4e
|
|||
|
ec0bf31906
|
|||
|
d12eb91245
|
|||
| f419e6020b | |||
| f9f61081bc | |||
| dc81847123 | |||
| a1ec1909d4 | |||
| 6b420338df | |||
| 01eb38d360 | |||
| cf64e3c933 | |||
| f41e7aefb4 | |||
| c10df8e5df | |||
| b16956b184 | |||
| 028138677b |
2
.mvn/maven.config
Normal file
2
.mvn/maven.config
Normal file
@@ -0,0 +1,2 @@
|
||||
-Dstyle.color=always
|
||||
-T1C
|
||||
165
LICENSE
Normal file
165
LICENSE
Normal file
@@ -0,0 +1,165 @@
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates
|
||||
the terms and conditions of version 3 of the GNU General Public
|
||||
License, supplemented by the additional permissions listed below.
|
||||
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||
General Public License.
|
||||
|
||||
"The Library" refers to a covered work governed by this License,
|
||||
other than an Application or a Combined Work as defined below.
|
||||
|
||||
An "Application" is any work that makes use of an interface provided
|
||||
by the Library, but which is not otherwise based on the Library.
|
||||
Defining a subclass of a class defined by the Library is deemed a mode
|
||||
of using an interface provided by the Library.
|
||||
|
||||
A "Combined Work" is a work produced by combining or linking an
|
||||
Application with the Library. The particular version of the Library
|
||||
with which the Combined Work was made is also called the "Linked
|
||||
Version".
|
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the
|
||||
Corresponding Source for the Combined Work, excluding any source code
|
||||
for portions of the Combined Work that, considered in isolation, are
|
||||
based on the Application, and not on the Linked Version.
|
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the
|
||||
object code and/or source code for the Application, including any data
|
||||
and utility programs needed for reproducing the Combined Work from the
|
||||
Application, but excluding the System Libraries of the Combined Work.
|
||||
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License
|
||||
without being bound by section 3 of the GNU GPL.
|
||||
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a
|
||||
facility refers to a function or data to be supplied by an Application
|
||||
that uses the facility (other than as an argument passed when the
|
||||
facility is invoked), then you may convey a copy of the modified
|
||||
version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to
|
||||
ensure that, in the event an Application does not supply the
|
||||
function or data, the facility still operates, and performs
|
||||
whatever part of its purpose remains meaningful, or
|
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of
|
||||
this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from
|
||||
a header file that is part of the Library. You may convey such object
|
||||
code under terms of your choice, provided that, if the incorporated
|
||||
material is not limited to numerical parameters, data structure
|
||||
layouts and accessors, or small macros, inline functions and templates
|
||||
(ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the
|
||||
Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that,
|
||||
taken together, effectively do not restrict modification of the
|
||||
portions of the Library contained in the Combined Work and reverse
|
||||
engineering for debugging such modifications, if you also do each of
|
||||
the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that
|
||||
the Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
c) For a Combined Work that displays copyright notices during
|
||||
execution, include the copyright notice for the Library among
|
||||
these notices, as well as a reference directing the user to the
|
||||
copies of the GNU GPL and this license document.
|
||||
|
||||
d) Do one of the following:
|
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this
|
||||
License, and the Corresponding Application Code in a form
|
||||
suitable for, and under terms that permit, the user to
|
||||
recombine or relink the Application with a modified version of
|
||||
the Linked Version to produce a modified Combined Work, in the
|
||||
manner specified by section 6 of the GNU GPL for conveying
|
||||
Corresponding Source.
|
||||
|
||||
1) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (a) uses at run time
|
||||
a copy of the Library already present on the user's computer
|
||||
system, and (b) will operate properly with a modified version
|
||||
of the Library that is interface-compatible with the Linked
|
||||
Version.
|
||||
|
||||
e) Provide Installation Information, but only if you would otherwise
|
||||
be required to provide such information under section 6 of the
|
||||
GNU GPL, and only to the extent that such information is
|
||||
necessary to install and execute a modified version of the
|
||||
Combined Work produced by recombining or relinking the
|
||||
Application with a modified version of the Linked Version. (If
|
||||
you use option 4d0, the Installation Information must accompany
|
||||
the Minimal Corresponding Source and Corresponding Application
|
||||
Code. If you use option 4d1, you must provide the Installation
|
||||
Information in the manner specified by section 6 of the GNU GPL
|
||||
for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the
|
||||
Library side by side in a single library together with other library
|
||||
facilities that are not Applications and are not covered by this
|
||||
License, and convey such a combined library under terms of your
|
||||
choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based
|
||||
on the Library, uncombined with any other library facilities,
|
||||
conveyed under the terms of this License.
|
||||
|
||||
b) Give prominent notice with the combined library that part of it
|
||||
is a work based on the Library, and explaining where to find the
|
||||
accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Lesser General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Library as you received it specifies that a certain numbered version
|
||||
of the GNU Lesser General Public License "or any later version"
|
||||
applies to it, you have the option of following the terms and
|
||||
conditions either of that published version or of any later version
|
||||
published by the Free Software Foundation. If the Library as you
|
||||
received it does not specify a version number of the GNU Lesser
|
||||
General Public License, you may choose any version of the GNU Lesser
|
||||
General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide
|
||||
whether future versions of the GNU Lesser General Public License shall
|
||||
apply, that proxy's public statement of acceptance of any version is
|
||||
permanent authorization for you to choose that version for the
|
||||
Library.
|
||||
57
README.md
57
README.md
@@ -1,15 +1,18 @@
|
||||
# Cipher Stream Java
|
||||
This is a Java port of the C++ library [Cipher Stream](https://bitbucket.org/Mattrixwv/CipherStream)
|
||||
|
||||
[](https://sonarqube.mattrixwv.com/dashboard?id=CipherStreamJava)
|
||||
|
||||
This is a Java port of the C++ library [Cipher Stream](https://git.mattrixwv.com/BaseLibraries/CipherStream)
|
||||
|
||||
## Combination
|
||||
|
||||
TODO:
|
||||
|
||||
### ADFGX
|
||||
|
||||
TODO:
|
||||
|
||||
Example:
|
||||
```
|
||||
```txt
|
||||
Square Keyword = SQUAREKEYWORD
|
||||
Keyword = KEYWORD
|
||||
Message = Message to encode
|
||||
@@ -17,114 +20,114 @@ AAGAGADFAGAXXDAXDXADAFAFXDDGDF
|
||||
```
|
||||
|
||||
### ADFGVX
|
||||
|
||||
TODO:
|
||||
|
||||
Example:
|
||||
```
|
||||
```txt
|
||||
Square Keyword = SQUAREKEYWORD
|
||||
Keyword = KEYWORD
|
||||
Message = Message to encode
|
||||
AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA
|
||||
```
|
||||
|
||||
|
||||
## Mono-Substitution
|
||||
|
||||
These are simple ciphers where a single character is transformed into a single or multiple characters the same way each time it is encountered in the input. These are simple ciphers and easy to break.
|
||||
|
||||
### BaseX
|
||||
|
||||
This is not technically a cipher, it simply converts ASCII characters to their numeric values at a given base. The most common use for this is to transform text to binary, octal, or hexadecimal. Strictly speaking it does not require a key, however you do need to include the base that you wish to use.
|
||||
|
||||
Example:
|
||||
```
|
||||
```txt
|
||||
Message to encode ->
|
||||
1001101 1100101 1110011 1110011 1100001 1100111 1100101 100000 1110100 1101111 100000 1100101 1101110 1100011 1101111 1100100 1100101
|
||||
```
|
||||
|
||||
### Baconian
|
||||
|
||||
The Baconian cipher is similar to Base2 encoding (binary) except that the alphabet starts at A = 0, instead of 0's and 1's it uses a's and b's, and I and J share an encoding space, as do U and V. It does not require a key.
|
||||
|
||||
Example:
|
||||
```
|
||||
```txt
|
||||
Message to encode ->
|
||||
ababb aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa
|
||||
```
|
||||
|
||||
### Caesar
|
||||
|
||||
The Caesar cipher offsets the letters in the alphabet by a given amount. It does require a "key" of sorts in that you must set how far the message needs to be shifted.
|
||||
|
||||
Example:
|
||||
```
|
||||
```txt
|
||||
offset = 3
|
||||
Message to encode ->
|
||||
Phvvdjh wr hqfrgh
|
||||
```
|
||||
|
||||
### Atbash
|
||||
|
||||
The Atbash cipher reverses the alphabet for encryption. i.e. a = z, b = y, .... It does not require a key.
|
||||
|
||||
Example:
|
||||
```
|
||||
```txt
|
||||
Message to enocde ->
|
||||
Nvhhztv gl vmxlwv
|
||||
```
|
||||
|
||||
### Vigenere
|
||||
|
||||
TODO: The Vigenere cipher shifts each letter in the input a varied amount based on the letters in the key, with 'a' shifting the letter 0 places, 'b' shifting the letter 1 place, etc. If the message is longer than the key you simply start back over at the beginning of the key. It does require a key word/phrase.
|
||||
|
||||
Example:
|
||||
```
|
||||
```txt
|
||||
key = keyword
|
||||
Message to encode ->
|
||||
Wiqooxh ds cjqfgo
|
||||
```
|
||||
|
||||
### Autokey
|
||||
|
||||
The Autokey cipher works in a similar way to the Vigenere cipher except that instead of reusing the key when you reach the end of it, you tack on the beginning of the message to the key. This way the key does not repeat, making it harder to crack. It does require a key word/phrase.
|
||||
|
||||
Example:
|
||||
```
|
||||
```txt
|
||||
key = keyword
|
||||
Message to encode ->
|
||||
Wiqooxh fs wfcuhx
|
||||
```
|
||||
|
||||
|
||||
## Poly-Substitution
|
||||
|
||||
These ciphers are slightly more complex, encoding multiple letters at the same time making the cipher harder to crack. While many of these keep one letter from always being encoded as another single letter it often does so in such a way that if multiple letters are often seen together they will be encoded the same way. This is offset somewhat because they have to appear in the same location in the cipher text % the number of letters you are encoding by. i.e. If you are using a cipher that encodes with pairs the 'es' in 'mess' and 'these' would not encode to the same thing because 'me' and 'ss' are encoded in the first example while 'th', 'es', 'e_' are encoded in the second, unless there were an odd number of characters before one of these words, shifting one of the words by one place.
|
||||
|
||||
### Columnar
|
||||
|
||||
TODO:
|
||||
|
||||
Example:
|
||||
```
|
||||
```txt
|
||||
key = keyword
|
||||
Message to encode ->
|
||||
Edxeoxm te acxgoxsnxsex
|
||||
```
|
||||
|
||||
### Morse
|
||||
|
||||
This is technically not a cipher, at least any more than writing is encoded speach, as it is meant to enocde information for transmission in a different media. It has been around for a long time and used to be widely known and used, though in recent years it is mainly used by hobbyists. Using this code a letter or number is converted to 1 or more .'s and -'s (dots and dashes or dits and dahs). It does not require a key.
|
||||
|
||||
Example:
|
||||
```
|
||||
```txt
|
||||
Message to encode ->
|
||||
-- . ... ... .- --. . - --- . -. -.-. --- -.. .
|
||||
```
|
||||
|
||||
### Playfair
|
||||
|
||||
TODO:
|
||||
|
||||
Example:
|
||||
```
|
||||
```txt
|
||||
Message to encode ->
|
||||
NKQZPCNDZKDULCGD
|
||||
```
|
||||
|
||||
### PolybiusSquare
|
||||
|
||||
TODO:
|
||||
|
||||
Example:
|
||||
```
|
||||
```txt
|
||||
Message to encode ->
|
||||
41124545233212 5115 124225152212
|
||||
```
|
||||
|
||||
223
pom.xml
223
pom.xml
@@ -14,19 +14,19 @@
|
||||
<developer>
|
||||
<name>Matthew Ellison</name>
|
||||
<email>m_ellison@ymail.com</email>
|
||||
<url>https://bitbucket.org/Mattrixwv</url>
|
||||
<url>https://git.mattrixwv.com/matthew</url>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<scm>
|
||||
<connection>scm:git:git://bitbucket.org/Mattrixwv/Matrix.git</connection>
|
||||
<developerConnection>scm:git:ssh://bitbucket.org:Mattrixwv/Matrix.git</developerConnection>
|
||||
<url>https://bitbucket.org/Mattrixwv/Matrix/src</url>
|
||||
<connection>scm:git:git://git.mattrixwv.com/HomeLab/CipherStreamJava.git</connection>
|
||||
<developerConnection>scm:git:ssh://git.mattrixwv.com/HomeLab/CipherStreamJava.git</developerConnection>
|
||||
<url>https://git.mattrixwv.com/HomeLab/CipherStreamJava</url>
|
||||
</scm>
|
||||
|
||||
<groupId>com.mattrixwv</groupId>
|
||||
<artifactId>cipher-stream-java</artifactId>
|
||||
<version>1.3.5</version>
|
||||
<version>1.4.1-SNAPSHOT</version>
|
||||
|
||||
<name>CipherStreamJava</name>
|
||||
<description>A library to encrypt and decrypt simple ciphers</description>
|
||||
@@ -37,17 +37,16 @@
|
||||
<!--Compile-->
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<java.version>21</java.version>
|
||||
<maven.compiler.source>25</maven.compiler.source>
|
||||
<maven.compiler.target>25</maven.compiler.target>
|
||||
<java.version>25</java.version>
|
||||
|
||||
<!--Sonarqube-->
|
||||
<sonar.java.source>21</sonar.java.source>
|
||||
<sonar.java.source>25</sonar.java.source>
|
||||
<sonar.dependencyCheck.jsonReportPath>target/dependency-check-report.json</sonar.dependencyCheck.jsonReportPath>
|
||||
<sonar.dependencyCheck.htmlReportPath>target/dependency-check-report.html</sonar.dependencyCheck.htmlReportPath>
|
||||
|
||||
<!--Deployment-->
|
||||
<gpg.keyname>3BA6515C8FF145249BEBBEABA52FDEC4259179D4</gpg.keyname>
|
||||
<argLine></argLine>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -55,71 +54,151 @@
|
||||
<dependency>
|
||||
<groupId>com.mattrixwv</groupId>
|
||||
<artifactId>myClasses</artifactId>
|
||||
<version>1.3.5</version>
|
||||
<version>1.4.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mattrixwv</groupId>
|
||||
<artifactId>matrix</artifactId>
|
||||
<version>1.1.3</version>
|
||||
<version>1.3.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>2.0.15</version>
|
||||
<version>2.0.17</version>
|
||||
</dependency>
|
||||
|
||||
<!--Test-->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.10.3</version>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>6.0.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>5.10.3</version>
|
||||
<version>6.0.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>5.12.0</version>
|
||||
<version>5.21.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-junit-jupiter</artifactId>
|
||||
<version>5.12.0</version>
|
||||
<version>5.21.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.simplify4u</groupId>
|
||||
<artifactId>slf4j2-mock</artifactId>
|
||||
<version>2.3.0</version>
|
||||
<version>2.4.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!--Ensure maven is the correct version-->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-enforcer-plugin</artifactId>
|
||||
<version>3.5.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>enforce-maven</id>
|
||||
<goals>
|
||||
<goal>enforce</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>3.9.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>properties</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!--Sonarqube-->
|
||||
<plugin>
|
||||
<groupId>org.owasp</groupId>
|
||||
<artifactId>dependency-check-maven</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>dependency-check</id>
|
||||
<phase>verify</phase>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!--Deployment-->
|
||||
<plugin>
|
||||
<groupId>org.sonatype.central</groupId>
|
||||
<artifactId>central-publishing-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>sign-artifacts</id>
|
||||
<phase>verify</phase>
|
||||
<goals>
|
||||
<goal>sign</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<!--Ensure maven is the correct version-->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-enforcer-plugin</artifactId>
|
||||
<version>3.6.2</version>
|
||||
<configuration>
|
||||
<rules>
|
||||
<requireMavenVersion>
|
||||
@@ -127,61 +206,68 @@
|
||||
</requireMavenVersion>
|
||||
</rules>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.5.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.4.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.13.0</version>
|
||||
<version>3.14.1</version>
|
||||
<configuration>
|
||||
<release>${java.version}</release>
|
||||
<compilerArgs>
|
||||
<arg>-Xlint:all</arg>
|
||||
<arg>-Xlint:-serial</arg>
|
||||
</compilerArgs>
|
||||
<showWarnings>true</showWarnings>
|
||||
<showDeprecation>true</showDeprecation>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
<version>3.5.4</version>
|
||||
<configuration>
|
||||
<argLine>@{argLine} -javaagent:${org.mockito:mockito-core:jar}</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.4.2</version>
|
||||
<version>3.5.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>3.1.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>3.1.2</version>
|
||||
<version>3.1.4</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.7.1</version>
|
||||
<version>3.8.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-site-plugin</artifactId>
|
||||
<version>3.12.1</version>
|
||||
<version>3.21.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
<version>3.6.2</version>
|
||||
<version>3.9.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>versions-maven-plugin</artifactId>
|
||||
<version>2.17.1</version>
|
||||
<version>2.21.0</version>
|
||||
<configuration>
|
||||
<rulesUri>file://${session.executionRootDirectory}/version-rules.xml</rulesUri>
|
||||
</configuration>
|
||||
@@ -189,18 +275,18 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>3.7.1</version>
|
||||
<version>3.9.0</version>
|
||||
</plugin>
|
||||
<!--Sonarqube-->
|
||||
<plugin>
|
||||
<groupId>org.sonarsource.scanner.maven</groupId>
|
||||
<artifactId>sonar-maven-plugin</artifactId>
|
||||
<version>4.0.0.4121</version>
|
||||
<version>5.5.0.6356</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>0.8.12</version>
|
||||
<version>0.8.14</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>jacoco-initialize</id>
|
||||
@@ -221,20 +307,15 @@
|
||||
<plugin>
|
||||
<groupId>org.owasp</groupId>
|
||||
<artifactId>dependency-check-maven</artifactId>
|
||||
<version>10.0.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>none</phase>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<version>12.2.0</version>
|
||||
<configuration>
|
||||
<formats>
|
||||
<format>json</format>
|
||||
<format>html</format>
|
||||
</formats>
|
||||
<nvdApiServerId>nvd</nvdApiServerId>
|
||||
<failBuildOnCVSS>7</failBuildOnCVSS>
|
||||
<ossIndexServerId>ossindex</ossIndexServerId>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
@@ -242,29 +323,27 @@
|
||||
<plugin>
|
||||
<groupId>org.sonatype.central</groupId>
|
||||
<artifactId>central-publishing-maven-plugin</artifactId>
|
||||
<version>0.5.0</version>
|
||||
<version>0.10.0</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<publishingServerId>ossrh</publishingServerId>
|
||||
<publishingServerId>central</publishingServerId>
|
||||
<autoPublish>true</autoPublish>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.sonatype.plugins</groupId>
|
||||
<artifactId>nexus-staging-maven-plugin</artifactId>
|
||||
<version>1.7.0</version>
|
||||
<extensions>true</extensions>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>3.1.4</version>
|
||||
<configuration>
|
||||
<serverId>ossrh</serverId>
|
||||
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
|
||||
<autoReleaseAfterCloase>true</autoReleaseAfterCloase>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
<version>3.4.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
@@ -278,7 +357,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<version>3.12.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
@@ -292,7 +371,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<version>3.2.8</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>sign-artifacts</id>
|
||||
@@ -300,24 +379,10 @@
|
||||
<goals>
|
||||
<goal>sign</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<keyname>${gpg.keyname}</keyname>
|
||||
<passphraseServerId>${gpg.keyname}</passphraseServerId>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<distributionManagement>
|
||||
<snapshotRepository>
|
||||
<id>ossrh</id>
|
||||
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
|
||||
</snapshotRepository>
|
||||
<repository>
|
||||
<id>ossrh</id>
|
||||
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
</project>
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/combination/ADFGVX.java
|
||||
//Mattrixwv
|
||||
// Created: 01-26-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.combination;
|
||||
|
||||
|
||||
@@ -15,23 +11,49 @@ import com.mattrixwv.cipherstream.polysubstitution.Columnar;
|
||||
import com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare;
|
||||
|
||||
|
||||
public class ADFGVX{
|
||||
/**
|
||||
* Implements the ADFGVX cipher, which is a combination of a Polybius square and a columnar transposition cipher.
|
||||
* This class provides methods to encode and decode strings using the ADFGVX cipher.
|
||||
*
|
||||
* <p>
|
||||
* The cipher involves two main steps:
|
||||
* </p>
|
||||
* <ol>
|
||||
* <li>Encoding/decoding with a Polybius square (LargePolybiusSquare)</li>
|
||||
* <li>Encoding/decoding with a columnar transposition cipher (Columnar)</li>
|
||||
* </ol>
|
||||
*/
|
||||
public final class ADFGVX{
|
||||
private static final Logger logger = LoggerFactory.getLogger(ADFGVX.class);
|
||||
//Fields
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The string that is output after encoding/decoding
|
||||
protected String squareKeyword; //The keyword used in the Polybius Square
|
||||
protected String keyword; //The Keyword used in the Columnar cipher
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//Internal ciphers
|
||||
protected LargePolybiusSquare largePolybiusSquare; //The first step in encoding
|
||||
protected Columnar columnar; //The second step in encoding
|
||||
//?Fields
|
||||
/** The string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The string that is output after encoding/decoding */
|
||||
protected String outputString;
|
||||
/** The keyword used in the Polybius Square */
|
||||
protected String squareKeyword;
|
||||
/** The Keyword used in the Columnar cipher */
|
||||
protected String keyword;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
//?Internal ciphers
|
||||
/** The first step in encoding */
|
||||
protected LargePolybiusSquare largePolybiusSquare;
|
||||
/** The second step in encoding */
|
||||
protected Columnar columnar;
|
||||
|
||||
|
||||
//Ensures Polybius keyword constraints
|
||||
/**
|
||||
* Sets the Polybius square keyword and validates it.
|
||||
*
|
||||
* @param squareKeyword the keyword for the Polybius square
|
||||
* @throws InvalidKeywordException if the keyword is null
|
||||
*/
|
||||
protected void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{
|
||||
if(squareKeyword == null){
|
||||
throw new InvalidKeywordException("Square Keyword cannot be null");
|
||||
@@ -40,7 +62,13 @@ public class ADFGVX{
|
||||
logger.debug("squareKeyword '{}'", squareKeyword);
|
||||
this.squareKeyword = squareKeyword;
|
||||
}
|
||||
//Ensures Columnar keyword constraints
|
||||
|
||||
/**
|
||||
* Sets the columnar cipher keyword and validates it.
|
||||
*
|
||||
* @param keyword the keyword for the columnar cipher
|
||||
* @throws InvalidKeywordException if the keyword is null
|
||||
*/
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
if(keyword == null){
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
@@ -49,7 +77,14 @@ public class ADFGVX{
|
||||
logger.debug("keyword '{}'", keyword);
|
||||
this.keyword = keyword;
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
|
||||
|
||||
/**
|
||||
* Sets and sanitizes the input string according to the preservation settings.
|
||||
*
|
||||
* @param inputString the string to be processed
|
||||
* @throws InvalidInputException if the input string is null or blank after processing
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -81,7 +116,11 @@ public class ADFGVX{
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
//Format the output string with capitals, symbols, and numbers that are in the input string
|
||||
|
||||
|
||||
/**
|
||||
* Formats the output string to match the original input string when encoding.
|
||||
*/
|
||||
protected void formatOutputStringEncode(){
|
||||
logger.debug("Formatting output string to match input string");
|
||||
|
||||
@@ -111,6 +150,9 @@ public class ADFGVX{
|
||||
outputString = output.toString();
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
/**
|
||||
* Formats the output string to match the original input string when decoding.
|
||||
*/
|
||||
protected void formatOutputStringDecode(){
|
||||
logger.debug("Formatting output string to match input string");
|
||||
|
||||
@@ -141,7 +183,16 @@ public class ADFGVX{
|
||||
outputString = output.toString();
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
|
||||
/**
|
||||
* Encodes the input string using the Polybius square and columnar cipher.
|
||||
* Stores the result in outputString.
|
||||
*
|
||||
* @return the encoded string
|
||||
* @throws InvalidCharacterException if there are invalid characters in the ciphers
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
* @throws InvalidKeywordException if any of the keywords are invalid
|
||||
*/
|
||||
protected String encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{
|
||||
//Encode the input with polybius
|
||||
logger.debug("Encoding using Polybius Square");
|
||||
@@ -160,7 +211,16 @@ public class ADFGVX{
|
||||
|
||||
return outputString;
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
|
||||
/**
|
||||
* Decodes the input string using the columnar cipher and Polybius square.
|
||||
* Stores the result in outputString.
|
||||
*
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if any of the keywords are invalid
|
||||
* @throws InvalidCharacterException if there are invalid characters in the ciphers
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected String decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{
|
||||
//Decode the input with columnar
|
||||
logger.debug("Decoding using columnar");
|
||||
@@ -181,13 +241,27 @@ public class ADFGVX{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code ADFGVX} instance with default settings:
|
||||
* capitals, whitespace, and symbols are not preserved.
|
||||
*
|
||||
* @throws InvalidCharacterException if there are invalid characters in the ciphers
|
||||
*/
|
||||
public ADFGVX() throws InvalidCharacterException{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code ADFGVX} instance with specified settings for preserving capitals, whitespace, and symbols.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capital letters in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
* @throws InvalidCharacterException if there are invalid characters in the ciphers
|
||||
*/
|
||||
public ADFGVX(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -195,14 +269,37 @@ public class ADFGVX{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using keyword and returns the result
|
||||
|
||||
/**
|
||||
* Encodes the provided input string using the specified keywords and returns the encoded result.
|
||||
*
|
||||
* @param squareKeyword the keyword for the Polybius square
|
||||
* @param keyword the keyword for the columnar cipher
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if any of the keywords are invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
* @throws InvalidCharacterException if the input contains invalid characters
|
||||
*/
|
||||
public String encode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
setSquareKeyword(squareKeyword);
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
return encode();
|
||||
}
|
||||
//Decodes inputString using keyword and returns the result
|
||||
|
||||
|
||||
/**
|
||||
* Decodes the provided input string using the specified keywords and returns the decoded result.
|
||||
*
|
||||
* @param squareKeyword the keyword for the Polybius square
|
||||
* @param keyword the keyword for the columnar cipher
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if any of the keywords are invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
* @throws InvalidCharacterException if the input contains invalid characters
|
||||
*/
|
||||
public String decode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
setSquareKeyword(squareKeyword);
|
||||
setKeyword(keyword);
|
||||
@@ -210,20 +307,47 @@ public class ADFGVX{
|
||||
return decode();
|
||||
}
|
||||
|
||||
//Getters
|
||||
|
||||
//?Getters
|
||||
/**
|
||||
* Returns the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Returns the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Returns the current Polybius square keyword.
|
||||
*
|
||||
* @return the Polybius square keyword
|
||||
*/
|
||||
public String getSquareKeyword(){
|
||||
return squareKeyword;
|
||||
}
|
||||
/**
|
||||
* Returns the current columnar cipher keyword.
|
||||
*
|
||||
* @return the columnar cipher keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
|
||||
|
||||
/**
|
||||
* Resets all fields to their default values and re-initializes the internal ciphers.
|
||||
*
|
||||
* @throws InvalidCharacterException if there are invalid characters in the ciphers
|
||||
*/
|
||||
public void reset() throws InvalidCharacterException{
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/combination/ADFGX.java
|
||||
//Mattrixwv
|
||||
// Created: 01-25-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.combination;
|
||||
|
||||
|
||||
@@ -15,24 +11,50 @@ import com.mattrixwv.cipherstream.polysubstitution.Columnar;
|
||||
import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
|
||||
|
||||
|
||||
public class ADFGX{
|
||||
/**
|
||||
* Implements the ADFGX cipher, which is a combination of a Polybius square and a columnar transposition cipher.
|
||||
* This class provides methods to encode and decode strings using the ADFGX cipher.
|
||||
*
|
||||
* <p>
|
||||
* The cipher involves two main steps:
|
||||
* </p>
|
||||
* <ol>
|
||||
* <li>Encoding/decoding with a Polybius square (PolybiusSquare)</li>
|
||||
* <li>Encoding/decoding with a columnar transposition cipher (Columnar)</li>
|
||||
* </ol>
|
||||
*/
|
||||
public final class ADFGX{
|
||||
private static final Logger logger = LoggerFactory.getLogger(ADFGX.class);
|
||||
|
||||
//Internal fields
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The string that is output after encoding/decoding
|
||||
protected String squareKeyword; //The keyword used in the Polybius Square
|
||||
protected String keyword; //The keyword used in the Columnar cipher
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//Internal ciphers
|
||||
protected PolybiusSquare polybiusSquare; //The first step in encoding
|
||||
protected Columnar columnar; //The second step in encoding
|
||||
//?Internal fields
|
||||
/** The string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The string that is output after encoding/decoding */
|
||||
protected String outputString;
|
||||
/** The keyword used in the Polybius Square */
|
||||
protected String squareKeyword;
|
||||
/** The keyword used in the Columnar cipher */
|
||||
protected String keyword;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
//?Internal ciphers
|
||||
/** The first step in encoding */
|
||||
protected PolybiusSquare polybiusSquare;
|
||||
/** The second step in encoding */
|
||||
protected Columnar columnar;
|
||||
|
||||
|
||||
//Ensures Polybius keyword constraints
|
||||
/**
|
||||
* Sets the Polybius square keyword and validates it.
|
||||
*
|
||||
* @param squareKeyword the keyword for the Polybius square
|
||||
* @throws InvalidKeywordException if the keyword is null
|
||||
*/
|
||||
protected void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{
|
||||
if(squareKeyword == null){
|
||||
throw new InvalidKeywordException("Square keyword cannot be null");
|
||||
@@ -41,7 +63,12 @@ public class ADFGX{
|
||||
logger.debug("Square keyword '{}'", squareKeyword);
|
||||
this.squareKeyword = squareKeyword;
|
||||
}
|
||||
//Ensures Columnar keyword constraints
|
||||
/**
|
||||
* Sets the columnar cipher keyword and validates it.
|
||||
*
|
||||
* @param keyword the keyword for the columnar cipher
|
||||
* @throws InvalidKeywordException if the keyword is null
|
||||
*/
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
if(keyword == null){
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
@@ -50,7 +77,12 @@ public class ADFGX{
|
||||
logger.debug("Keyword '{}'", keyword);
|
||||
this.keyword = keyword;
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
/**
|
||||
* Sets and sanitizes the input string according to the preservation settings.
|
||||
*
|
||||
* @param inputString the string to be processed
|
||||
* @throws InvalidInputException if the input string is null or blank after processing
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -80,7 +112,9 @@ public class ADFGX{
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
//Format the output string with capitals, symbols, and numbers that are in the input string
|
||||
/**
|
||||
* Formats the output string to match the original input string when encoding.
|
||||
*/
|
||||
protected void formatOutputStringEncode(){
|
||||
logger.debug("Formatting output string to match input string");
|
||||
|
||||
@@ -110,6 +144,9 @@ public class ADFGX{
|
||||
outputString = output.toString();
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
/**
|
||||
* Formats the output string to match the original input string when decoding.
|
||||
*/
|
||||
protected void formatOutputStringDecode(){
|
||||
logger.debug("Formatting output string to match input string");
|
||||
StringBuilder output = new StringBuilder();
|
||||
@@ -139,7 +176,13 @@ public class ADFGX{
|
||||
outputString = output.toString();
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the Polybius square and columnar cipher.
|
||||
*
|
||||
* @throws InvalidCharacterException if there are invalid characters in the ciphers
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
* @throws InvalidKeywordException if any of the keywords are invalid
|
||||
*/
|
||||
protected void encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{
|
||||
//Encode the input with polybius
|
||||
logger.debug("Encoding using Polybius Square");
|
||||
@@ -158,7 +201,13 @@ public class ADFGX{
|
||||
//Add whatever is needed to the output string
|
||||
formatOutputStringEncode();
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the columnar cipher and Polybius square.
|
||||
*
|
||||
* @throws InvalidKeywordException if any of the keywords are invalid
|
||||
* @throws InvalidCharacterException if there are invalid characters in the ciphers
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{
|
||||
//Decode the input with columnar
|
||||
logger.debug("Decoding using columnar");
|
||||
@@ -179,13 +228,27 @@ public class ADFGX{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code ADFGX} instance with default settings:
|
||||
* capitals, whitespace, and symbols are not preserved.
|
||||
*
|
||||
* @throws InvalidCharacterException if there are invalid characters in the ciphers
|
||||
*/
|
||||
public ADFGX() throws InvalidCharacterException{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code ADFGX} instance with specified settings for preserving capitals, whitespace, and symbols.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capital letters in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
* @throws InvalidCharacterException if there are invalid characters in the ciphers
|
||||
*/
|
||||
public ADFGX(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -193,7 +256,17 @@ public class ADFGX{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using keyword and returns the result
|
||||
/**
|
||||
* Encodes the provided input string using the specified keywords and returns the encoded result.
|
||||
*
|
||||
* @param squareKeyword the keyword for the Polybius square
|
||||
* @param keyword the keyword for the columnar cipher
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if any of the keywords are invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
* @throws InvalidCharacterException if the input contains invalid characters
|
||||
*/
|
||||
public String encode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
setSquareKeyword(squareKeyword);
|
||||
setKeyword(keyword);
|
||||
@@ -201,7 +274,17 @@ public class ADFGX{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using keyword and returns the result
|
||||
/**
|
||||
* Decodes the provided input string using the specified keywords and returns the decoded result.
|
||||
*
|
||||
* @param squareKeyword the keyword for the Polybius square
|
||||
* @param keyword the keyword for the columnar cipher
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if any of the keywords are invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
* @throws InvalidCharacterException if the input contains invalid characters
|
||||
*/
|
||||
public String decode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
setSquareKeyword(squareKeyword);
|
||||
setKeyword(keyword);
|
||||
@@ -210,20 +293,44 @@ public class ADFGX{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Returns the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Returns the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Returns the current Polybius square keyword.
|
||||
*
|
||||
* @return the Polybius square keyword
|
||||
*/
|
||||
public String getSquareKeyword(){
|
||||
return squareKeyword;
|
||||
}
|
||||
/**
|
||||
* Returns the current columnar cipher keyword.
|
||||
*
|
||||
* @return the columnar cipher keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all fields to their default values and reinitializes the internal ciphers.
|
||||
*
|
||||
* @throws InvalidCharacterException if there are invalid characters in the ciphers
|
||||
*/
|
||||
public void reset() throws InvalidCharacterException{
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
@@ -1,20 +1,38 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Exceptions/InvalidBaseException.java
|
||||
//Mattrixwv
|
||||
// Created: 01-09-22
|
||||
//Modified: 01-09-22
|
||||
package com.mattrixwv.cipherstream.exceptions;
|
||||
|
||||
|
||||
/**
|
||||
* Thrown to indicate that a problem has occurred related to the base or group size.
|
||||
*/
|
||||
public class InvalidBaseException extends RuntimeException{
|
||||
/**
|
||||
* Constructs a new {@code InvalidBaseException} with {@code null} as its detail message.
|
||||
*/
|
||||
public InvalidBaseException(){
|
||||
super();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidBaseException} with the specified detail message.
|
||||
*
|
||||
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method
|
||||
*/
|
||||
public InvalidBaseException(String message){
|
||||
super(message);
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidBaseException} with the specified cause.
|
||||
*
|
||||
* @param error the cause, which is saved for later retrieval by the {@link #getCause()} method
|
||||
*/
|
||||
public InvalidBaseException(Throwable error){
|
||||
super(error);
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidBaseException} with the specified detail message and cause.
|
||||
*
|
||||
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method
|
||||
* @param error the cause, which is saved for later retrieval by the {@link #getCause()} method
|
||||
*/
|
||||
public InvalidBaseException(String message, Throwable error){
|
||||
super(message, error);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,38 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Exceptions/InvalidCharacterException.java
|
||||
//Mattrixwv
|
||||
// Created: 01-04-22
|
||||
//Modified: 01-04-22
|
||||
package com.mattrixwv.cipherstream.exceptions;
|
||||
|
||||
|
||||
/**
|
||||
* Thrown to indicate that a problem has occurred related to an invalid character.
|
||||
*/
|
||||
public class InvalidCharacterException extends RuntimeException{
|
||||
/**
|
||||
* Constructs a new {@code InvalidCharacterException} with {@code null} as its detail message.
|
||||
*/
|
||||
public InvalidCharacterException(){
|
||||
super();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidCharacterException} with the specified detail message.
|
||||
*
|
||||
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method
|
||||
*/
|
||||
public InvalidCharacterException(String message){
|
||||
super(message);
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidCharacterException} with the specified cause.
|
||||
*
|
||||
* @param error the cause, which is saved for later retrieval by the {@link #getCause()} method
|
||||
*/
|
||||
public InvalidCharacterException(Throwable error){
|
||||
super(error);
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidCharacterException} with the specified detail message and cause.
|
||||
*
|
||||
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method
|
||||
* @param error the cause, which is saved for later retrieval by the {@link #getCause()} method
|
||||
*/
|
||||
public InvalidCharacterException(String message, Throwable error){
|
||||
super(message, error);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,38 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Exceptions/InvalidInputException.java
|
||||
//Mattrixwv
|
||||
// Created: 01-09-22
|
||||
//Modified: 01-09-22
|
||||
package com.mattrixwv.cipherstream.exceptions;
|
||||
|
||||
|
||||
/**
|
||||
* Thrown to indicate that a problem has occurred related to invalid input.
|
||||
*/
|
||||
public class InvalidInputException extends RuntimeException{
|
||||
/**
|
||||
* Constructs a new {@code InvalidInputException} with {@code null} as its detail message.
|
||||
*/
|
||||
public InvalidInputException(){
|
||||
super();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidInputException} with the specified detail message.
|
||||
*
|
||||
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method
|
||||
*/
|
||||
public InvalidInputException(String message){
|
||||
super(message);
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidInputException} with the specified cause.
|
||||
*
|
||||
* @param error the cause, which is saved for later retrieval by the {@link #getCause()} method
|
||||
*/
|
||||
public InvalidInputException(Throwable error){
|
||||
super(error);
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidInputException} with the specified detail message and cause.
|
||||
*
|
||||
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method
|
||||
* @param error the cause, which is saved for later retrieval by the {@link #getCause()} method
|
||||
*/
|
||||
public InvalidInputException(String message, Throwable error){
|
||||
super(message, error);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,38 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/exceptions/InvalidKeyException.java
|
||||
//Matrixwv
|
||||
// Created: 07-09-22
|
||||
//Modified: 07-09-22
|
||||
package com.mattrixwv.cipherstream.exceptions;
|
||||
|
||||
|
||||
/**
|
||||
* Thrown to indicate that a problem has occurred related to an invalid key.
|
||||
*/
|
||||
public class InvalidKeyException extends RuntimeException{
|
||||
/**
|
||||
* Constructs a new {@code InvalidKeyException} with {@code null} as its detail message.
|
||||
*/
|
||||
public InvalidKeyException(){
|
||||
super();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidKeyException} with the specified detail message.
|
||||
*
|
||||
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method
|
||||
*/
|
||||
public InvalidKeyException(String message){
|
||||
super(message);
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidKeyException} with the specified cause.
|
||||
*
|
||||
* @param error the cause, which is saved for later retrieval by the {@link #getCause()} method
|
||||
*/
|
||||
public InvalidKeyException(Throwable error){
|
||||
super(error);
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidKeyException} with the specified detail message and cause.
|
||||
*
|
||||
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method
|
||||
* @param error the cause, which is saved for later retrieval by the {@link #getCause()} method
|
||||
*/
|
||||
public InvalidKeyException(String message, Throwable error){
|
||||
super(message, error);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,38 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Exceptions/InvalidKeywordException.java
|
||||
//Mattrixwv
|
||||
// Created: 01-09-22
|
||||
//Modified: 01-09-22
|
||||
package com.mattrixwv.cipherstream.exceptions;
|
||||
|
||||
|
||||
/**
|
||||
* Thrown to indicate that a problem has occurred related to an invalid keyword.
|
||||
*/
|
||||
public class InvalidKeywordException extends RuntimeException{
|
||||
/**
|
||||
* Constructs a new {@code InvalidKeywordException} with {@code null} as its detail message.
|
||||
*/
|
||||
public InvalidKeywordException(){
|
||||
super();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidKeywordException} with the specified detail message.
|
||||
*
|
||||
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method
|
||||
*/
|
||||
public InvalidKeywordException(String message){
|
||||
super(message);
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidKeywordException} with the specified cause.
|
||||
*
|
||||
* @param error the cause, which is saved for later retrieval by the {@link #getCause()} method
|
||||
*/
|
||||
public InvalidKeywordException(Throwable error){
|
||||
super(error);
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code InvalidKeywordException} with the specified detail message and cause.
|
||||
*
|
||||
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method
|
||||
* @param error the cause, which is saved for later retrieval by the {@link #getCause()} method
|
||||
*/
|
||||
public InvalidKeywordException(String message, Throwable error){
|
||||
super(message, error);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Affine.java
|
||||
//Mattrixwv
|
||||
// Created: 01-26-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -13,20 +9,44 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Affine{
|
||||
/**
|
||||
* Implements the Affine cipher, which is a monoalphabetic substitution cipher based on linear algebra.
|
||||
* This class provides methods to encode and decode strings using the Affine cipher with a specified key.
|
||||
*
|
||||
* <p>
|
||||
* The Affine cipher uses two keys:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li><strong>Key1</strong>: The multiplicative key (must be relatively prime to 26)</li>
|
||||
* <li><strong>Key2</strong>: The additive key</li>
|
||||
* </ul>
|
||||
*/
|
||||
public final class Affine{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Affine.class);
|
||||
//Fields
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The string that is output after encoding/decoding
|
||||
protected int key1; //The multiplicative key. Key1 must be relatively prime to 26
|
||||
protected int key2; //The additive key
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
//?Fields
|
||||
/** The string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The string that is output after encoding/decoding */
|
||||
protected String outputString;
|
||||
/** The multiplicative key. Key1 must be relatively prime to 26 */
|
||||
protected int key1;
|
||||
/** The additive key */
|
||||
protected int key2;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
|
||||
|
||||
//Ensures key1 constraints
|
||||
/**
|
||||
* Sets the multiplicative key and validates it.
|
||||
*
|
||||
* @param key1 the multiplicative key
|
||||
* @throws InvalidKeywordException if the key1 is not relatively prime to 26
|
||||
*/
|
||||
protected void setKey1(int key1) throws InvalidKeywordException{
|
||||
logger.debug("Setting key1 {}", key1);
|
||||
|
||||
@@ -48,7 +68,11 @@ public class Affine{
|
||||
|
||||
logger.debug("Cleaned key1 {}", key1);
|
||||
}
|
||||
//Ensures key2 constraints
|
||||
/**
|
||||
* Sets the additive key.
|
||||
*
|
||||
* @param key2 the additive key
|
||||
*/
|
||||
protected void setKey2(int key2){
|
||||
logger.debug("Setting key2 {}", key2);
|
||||
|
||||
@@ -65,7 +89,12 @@ public class Affine{
|
||||
|
||||
logger.debug("Cleaned key2 {}", key2);
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
/**
|
||||
* Sets and sanitizes the input string according to the preservation settings.
|
||||
*
|
||||
* @param inputString the string to be processed
|
||||
* @throws InvalidInputException if the input string is null or blank after processing
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not be null");
|
||||
@@ -97,7 +126,9 @@ public class Affine{
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the affine cipher.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -138,7 +169,9 @@ public class Affine{
|
||||
outputString = output.toString();
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the affine cipher.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -194,13 +227,24 @@ public class Affine{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Affine} instance with default settings:
|
||||
* capitals, symbols, and whitespace are not preserved.
|
||||
*/
|
||||
public Affine(){
|
||||
preserveCapitals = false;
|
||||
preserveSymbols = false;
|
||||
preserveWhitespace = false;
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Affine} instance with specified settings for preserving capitals, symbols, and whitespace.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capital letters in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
*/
|
||||
public Affine(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
@@ -208,7 +252,16 @@ public class Affine{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using key1 and key2 and returns the result
|
||||
/**
|
||||
* Encodes the provided input string using the specified keys and returns the encoded result.
|
||||
*
|
||||
* @param key1 the multiplicative key (must be relatively prime to 26)
|
||||
* @param key2 the additive key
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if key1 is not relatively prime to 26
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKey1(key1);
|
||||
setKey2(key2);
|
||||
@@ -216,7 +269,16 @@ public class Affine{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using key1 and key2 and returns the result
|
||||
/**
|
||||
* Decodes the provided input string using the specified keys and returns the decoded result.
|
||||
*
|
||||
* @param key1 the multiplicative key (must be relatively prime to 26)
|
||||
* @param key2 the additive key
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if key1 is not relatively prime to 26
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKey1(key1);
|
||||
setKey2(key2);
|
||||
@@ -225,20 +287,42 @@ public class Affine{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Returns the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Returns the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Returns the current multiplicative key.
|
||||
*
|
||||
* @return the multiplicative key
|
||||
*/
|
||||
public int getKey1(){
|
||||
return key1;
|
||||
}
|
||||
/**
|
||||
* Returns the current additive key.
|
||||
*
|
||||
* @return the additive key
|
||||
*/
|
||||
public int getKey2(){
|
||||
return key2;
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
/**
|
||||
* Resets all fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Atbash.java
|
||||
//Mattrixwv
|
||||
// Created: 07-25-21
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -11,18 +7,34 @@ import org.slf4j.LoggerFactory;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class Atbash{
|
||||
/**
|
||||
* Implements the Atbash cipher, a simple substitution cipher where each letter of the alphabet is mapped to its reverse.
|
||||
* For example, 'A' is mapped to 'Z', 'B' to 'Y', and so on.
|
||||
* This class provides methods to encode and decode strings using the Atbash cipher.
|
||||
*
|
||||
* <p>
|
||||
* The Atbash cipher is symmetric, meaning that encoding and decoding are the same operation.
|
||||
* </p>
|
||||
*/
|
||||
public final class Atbash{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Atbash.class);
|
||||
//Fields
|
||||
protected String inputString; //Holds the string that needs encoded or decoded
|
||||
protected String outputString; //The encoded/decoded string
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//?Fields
|
||||
/** Holds the string that needs encoded or decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded string */
|
||||
protected String outputString;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
|
||||
|
||||
//Encodes inputString and stores in outputString
|
||||
/**
|
||||
* Encodes the input string using the Atbash cipher.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
StringBuilder output = new StringBuilder();
|
||||
@@ -57,7 +69,12 @@ public class Atbash{
|
||||
outputString = output.toString();
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
//Removes all invalid characters and sets inputString
|
||||
/**
|
||||
* Sets and sanitizes the input string according to the preservation settings.
|
||||
*
|
||||
* @param inputString the string to be processed
|
||||
* @throws InvalidInputException if the input string is null or blank after processing
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -95,13 +112,24 @@ public class Atbash{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Atbash} instance with default settings:
|
||||
* capitals, symbols, and whitespace are not preserved.
|
||||
*/
|
||||
public Atbash(){
|
||||
reset();
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Atbash} instance with specified settings for preserving capitals, whitespace, and symbols.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capital letters in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
*/
|
||||
public Atbash(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
reset();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
@@ -109,7 +137,13 @@ public class Atbash{
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
}
|
||||
|
||||
//Encodes inputString and returns the result
|
||||
/**
|
||||
* Encodes the provided input string using the Atbash cipher and returns the encoded result.
|
||||
*
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String inputString) throws InvalidInputException{
|
||||
//Make sure everything is empty before you begin
|
||||
reset();
|
||||
@@ -117,19 +151,38 @@ public class Atbash{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString and returns the result
|
||||
/**
|
||||
* Decodes the provided input string using the Atbash cipher and returns the decoded result.
|
||||
* Since the Atbash cipher is symmetric, this method performs the same operation as encoding.
|
||||
*
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String inputString) throws InvalidInputException{
|
||||
return encode(inputString);
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Returns the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Returns the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
@@ -1,7 +1,23 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
|
||||
//Mattrixwv
|
||||
// Created: 07-25-21
|
||||
//Modified: 05-04-23
|
||||
//Modified: 08-11-24
|
||||
/*
|
||||
Copyright (C) 2024 Mattrixwv
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -12,11 +28,28 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
/**
|
||||
* Implements the Autokey cipher, an extension of the Vigenère cipher that uses a keyword combined with the plaintext itself as the key.
|
||||
* The Autokey cipher adds the plaintext to the end of the keyword to create a longer key, which helps to make the encryption stronger.
|
||||
* This class inherits from the {@code Vigenere} class and overrides methods to handle encoding and decoding with the Autokey cipher.
|
||||
*
|
||||
* <p>
|
||||
* The Autokey cipher is symmetric, meaning that encoding and decoding are essentially the same process, but with a different key setup for decoding.
|
||||
* </p>
|
||||
*/
|
||||
public class Autokey extends Vigenere{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Autokey.class);
|
||||
|
||||
|
||||
//Special rules for setting the strings for encoding
|
||||
/**
|
||||
* Sets up the keyword and input string for encoding, generating a longer key that includes the plaintext.
|
||||
* This method is used internally to prepare the cipher for encoding.
|
||||
*
|
||||
* @param keyword the keyword used for encoding
|
||||
* @param inputString the string to be encoded
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
logger.debug("Setting fields for encoding");
|
||||
|
||||
@@ -44,7 +77,15 @@ public class Autokey extends Vigenere{
|
||||
offset.clear();
|
||||
setOffset();
|
||||
}
|
||||
//Setting the strings for decoding
|
||||
/**
|
||||
* Sets up the keyword and input string for decoding. The keyword is used to decode the input string.
|
||||
* This method is used internally to prepare the cipher for decoding.
|
||||
*
|
||||
* @param keyword the keyword used for decoding
|
||||
* @param inputString the string to be decoded
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void decodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
logger.debug("Setting fields for decoding");
|
||||
|
||||
@@ -56,7 +97,10 @@ public class Autokey extends Vigenere{
|
||||
logger.debug("Setting input string");
|
||||
setInputString(inputString);
|
||||
}
|
||||
//Decodes the inputString
|
||||
/**
|
||||
* Decodes the input string using the Autokey cipher.
|
||||
* This method is overridden to handle decoding with the Autokey cipher, which involves using the key and updating it with decoded characters.
|
||||
*/
|
||||
@Override
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
@@ -113,14 +157,32 @@ public class Autokey extends Vigenere{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Autokey} instance with default settings for preserving capitals, whitespace, and symbols.
|
||||
*/
|
||||
public Autokey(){
|
||||
super();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Autokey} instance with specified settings for preserving capitals, whitespace, and symbols.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capital letters in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
*/
|
||||
public Autokey(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
super(preserveCapitals, preserveWhitespace, preserveSymbols);
|
||||
}
|
||||
//Encodes inputString using the Autokey cipher
|
||||
/**
|
||||
* Encodes the input string using the Autokey cipher with the provided keyword.
|
||||
*
|
||||
* @param keyword the keyword used for encoding
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
@Override
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
reset();
|
||||
@@ -128,7 +190,15 @@ public class Autokey extends Vigenere{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using the Autokey cipher
|
||||
/**
|
||||
* Decodes the input string using the Autokey cipher with the provided keyword.
|
||||
*
|
||||
* @param keyword the keyword used for decoding
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
@Override
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
reset();
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java
|
||||
//Mattrixwv
|
||||
// Created: 01-12-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -16,20 +12,38 @@ import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class Baconian{
|
||||
/**
|
||||
* Implements the Baconian cipher, a method of steganography where each letter of the alphabet is represented by a unique sequence of five characters ('a' or 'b').
|
||||
* The Baconian cipher is a simple substitution cipher that can encode and decode text based on these sequences.
|
||||
*
|
||||
* <p>
|
||||
* The cipher uses a predefined list of five-character strings to represent each letter of the alphabet (A-Z). The input string is converted to these sequences for encoding,
|
||||
* and sequences are converted back to letters for decoding.
|
||||
* </p>
|
||||
*/
|
||||
public final class Baconian{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Baconian.class);
|
||||
|
||||
//Conversions
|
||||
//?Conversions
|
||||
/** Predefined code for Baconian cipher (5-character strings for A-Z) */
|
||||
protected static final ArrayList<String> code = new ArrayList<>(Arrays.asList(
|
||||
"aaaaa", "aaaab", "aaaba", "aaabb", "aabaa", "aabab", "aabba","aabbb", "abaaa", "abaaa", "abaab", "ababa", "ababb", //A-M
|
||||
"abbaa", "abbab", "abbba", "abbbb", "baaaa", "baaab", "baaba", "baabb", "baabb", "babaa", "babab", "babba", "babbb" //N-Z
|
||||
));
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The encoded/decoded string
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
/** The string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded string */
|
||||
protected String outputString;
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
|
||||
|
||||
//Sets the input string
|
||||
/**
|
||||
* Sets the input string for encoding, removing whitespace and symbols, and handling capitalization based on the flag.
|
||||
*
|
||||
* @param inputString the string to be encoded
|
||||
* @throws InvalidInputException if the input string is null, empty, or invalid
|
||||
*/
|
||||
protected void setInputStringEncode(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -53,6 +67,13 @@ public class Baconian{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Sets the input string for decoding, ensuring it contains only valid Baconian characters (a's and b's) with a length of 5.
|
||||
*
|
||||
* @param inputString the string to be decoded
|
||||
* @throws InvalidCharacterException if the input string contains invalid Baconian characters
|
||||
* @throws InvalidInputException if the input string is null or empty
|
||||
*/
|
||||
protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -91,7 +112,10 @@ public class Baconian{
|
||||
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the Baconian cipher.
|
||||
* Each character in the input string is converted to its corresponding Baconian sequence.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
StringJoiner output = new StringJoiner(" ");
|
||||
@@ -121,7 +145,10 @@ public class Baconian{
|
||||
outputString = output.toString();
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the Baconian cipher.
|
||||
* Each Baconian sequence in the input string is converted back to its corresponding character.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -157,24 +184,45 @@ public class Baconian{
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Baconian} instance with default settings for preserving capitals.
|
||||
*/
|
||||
public Baconian(){
|
||||
reset();
|
||||
preserveCapitals = false;
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Baconian} instance with a specified setting for preserving capitals.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capital letters in the output
|
||||
*/
|
||||
public Baconian(boolean preserveCapitals){
|
||||
reset();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
}
|
||||
|
||||
//Sets the inputString and encodes the message
|
||||
/**
|
||||
* Encodes the input string using the Baconian cipher.
|
||||
*
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String inputString) throws InvalidInputException{
|
||||
reset();
|
||||
setInputStringEncode(inputString);
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Sets the inputString and decodes the message
|
||||
/**
|
||||
* Decodes the input string using the Baconian cipher.
|
||||
*
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidCharacterException if the input string contains invalid Baconian characters
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setInputStringDecode(inputString);
|
||||
@@ -182,14 +230,26 @@ public class Baconian{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Gets the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets the input and output strings to empty.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/BaseX.java
|
||||
//Mattrixwv
|
||||
// Created: 01-08-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -15,16 +11,35 @@ import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class BaseX{
|
||||
/**
|
||||
* A class for encoding and decoding strings using a specified numerical base.
|
||||
* The BaseX class allows encoding and decoding of strings where characters are represented in a given base.
|
||||
* The base can be set to any value between Character.MIN_RADIX and Character.MAX_RADIX.
|
||||
*
|
||||
* <p>
|
||||
* This class supports encoding and decoding of ASCII characters into their base-X representations,
|
||||
* where X is the base provided by the user. It ensures that input strings are valid and within the acceptable range
|
||||
* for the specified base.
|
||||
* </p>
|
||||
*/
|
||||
public final class BaseX{
|
||||
private static final Logger logger = LoggerFactory.getLogger(BaseX.class);
|
||||
//Fields
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The encoded/decoded string
|
||||
//Settings
|
||||
protected int base; //The base that the number will be encoded at
|
||||
//?Fields
|
||||
/** The string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded string */
|
||||
protected String outputString;
|
||||
//?Settings
|
||||
/** The base that the number will be encoded at */
|
||||
protected int base;
|
||||
|
||||
|
||||
//Sets the input string
|
||||
/**
|
||||
* Sets the input string for encoding, ensuring it is not null and contains at least one letter.
|
||||
*
|
||||
* @param inputString the string to be encoded
|
||||
* @throws InvalidInputException if the input string is null or blank
|
||||
*/
|
||||
protected void setInputStringEncode(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -38,6 +53,13 @@ public class BaseX{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Sets the input string for decoding, ensuring it is not null, does not contain invalid characters, and is properly formatted.
|
||||
*
|
||||
* @param inputString the string to be decoded
|
||||
* @throws InvalidCharacterException if the input string contains invalid characters
|
||||
* @throws InvalidInputException if the input string is null or blank
|
||||
*/
|
||||
protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -68,7 +90,12 @@ public class BaseX{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Sets the numeric base
|
||||
/**
|
||||
* Sets the base for encoding and decoding, ensuring it is within valid range.
|
||||
*
|
||||
* @param base the base to be set
|
||||
* @throws InvalidBaseException if the base is less than Character.MIN_RADIX or greater than Character.MAX_RADIX
|
||||
*/
|
||||
protected void setBase(int base) throws InvalidBaseException{
|
||||
if(base < Character.MIN_RADIX){
|
||||
throw new InvalidBaseException("Base cannot be less than " + Character.MIN_RADIX);
|
||||
@@ -81,7 +108,9 @@ public class BaseX{
|
||||
|
||||
this.base = base;
|
||||
}
|
||||
//Encode inputString, store it in outputString, and return it
|
||||
/**
|
||||
* Encodes the input string using the specified base.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -102,7 +131,11 @@ public class BaseX{
|
||||
outputString = output.toString().toUpperCase();
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
//Decode inputString, store it in outputString, and return it
|
||||
/**
|
||||
* Decodes the input string from the specified base.
|
||||
*
|
||||
* @throws InvalidCharacterException if the input string contains invalid characters for the base
|
||||
*/
|
||||
protected void decode() throws InvalidCharacterException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -129,23 +162,49 @@ public class BaseX{
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code BaseX} instance with the default base of 2.
|
||||
*
|
||||
* @throws InvalidBaseException if the default base is invalid
|
||||
*/
|
||||
public BaseX() throws InvalidBaseException{
|
||||
reset();
|
||||
setBase(2);
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code BaseX} instance with the specified base.
|
||||
*
|
||||
* @param base the base to be used for encoding and decoding
|
||||
* @throws InvalidBaseException if the base is invalid
|
||||
*/
|
||||
public BaseX(int base) throws InvalidBaseException{
|
||||
reset();
|
||||
setBase(base);
|
||||
}
|
||||
|
||||
//Sets the inputString and encodes the message
|
||||
/**
|
||||
* Encodes the given input string using the current base.
|
||||
*
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String inputString) throws InvalidInputException{
|
||||
reset();
|
||||
setInputStringEncode(inputString);
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Encodes the given input string using the specified base.
|
||||
*
|
||||
* @param base the base to use for encoding
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidBaseException if the base is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(int base, String inputString) throws InvalidBaseException, InvalidInputException{
|
||||
reset();
|
||||
setBase(base);
|
||||
@@ -153,13 +212,30 @@ public class BaseX{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Sets the inputString and decodes the message
|
||||
/**
|
||||
* Decodes the given input string using the current base.
|
||||
*
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidCharacterException if the input string contains invalid characters
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setInputStringDecode(inputString);
|
||||
decode();
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Decodes the given input string using the specified base.
|
||||
*
|
||||
* @param base the base to use for decoding
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidBaseException if the base is invalid
|
||||
* @throws InvalidCharacterException if the input string contains invalid characters
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(int base, String inputString) throws InvalidBaseException, InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setBase(base);
|
||||
@@ -168,17 +244,34 @@ public class BaseX{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Gets the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current base.
|
||||
*
|
||||
* @return the base
|
||||
*/
|
||||
public int getBase(){
|
||||
return base;
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets the input and output strings to empty.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Beaufort.java
|
||||
//Mattrixwv
|
||||
// Created: 02-23-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -12,23 +8,53 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Beaufort{
|
||||
/**
|
||||
* A class for encoding and decoding strings using the Beaufort cipher,
|
||||
* which is a variant of the Vigenère cipher with additional steps.
|
||||
*
|
||||
* <p>
|
||||
* The Beaufort cipher consists of three main steps:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li>Atbash cipher for reversing the string</li>
|
||||
* <li>Caesar cipher for shifting characters by a fixed amount</li>
|
||||
* <li>Vigenère cipher for applying a keyword-based shift</li>
|
||||
* </ul>
|
||||
* This class allows you to encode and decode strings with options to preserve
|
||||
* capitalization, whitespace, and symbols.
|
||||
*/
|
||||
public final class Beaufort{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Beaufort.class);
|
||||
//Fields
|
||||
protected String inputString; //This is the string that needs encoded/decoded
|
||||
protected String outputString; //This is the string that is output after encoding/decoding
|
||||
protected String keyword; //This is the keyword that is responsible for determining the offsets that you change each character by
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//Internal ciphers
|
||||
protected Atbash atbash; //The first step in encoding/decoding the cipher
|
||||
protected Caesar caesar; //The second step in encoding/decoding the cipher
|
||||
protected Vigenere vigenere; //The third step in encoding/decoding the cipher
|
||||
//?Fields
|
||||
/** This is the string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** This is the string that is output after encoding/decoding */
|
||||
protected String outputString;
|
||||
/** This is the keyword that is responsible for determining the offsets that you change each character by */
|
||||
protected String keyword;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
//?Internal ciphers
|
||||
/** The first step in encoding/decoding the cipher */
|
||||
protected Atbash atbash;
|
||||
/** The second step in encoding/decoding the cipher */
|
||||
protected Caesar caesar;
|
||||
/** The third step in encoding/decoding the cipher */
|
||||
protected Vigenere vigenere;
|
||||
|
||||
|
||||
//Ensures inputString constraints
|
||||
/**
|
||||
* Sets the input string for encoding or decoding, applying removal options
|
||||
* for case, whitespace, and symbols.
|
||||
*
|
||||
* @param inputString the string to be processed
|
||||
* @throws InvalidInputException if the input string is null or blank after processing
|
||||
*/
|
||||
public void setInputString(String inputString) throws InvalidInputException{
|
||||
//Make sure the input isn't null
|
||||
if(inputString == null){
|
||||
@@ -63,7 +89,13 @@ public class Beaufort{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Ensures keyword constraints
|
||||
/**
|
||||
* Sets the keyword for encoding or decoding, ensuring it contains only
|
||||
* uppercase letters and is at least 2 letters long.
|
||||
*
|
||||
* @param keyword the keyword for the Vigenère cipher
|
||||
* @throws InvalidKeywordException if the keyword is null, blank, or less than 2 letters
|
||||
*/
|
||||
public void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Make sure the keyword isn't null
|
||||
if(keyword == null){
|
||||
@@ -88,20 +120,38 @@ public class Beaufort{
|
||||
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
|
||||
}
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the Beaufort cipher and stores the result in {@code outputString}.
|
||||
*
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void encode() throws InvalidKeywordException, InvalidInputException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
code();
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the Beaufort cipher and stores the result in {@code outputString}.
|
||||
* Decoding is the same process as encoding in this cipher.
|
||||
*
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void decode() throws InvalidKeywordException, InvalidInputException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Decoding is just encoding again
|
||||
code();
|
||||
}
|
||||
//Codes input and saves to output
|
||||
/**
|
||||
* Performs the Beaufort cipher encoding/decoding process:
|
||||
* <ul>
|
||||
* <li>Encodes the input string using the Atbash cipher</li>
|
||||
* <li>Shifts the result by 1 using the Caesar cipher</li>
|
||||
* <li>Applies the Vigenère cipher using the keyword</li>
|
||||
* </ul>
|
||||
*/
|
||||
protected void code(){
|
||||
//Reverse the string
|
||||
logger.debug("Encoding with Atbash");
|
||||
@@ -120,7 +170,10 @@ public class Beaufort{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Beaufort} instance with default settings.
|
||||
*/
|
||||
public Beaufort(){
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
@@ -130,6 +183,13 @@ public class Beaufort{
|
||||
vigenere = new Vigenere(false, false, false);
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Beaufort} instance with specified settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capitalization in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
*/
|
||||
public Beaufort(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -140,7 +200,15 @@ public class Beaufort{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using keyword and returns the result
|
||||
/**
|
||||
* Encodes the input string using the specified keyword.
|
||||
*
|
||||
* @param keyword the keyword for the Vigenère cipher
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
setKeyword(keyword);
|
||||
@@ -150,7 +218,15 @@ public class Beaufort{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using keyword and returns the result
|
||||
/**
|
||||
* Decodes the input string using the specified keyword.
|
||||
*
|
||||
* @param keyword the keyword for the Vigenère cipher
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
setKeyword(keyword);
|
||||
@@ -161,17 +237,34 @@ public class Beaufort{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Gets the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current keyword.
|
||||
*
|
||||
* @return the keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets the input string, output string, and keyword to empty.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Caesar.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -11,28 +7,59 @@ import org.slf4j.LoggerFactory;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class Caesar{
|
||||
/**
|
||||
* A class for encoding and decoding strings using the Caesar cipher.
|
||||
*
|
||||
* <p>
|
||||
* The Caesar cipher is a substitution cipher where each letter in the
|
||||
* plaintext is shifted a fixed number of places down or up the alphabet.
|
||||
* This class allows you to encode and decode strings with options to preserve
|
||||
* capitalization, whitespace, and symbols.
|
||||
* </p>
|
||||
*/
|
||||
public final class Caesar{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Caesar.class);
|
||||
//Fields
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The encoded/decoded string
|
||||
protected int shift; //The amount that you need to shift each letter
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//?Fields
|
||||
/** The string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded string */
|
||||
protected String outputString;
|
||||
/** The amount that you need to shift each letter */
|
||||
protected int shift;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
|
||||
|
||||
//Sets shift and makes sure it is within the propper bounds
|
||||
/**
|
||||
* Sets the shift amount and ensures it is within the proper bounds (0-25).
|
||||
*
|
||||
* @param shiftAmount the amount to shift each letter
|
||||
*/
|
||||
protected void setShift(int shiftAmount){
|
||||
logger.debug("Setting shift {}", shiftAmount);
|
||||
|
||||
//If you shift more than 26 you will just be wrapping back around again
|
||||
if(shiftAmount < 0){
|
||||
logger.debug("Negative shift detected, converting to positive equivalent");
|
||||
|
||||
shiftAmount += 26;
|
||||
}
|
||||
shift = shiftAmount % 26;
|
||||
|
||||
logger.debug("Cleaned shift {}", shift);
|
||||
}
|
||||
//Sets the input string
|
||||
/**
|
||||
* Sets the input string for encoding or decoding, applying removal options
|
||||
* for case, whitespace, and symbols.
|
||||
*
|
||||
* @param inputString the string to be processed
|
||||
* @throws InvalidInputException if the input string is null or blank after processing
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -63,7 +90,9 @@ public class Caesar{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string by shifting letters according to the Caesar cipher.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -76,7 +105,7 @@ public class Caesar{
|
||||
if(Character.isUpperCase(currentChar)){
|
||||
logger.debug("Encoding uppercase");
|
||||
|
||||
currentChar += shift;
|
||||
currentChar = (char)(currentChar + shift);
|
||||
//Wrap around if the letter is now out of bounds
|
||||
if(currentChar < 'A'){
|
||||
logger.debug("Wrapping around to Z");
|
||||
@@ -91,7 +120,7 @@ public class Caesar{
|
||||
else if(Character.isLowerCase(currentChar)){
|
||||
logger.debug("Encoding lowercase");
|
||||
|
||||
currentChar += shift;
|
||||
currentChar = (char)(currentChar + shift);
|
||||
//Wrap around if the letter is now out of bounds
|
||||
if(currentChar < 'a'){
|
||||
logger.debug("Wrapping around to z");
|
||||
@@ -111,7 +140,9 @@ public class Caesar{
|
||||
outputString = output.toString();
|
||||
logger.debug("Saving encoded string '{}'", outputString);
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string by reversing the shift applied during encoding.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -124,7 +155,7 @@ public class Caesar{
|
||||
if(Character.isUpperCase(currentChar)){
|
||||
logger.debug("Decoding uppercase");
|
||||
|
||||
currentChar -= shift;
|
||||
currentChar = (char)(currentChar - shift);
|
||||
//Wrap around if the letter is now out of bounds
|
||||
if(currentChar < 'A'){
|
||||
logger.debug("Wrapping around to Z");
|
||||
@@ -141,7 +172,7 @@ public class Caesar{
|
||||
else if(Character.isLowerCase(currentChar)){
|
||||
logger.debug("Decoding lowercase");
|
||||
|
||||
currentChar -= shift;
|
||||
currentChar = (char)(currentChar - shift);
|
||||
//Wrap around if the letter is now out of bounds
|
||||
if(currentChar < 'a'){
|
||||
logger.debug("Wrapping around to z");
|
||||
@@ -164,13 +195,23 @@ public class Caesar{
|
||||
logger.debug("Saving decoded string '{}'", outputString);
|
||||
}
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Caesar} instance with default settings.
|
||||
*/
|
||||
public Caesar(){
|
||||
reset();
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Caesar} instance with specified settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capitalization in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
*/
|
||||
public Caesar(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
reset();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
@@ -178,7 +219,14 @@ public class Caesar{
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
}
|
||||
|
||||
//Sets the shift and inputString and encodes the message
|
||||
/**
|
||||
* Encodes the input string with the specified shift amount.
|
||||
*
|
||||
* @param shiftAmount the amount to shift each letter
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(int shiftAmount, String inputString) throws InvalidInputException{
|
||||
reset();
|
||||
setShift(shiftAmount);
|
||||
@@ -186,7 +234,14 @@ public class Caesar{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Sets the shift and inputString and decodes the message
|
||||
/**
|
||||
* Decodes the input string with the specified shift amount.
|
||||
*
|
||||
* @param shiftAmount the amount to shift each letter
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(int shiftAmount, String inputString) throws InvalidInputException{
|
||||
reset();
|
||||
setShift(shiftAmount);
|
||||
@@ -195,17 +250,34 @@ public class Caesar{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Gets the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current shift amount.
|
||||
*
|
||||
* @return the shift amount
|
||||
*/
|
||||
public int getShift(){
|
||||
return shift;
|
||||
}
|
||||
/**
|
||||
* Gets the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets the internal fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
@@ -1,7 +1,23 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/OneTimePad.java
|
||||
//Mattrixwv
|
||||
// Created: 02-23-22
|
||||
//Modified: 05-04-23
|
||||
//Modified: 08-11-24
|
||||
/*
|
||||
Copyright (C) 2024 Mattrixwv
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -12,21 +28,51 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
/**
|
||||
* A class for encoding and decoding strings using the One-Time Pad cipher,
|
||||
* which is a special case of the Vigenère cipher where the key is as long as
|
||||
* the input message and used only once.
|
||||
*
|
||||
* <p>
|
||||
* The One-Time Pad cipher provides perfect secrecy when the key is truly random,
|
||||
* as long as the key length is equal to or greater than the length of the message
|
||||
* and the key is used only once.
|
||||
* </p>
|
||||
*/
|
||||
public class OneTimePad extends Vigenere{
|
||||
private static final Logger logger = LoggerFactory.getLogger(OneTimePad.class);
|
||||
|
||||
//?Add some kind of entropy calculator?
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code OneTimePad} instance with default settings.
|
||||
*/
|
||||
public OneTimePad(){
|
||||
super();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code OneTimePad} instance with specified settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capitalization in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
*/
|
||||
public OneTimePad(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
super(preserveCapitals, preserveWhitespace, preserveSymbols);
|
||||
}
|
||||
|
||||
//Encodes input using key and returns the result
|
||||
/**
|
||||
* Encodes the input string using the One-Time Pad cipher with the provided key.
|
||||
* The key must be at least as long as the input string.
|
||||
*
|
||||
* @param keyword the key to use for encoding
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if the key is shorter than the input string
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
@Override
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
if(keyword.length() < inputString.length()){
|
||||
@@ -37,7 +83,16 @@ public class OneTimePad extends Vigenere{
|
||||
|
||||
return super.encode(keyword, inputString);
|
||||
}
|
||||
//Decodes input using key and returns the result
|
||||
/**
|
||||
* Decodes the input string using the One-Time Pad cipher with the provided key.
|
||||
* The key must be at least as long as the input string.
|
||||
*
|
||||
* @param keyword the key to use for decoding
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if the key is shorter than the input string
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
@Override
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
if(keyword.length() < inputString.length()){
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Porta.java
|
||||
//Mattrixwv
|
||||
// Created: 02-28-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -12,9 +8,22 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Porta{
|
||||
/**
|
||||
* A class for encoding and decoding strings using the Porta cipher,
|
||||
* which is a variant of the Vigenère cipher that uses a tableau of
|
||||
* alphabets to encode and decode messages.
|
||||
*
|
||||
* <p>
|
||||
* The Porta cipher uses a series of Caesar ciphers based on a repeating
|
||||
* keyword, with a pre-defined set of alphabetic shifts. This implementation
|
||||
* allows for encoding and decoding of messages while preserving or removing
|
||||
* certain characters based on configuration settings.
|
||||
* </p>
|
||||
*/
|
||||
public final class Porta{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Porta.class);
|
||||
|
||||
/** Predefined alphabetic tableau used for encoding and decoding */
|
||||
private static final String[] tableau = {
|
||||
"NOPQRSTUVWXYZABCDEFGHIJKLM", //A-B
|
||||
"OPQRSTUVWXYZNMABCDEFGHIJKL", //C-D
|
||||
@@ -31,17 +40,28 @@ public class Porta{
|
||||
"ZNOPQRSTUVWXYBCDEFGHIJKLMA" //Y-Z
|
||||
};
|
||||
|
||||
//Fields
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The encoded/decoded string
|
||||
protected String keyword; //The keyword used to encode the input string
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//?Fields
|
||||
/** The string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded string */
|
||||
protected String outputString;
|
||||
/** The keyword used to encode the input string */
|
||||
protected String keyword;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
|
||||
|
||||
//Ensure all keyword constraints are followed
|
||||
/**
|
||||
* Ensures all keyword constraints are followed.
|
||||
*
|
||||
* @param keyword the keyword to be used for encoding/decoding
|
||||
* @throws InvalidKeywordException if the keyword is null, empty, or less than 2 characters
|
||||
*/
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Make sure the keyword isn't null
|
||||
if(keyword == null){
|
||||
@@ -67,7 +87,12 @@ public class Porta{
|
||||
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
|
||||
}
|
||||
}
|
||||
//Ensure all input constraints are followed
|
||||
/**
|
||||
* Ensures all input constraints are followed.
|
||||
*
|
||||
* @param inputString the string to be encoded/decoded
|
||||
* @throws InvalidInputException if the input string is null or blank
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
//Ensure the input isn't null
|
||||
if(inputString == null){
|
||||
@@ -102,7 +127,13 @@ public class Porta{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Returns the letter that replaces the passed in letter
|
||||
/**
|
||||
* Returns the letter that replaces the passed-in letter based on the keyword and tableau.
|
||||
*
|
||||
* @param keywordCnt the index of the keyword character to use
|
||||
* @param letter the letter to be replaced
|
||||
* @return the replacement letter
|
||||
*/
|
||||
protected char getReplacer(int keywordCnt, char letter){
|
||||
logger.debug("Getting letter that replaces {} at {}", letter, keywordCnt);
|
||||
|
||||
@@ -130,21 +161,30 @@ public class Porta{
|
||||
logger.debug("Replacer {}", replacer);
|
||||
return replacer;
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the inputString and stores the result in outputString.
|
||||
* Encoding is the same as decoding for this cipher.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
//Encoding is the same as decoding
|
||||
code();
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the inputString and stores the result in outputString.
|
||||
* Decoding is the same as encoding for this cipher.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Decoding is the same as encoding
|
||||
code();
|
||||
}
|
||||
//Codes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes or decodes the inputString based on the provided keyword.
|
||||
* Uses the tableau to replace characters.
|
||||
*/
|
||||
protected void code(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
@@ -175,13 +215,23 @@ public class Porta{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Porta} instance with default settings.
|
||||
*/
|
||||
public Porta(){
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Porta} instance with specified settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capitalization in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
*/
|
||||
public Porta(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -189,7 +239,15 @@ public class Porta{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Sets the keyword and inputString and encodes the message
|
||||
/**
|
||||
* Sets the keyword and inputString and encodes the message.
|
||||
*
|
||||
* @param keyword the keyword to use for encoding
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
@@ -200,7 +258,15 @@ public class Porta{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Sets the keyword and inputString and decodes the message
|
||||
/**
|
||||
* Sets the keyword and inputString and decodes the message.
|
||||
*
|
||||
* @param keyword the keyword to use for decoding
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
@@ -212,17 +278,34 @@ public class Porta{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Gets the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current keyword.
|
||||
*
|
||||
* @return the keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
//Makes sure all fields are empty
|
||||
/**
|
||||
* Resets all fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Substitution.java
|
||||
//Mattrixwv
|
||||
// Created: 02-22-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -12,19 +8,41 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Substitution{
|
||||
/**
|
||||
* A class for encoding and decoding strings using a substitution cipher.
|
||||
* This cipher can handle both alphabetic and alphanumeric keys and supports
|
||||
* preserving or removing capitalization, whitespace, and symbols.
|
||||
*
|
||||
* <p>
|
||||
* The substitution cipher replaces each letter or digit in the input string
|
||||
* with a corresponding character from a key. The key must contain all letters
|
||||
* of the alphabet and optionally digits, without duplicates.
|
||||
* </p>
|
||||
*/
|
||||
public final class Substitution{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Substitution.class);
|
||||
//Fields
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The encoded/decoded string
|
||||
protected String keyword; //The keyword used to encode/decode the input
|
||||
//Getters
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//?Fields
|
||||
/** The string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded string */
|
||||
protected String outputString;
|
||||
/** The keyword used to encode/decode the input */
|
||||
protected String keyword;
|
||||
//?Getters
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
|
||||
|
||||
//Ensures key constraints are followed
|
||||
/**
|
||||
* Ensures all key constraints are followed.
|
||||
*
|
||||
* @param key the key to be used for encoding/decoding
|
||||
* @throws InvalidKeywordException if the key is null, contains duplicates, or has invalid length
|
||||
*/
|
||||
protected void setKeyword(String key) throws InvalidKeywordException{
|
||||
if(key == null){
|
||||
throw new InvalidKeywordException("Key cannot be null");
|
||||
@@ -71,7 +89,12 @@ public class Substitution{
|
||||
logger.debug("Cleaned key '{}'", key);
|
||||
this.keyword = key;
|
||||
}
|
||||
//Ensure intput constraints are followed
|
||||
/**
|
||||
* Ensures all input constraints are followed.
|
||||
*
|
||||
* @param inputString the string to be encoded/decoded
|
||||
* @throws InvalidInputException if the input string is null or blank
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -105,7 +128,9 @@ public class Substitution{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the inputString using the provided key and stores the result in outputString.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -137,7 +162,9 @@ public class Substitution{
|
||||
this.outputString = output.toString();
|
||||
logger.debug("Encoded message '{}'", outputString);
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the inputString using the provided key and stores the result in outputString.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -173,13 +200,23 @@ public class Substitution{
|
||||
logger.debug("Decoded message '{}'", outputString);
|
||||
}
|
||||
|
||||
//Constructors
|
||||
//?Constructors
|
||||
/**
|
||||
* Constructs a new {@code Substitution} instance with default settings.
|
||||
*/
|
||||
public Substitution(){
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Substitution} instance with specified settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capitalization in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
*/
|
||||
public Substitution(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -187,14 +224,30 @@ public class Substitution{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using the provided key
|
||||
/**
|
||||
* Encodes the inputString using the provided key.
|
||||
*
|
||||
* @param key the key to use for encoding
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if the key is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKeyword(key);
|
||||
setInputString(inputString);
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using the provided key
|
||||
/**
|
||||
* Decodes the inputString using the provided key.
|
||||
*
|
||||
* @param key the key to use for decoding
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if the key is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKeyword(key);
|
||||
setInputString(inputString);
|
||||
@@ -202,17 +255,34 @@ public class Substitution{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Gets the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current keyword.
|
||||
*
|
||||
* @return the keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Vigenere.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -15,20 +11,38 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
/**
|
||||
* A class for encoding and decoding strings using the Vigenère cipher.
|
||||
* This cipher uses a keyword to determine the shift for each character in the input string.
|
||||
*
|
||||
* <p>
|
||||
* The Vigenère cipher applies a series of Caesar ciphers based on the letters of a keyword.
|
||||
* The keyword determines the shift for each letter in the input string.
|
||||
* </p>
|
||||
*/
|
||||
public class Vigenere{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Vigenere.class);
|
||||
//Fields
|
||||
protected String inputString; //This is the string that needs encoded/decoded
|
||||
protected String outputString; //This is the string that is output after encoding/decoding
|
||||
protected String keyword; //This is the keyword that is resposible for determining the offsets that you change each character by
|
||||
protected ArrayList<Integer> offset; //Holds the offsets coputed from each character in the keyword
|
||||
/** This is the string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** This is the string that is output after encoding/decoding */
|
||||
protected String outputString;
|
||||
/** This is the keyword that is resposible for determining the offsets that you change each character by */
|
||||
protected String keyword;
|
||||
/** Holds the offsets coputed from each character in the keyword */
|
||||
protected ArrayList<Integer> offset;
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
|
||||
|
||||
//Uses keyword to calculate the offset for the Caesar cipher for each character
|
||||
/**
|
||||
* Calculates the offsets for the Vigenère cipher from the keyword.
|
||||
*/
|
||||
protected void setOffset(){
|
||||
logger.debug("Setting offset array from keyword");
|
||||
|
||||
@@ -43,7 +57,12 @@ public class Vigenere{
|
||||
|
||||
logger.debug("Offset {}", offset);
|
||||
}
|
||||
//Sets inputString
|
||||
/**
|
||||
* Sets the input string and applies transformation settings.
|
||||
*
|
||||
* @param inputString the string to be encoded/decoded
|
||||
* @throws InvalidInputException if the input string is null or blank
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -74,7 +93,12 @@ public class Vigenere{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Sets keyword
|
||||
/**
|
||||
* Sets the keyword for the Vigenère cipher and computes the offsets.
|
||||
*
|
||||
* @param keyword the keyword to be used for encoding/decoding
|
||||
* @throws InvalidKeywordException if the keyword is null, too short, or contains invalid characters
|
||||
*/
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
if(keyword == null){
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
@@ -102,7 +126,9 @@ public class Vigenere{
|
||||
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
|
||||
}
|
||||
}
|
||||
//Encodes inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the Vigenère cipher and stores the result.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -147,7 +173,9 @@ public class Vigenere{
|
||||
outputString = output.toString();
|
||||
logger.debug("Encoded message '{}'", outputString);
|
||||
}
|
||||
//Decodes inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the Vigenère cipher and stores the result.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -194,23 +222,47 @@ public class Vigenere{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Vigenere} instance with default settings.
|
||||
*/
|
||||
public Vigenere(){
|
||||
offset = new ArrayList<>();
|
||||
reset();
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
offset.clear();
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Vigenere} instance with specified settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capitalization in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
*/
|
||||
public Vigenere(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
offset = new ArrayList<>();
|
||||
reset();
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
offset.clear();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
}
|
||||
|
||||
//Encodes input using key and returns the result
|
||||
/**
|
||||
* Encodes the input string using the provided keyword.
|
||||
*
|
||||
* @param keyword the keyword to use for encoding
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
@@ -218,7 +270,15 @@ public class Vigenere{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes input using key and returns the result
|
||||
/**
|
||||
* Decodes the input string using the provided keyword.
|
||||
*
|
||||
* @param keyword the keyword to use for decoding
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
@@ -227,21 +287,42 @@ public class Vigenere{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Gets the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current keyword.
|
||||
*
|
||||
* @return the keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
/**
|
||||
* Gets the current offset list.
|
||||
*
|
||||
* @return the offset list
|
||||
*/
|
||||
public List<Integer> getOffsets(){
|
||||
return offset;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Bifid.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -13,21 +9,44 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Bifid{
|
||||
/**
|
||||
* A class for encoding and decoding strings using the Bifid cipher.
|
||||
* The Bifid cipher is a polygraphic substitution cipher that uses a Polybius square
|
||||
* and applies a columnar transposition to encode or decode messages.
|
||||
*
|
||||
* <p>
|
||||
* The Bifid cipher operates by converting the plaintext message into coordinates based on a
|
||||
* Polybius square, then rearranges these coordinates using a transposition before converting
|
||||
* them back into text.
|
||||
* </p>
|
||||
*/
|
||||
public final class Bifid{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Bifid.class);
|
||||
//Fields
|
||||
protected String inputString; //The message that needs to be encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
protected String keyword; //The keyword used to create the grid
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//Internal ciphers
|
||||
protected PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters
|
||||
//?Fields
|
||||
/** The message that needs to be encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded message */
|
||||
protected String outputString;
|
||||
/** The keyword used to create the grid */
|
||||
protected String keyword;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
//?Internal ciphers
|
||||
/** Used to encode the message to numbers then back into letters */
|
||||
protected PolybiusSquare polybiusSquare;
|
||||
|
||||
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
/**
|
||||
* Sets the keyword for creating the Polybius square.
|
||||
*
|
||||
* @param keyword the keyword to use for the Polybius square
|
||||
* @throws InvalidKeywordException if the keyword is null
|
||||
*/
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Ensure the keyword isn't null
|
||||
if(keyword == null){
|
||||
@@ -39,7 +58,12 @@ public class Bifid{
|
||||
//Save the key for polybius to deal with
|
||||
this.keyword = keyword;
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
/**
|
||||
* Validates and preprocesses the input string based on the current settings.
|
||||
*
|
||||
* @param inputString the string to be encoded/decoded
|
||||
* @throws InvalidInputException if the input string is null or blank
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
//Ensure the input string isn't null
|
||||
if(inputString == null){
|
||||
@@ -74,7 +98,11 @@ public class Bifid{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Adds all non-letter characters back to the output string
|
||||
/**
|
||||
* Formats the encoded/decoded output to match the input string's original format.
|
||||
*
|
||||
* @param outputString the encoded/decoded message to be formatted
|
||||
*/
|
||||
protected void formatOutput(String outputString){
|
||||
logger.debug("Formatting output");
|
||||
//Keep track of where you are in the output
|
||||
@@ -104,7 +132,12 @@ public class Bifid{
|
||||
this.outputString = output.toString();
|
||||
logger.debug("Formatted output string '{}'", this.outputString);
|
||||
}
|
||||
//Encodes inputString using a polybius square and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the Bifid cipher and stores the result in outputString.
|
||||
*
|
||||
* @throws InvalidCharacterException if there are invalid characters in the input
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void encode() throws InvalidCharacterException, InvalidInputException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -140,7 +173,12 @@ public class Bifid{
|
||||
//Format the output
|
||||
formatOutput(letterResult);
|
||||
}
|
||||
//Decodes inputString using a polybius square and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the Bifid cipher and stores the result in outputString.
|
||||
*
|
||||
* @throws InvalidCharacterException if there are invalid characters in the input
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void decode() throws InvalidCharacterException, InvalidInputException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -170,7 +208,12 @@ public class Bifid{
|
||||
}
|
||||
|
||||
|
||||
//Constructors
|
||||
//?Constructors
|
||||
/**
|
||||
* Constructs a new {@code Bifid} instance with default settings.
|
||||
*
|
||||
* @throws InvalidCharacterException if initialization of PolybiusSquare fails
|
||||
*/
|
||||
public Bifid() throws InvalidCharacterException{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
@@ -178,6 +221,14 @@ public class Bifid{
|
||||
polybiusSquare = new PolybiusSquare(false, false);
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Bifid} instance with specified settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capitalization in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
* @throws InvalidCharacterException if initialization of PolybiusSquare fails
|
||||
*/
|
||||
public Bifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -186,7 +237,16 @@ public class Bifid{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using keyword and returns the result
|
||||
/**
|
||||
* Encodes the input string using the provided keyword.
|
||||
*
|
||||
* @param keyword the keyword to use for encoding
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
* @throws InvalidCharacterException if the input contains invalid characters
|
||||
*/
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
@@ -197,7 +257,16 @@ public class Bifid{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using keyword and returns the result
|
||||
/**
|
||||
* Decodes the input string using the provided keyword.
|
||||
*
|
||||
* @param keyword the keyword to use for decoding
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
* @throws InvalidCharacterException if the input contains invalid characters
|
||||
*/
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
@@ -209,18 +278,34 @@ public class Bifid{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Gets the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current keyword.
|
||||
*
|
||||
* @return the keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//MattrixwvWebsite/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Columnar.java
|
||||
//Mattrixwv
|
||||
// Created: 01-16-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -17,28 +13,54 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Columnar{
|
||||
/**
|
||||
* This class implements the Columnar Transposition cipher for encoding and decoding messages.
|
||||
* The Columnar Transposition cipher rearranges the letters of the plaintext based on a keyword,
|
||||
* and then reads the message column-wise to produce the encoded output. For decoding, it reverses
|
||||
* this process.
|
||||
* <p>
|
||||
* This class provides methods to configure the cipher, process input strings, and generate output.
|
||||
* It also includes options to preserve capitalization, whitespace, symbols, and to handle padding characters.
|
||||
* </p>
|
||||
*/
|
||||
public final class Columnar{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Columnar.class);
|
||||
//Fields
|
||||
protected String inputString; //The message that needs to be encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
protected String keyword; //The keyword used to create the grid
|
||||
protected char characterToAdd; //The character that is added to the end of a string to bring it to the correct length
|
||||
protected int charsAdded; //The number of characters that were added to the end of the message
|
||||
protected ArrayList<ArrayList<Character>> grid; //The grid used to encode/decode the message
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
protected boolean removePadding; //Remove the padding letters added to the cipher
|
||||
//?Fields
|
||||
/** The message that needs to be encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded message */
|
||||
protected String outputString;
|
||||
/** The keyword used to create the grid */
|
||||
protected String keyword;
|
||||
/** The character that is added to the end of a string to bring it to the correct length */
|
||||
protected char characterToAdd;
|
||||
/** The number of characters that were added to the end of the message */
|
||||
protected int charsAdded;
|
||||
/** The grid used to encode/decode the message */
|
||||
protected ArrayList<ArrayList<Character>> grid;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
/** Remove the padding letters added to the cipher */
|
||||
protected boolean removePadding;
|
||||
|
||||
|
||||
//Strip the inputString of all non-letter characters and change them to capitals
|
||||
/**
|
||||
* Strip the inputString of all non-letter characters and change them to capitals
|
||||
*
|
||||
* @return The input string in capital letters with all non-letter characters removed
|
||||
*/
|
||||
protected String getCleanInputString(){
|
||||
logger.debug("Cleaning input string");
|
||||
return inputString.toUpperCase().replaceAll("[^A-Z]", "");
|
||||
}
|
||||
//Create the grid from the keyword
|
||||
/**
|
||||
* Creates the grid used for encoding.
|
||||
*/
|
||||
protected void createGridEncode(){
|
||||
logger.debug("Creating grid for encoding");
|
||||
|
||||
@@ -58,6 +80,9 @@ public class Columnar{
|
||||
)));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Creates the grid used for decoding.
|
||||
*/
|
||||
protected void createGridDecode(){
|
||||
logger.debug("Creating grid for decoding");
|
||||
|
||||
@@ -86,7 +111,12 @@ public class Columnar{
|
||||
}
|
||||
}
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
/**
|
||||
* Sets the input string for encoding, applying necessary modifications and padding.
|
||||
*
|
||||
* @param inputString the input string to encode
|
||||
* @throws InvalidInputException if the input string is null or blank
|
||||
*/
|
||||
protected void setInputStringEncode(String inputString) throws InvalidInputException{
|
||||
logger.debug("Setting input string for encoding");
|
||||
|
||||
@@ -140,6 +170,12 @@ public class Columnar{
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Sets the input string for decoding, applying necessary adjustments and padding.
|
||||
*
|
||||
* @param inputString the input string to decode
|
||||
* @throws InvalidInputException if the input string is null or blank
|
||||
*/
|
||||
protected void setInputStringDecode(String inputString) throws InvalidInputException{
|
||||
logger.debug("Setting input string for decoding");
|
||||
|
||||
@@ -225,7 +261,9 @@ public class Columnar{
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
//Creates the output string from the grid
|
||||
/**
|
||||
* Creates the output string by reading the columns of the grid.
|
||||
*/
|
||||
protected void createOutputStringFromColumns(){
|
||||
logger.debug("Creating output string for encoding");
|
||||
|
||||
@@ -275,6 +313,9 @@ public class Columnar{
|
||||
outputString = output.toString();
|
||||
logger.debug("Output string '{}'", outputString);
|
||||
}
|
||||
/**
|
||||
* Creates the output string by reading the rows of the grid.
|
||||
*/
|
||||
protected void createOutputStringFromRows(){
|
||||
logger.debug("Creating output string for decoding");
|
||||
|
||||
@@ -345,7 +386,12 @@ public class Columnar{
|
||||
outputString = output.toString();
|
||||
logger.debug("Decoded output string '{}'", outputString);
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
/**
|
||||
* Sets the keyword used for encoding or decoding.
|
||||
*
|
||||
* @param keyword the keyword to use
|
||||
* @throws InvalidKeywordException if the keyword is null, contains less than 2 letters, or contains invalid characters
|
||||
*/
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Ensure the keyword isn't null
|
||||
if(keyword == null){
|
||||
@@ -365,7 +411,12 @@ public class Columnar{
|
||||
throw new InvalidKeywordException("The keyword must contain at least 2 letters");
|
||||
}
|
||||
}
|
||||
//Set the character that is added to the end of the string
|
||||
/**
|
||||
* Sets the character used for padding.
|
||||
*
|
||||
* @param characterToAdd the padding character
|
||||
* @throws InvalidCharacterException if the padding character is not a letter
|
||||
*/
|
||||
protected void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
|
||||
if(!Character.isAlphabetic(characterToAdd)){
|
||||
throw new InvalidCharacterException("Character to add must be a letter");
|
||||
@@ -381,7 +432,11 @@ public class Columnar{
|
||||
|
||||
logger.debug("Character to add for padding {}", characterToAdd);
|
||||
}
|
||||
//Returns a list of integers that represents the location of the characters of the keyword in alphabetic order
|
||||
/**
|
||||
* Gets the positions of the keyword characters in alphabetical order.
|
||||
*
|
||||
* @return the list of positions of keyword characters in alphabetical order
|
||||
*/
|
||||
protected ArrayList<Integer> getKeywordAlphaLocations(){
|
||||
logger.debug("Creating an array of keyword letter locations");
|
||||
|
||||
@@ -400,6 +455,11 @@ public class Columnar{
|
||||
logger.debug("Array of keyword letters {}", orderedLocations);
|
||||
return orderedLocations;
|
||||
}
|
||||
/**
|
||||
* Gets the original positions of the keyword characters.
|
||||
*
|
||||
* @return the list of original positions of keyword characters
|
||||
*/
|
||||
protected ArrayList<Integer> getKeywordOriginalLocations(){
|
||||
logger.debug("Creating array of original keyword locations");
|
||||
|
||||
@@ -420,7 +480,11 @@ public class Columnar{
|
||||
logger.debug("Array of keyword letters {}", originalOrder);
|
||||
return originalOrder;
|
||||
}
|
||||
//Rearanges the grid based on the list of numbers given
|
||||
/**
|
||||
* Rearranges the grid based on the specified order.
|
||||
*
|
||||
* @param listOrder the order in which columns should be rearranged
|
||||
*/
|
||||
protected void rearangeGrid(ArrayList<Integer> listOrder){
|
||||
logger.debug("Rearanging grid");
|
||||
|
||||
@@ -442,7 +506,9 @@ public class Columnar{
|
||||
logger.debug("New grid {}", newGrid);
|
||||
grid = newGrid;
|
||||
}
|
||||
//Encodes inputString using the Columnar cipher and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the Columnar cipher.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -455,7 +521,9 @@ public class Columnar{
|
||||
//Create the output
|
||||
createOutputStringFromColumns();
|
||||
}
|
||||
//Decodes inputString using the Columnar cipher and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the Columnar cipher.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -468,7 +536,13 @@ public class Columnar{
|
||||
createOutputStringFromRows();
|
||||
}
|
||||
|
||||
//Constructors
|
||||
//?Constructors
|
||||
/**
|
||||
* Constructs a new Columnar cipher instance with default settings.
|
||||
* The default settings include preserving no capitalization, whitespace, or symbols, and using 'x' as the padding character.
|
||||
*
|
||||
* @throws InvalidCharacterException if the default padding character is invalid
|
||||
*/
|
||||
public Columnar() throws InvalidCharacterException{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
@@ -477,6 +551,15 @@ public class Columnar{
|
||||
setCharacterToAdd('x');
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new Columnar cipher instance with customizable settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capitalization in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
* @param removePadding whether to remove padding characters from the output
|
||||
* @throws InvalidCharacterException if the default padding character is invalid
|
||||
*/
|
||||
public Columnar(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, boolean removePadding) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -485,6 +568,16 @@ public class Columnar{
|
||||
setCharacterToAdd('x');
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new Columnar cipher instance with customizable settings and a specific padding character.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capitalization in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
* @param removePadding whether to remove padding characters from the output
|
||||
* @param characterToAdd the character to use for padding
|
||||
* @throws InvalidCharacterException if the padding character is not a letter
|
||||
*/
|
||||
public Columnar(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, boolean removePadding, char characterToAdd) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -494,7 +587,15 @@ public class Columnar{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using keyword and returns the result
|
||||
/**
|
||||
* Encodes the given input string using the provided keyword.
|
||||
*
|
||||
* @param keyword the keyword used for encoding
|
||||
* @param inputString the message to encode
|
||||
* @return the encoded message
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
@@ -505,7 +606,15 @@ public class Columnar{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Encodes inputString using keyword and returns the result
|
||||
/**
|
||||
* Decodes the given input string using the provided keyword.
|
||||
*
|
||||
* @param keyword the keyword used for decoding
|
||||
* @param inputString the message to decode
|
||||
* @return the decoded message
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
@@ -517,7 +626,9 @@ public class Columnar{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
@@ -528,13 +639,28 @@ public class Columnar{
|
||||
charsAdded = 0;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Gets the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current keyword.
|
||||
*
|
||||
* @return the keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Hill.java
|
||||
//Mattrixwv
|
||||
// Created: 01-31-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -18,20 +14,39 @@ import com.mattrixwv.matrix.exceptions.InvalidGeometryException;
|
||||
import com.mattrixwv.matrix.exceptions.InvalidScalarException;
|
||||
|
||||
|
||||
public class Hill{
|
||||
/**
|
||||
* Implements the Hill cipher for encoding and decoding messages using matrix operations.
|
||||
* <p>
|
||||
* The Hill cipher is a polygraphic substitution cipher that uses linear algebra to encrypt and decrypt
|
||||
* messages. It operates on blocks of text by representing them as vectors and applying matrix transformations.
|
||||
* </p>
|
||||
*/
|
||||
public final class Hill{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Hill.class);
|
||||
//Fields
|
||||
protected String inputString; //The message that needs to be encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
protected char characterToAdd; //The keyword used to create the grid
|
||||
protected ModMatrix key; //The matrix used perform the encoding/decoding
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//?Fields
|
||||
/** The message that needs to be encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded message */
|
||||
protected String outputString;
|
||||
/** The keyword used to create the grid */
|
||||
protected char characterToAdd;
|
||||
/** The matrix used perform the encoding/decoding */
|
||||
protected ModMatrix key;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
|
||||
|
||||
//Validate and set the key
|
||||
/**
|
||||
* Sets the key matrix for the Hill cipher after validating it.
|
||||
*
|
||||
* @param key the matrix to be used for encoding/decoding
|
||||
* @throws InvalidKeyException if the key is not a square matrix, or does not have an inverse modulo 26
|
||||
*/
|
||||
protected void setKey(ModMatrix key) throws InvalidKeyException{
|
||||
logger.debug("Setting key");
|
||||
|
||||
@@ -60,7 +75,13 @@ public class Hill{
|
||||
logger.debug("key\n{}", key);
|
||||
this.key = new ModMatrix(key);
|
||||
}
|
||||
//Validate and set the input string
|
||||
/**
|
||||
* Prepares and validates the input string for encoding by removing unwanted characters
|
||||
* and ensuring the length is appropriate.
|
||||
*
|
||||
* @param inputString the message to be encoded
|
||||
* @throws InvalidInputException if the input is null, blank, or its length is not a multiple of the matrix size
|
||||
*/
|
||||
protected void setInputStringEncode(String inputString) throws InvalidInputException{
|
||||
logger.debug("Setting input string for encoding");
|
||||
|
||||
@@ -111,6 +132,13 @@ public class Hill{
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Prepares and validates the input string for decoding by removing unwanted characters
|
||||
* and ensuring the length is appropriate.
|
||||
*
|
||||
* @param inputString the message to be decoded
|
||||
* @throws InvalidInputException if the input is null, blank, or its length is not a multiple of the matrix size
|
||||
*/
|
||||
protected void setInputStringDecode(String inputString) throws InvalidInputException{
|
||||
logger.debug("Setting input string for decoding");
|
||||
|
||||
@@ -145,7 +173,11 @@ public class Hill{
|
||||
throw new InvalidInputException("Length of input string must be a multiple of the number of rows in the key");
|
||||
}
|
||||
}
|
||||
//Get a perfectly clean copy of input string
|
||||
/**
|
||||
* Returns a clean version of the input string with only uppercase letters.
|
||||
*
|
||||
* @return the cleaned input string
|
||||
*/
|
||||
protected String getCleanInputString(){
|
||||
logger.debug("Cleaning inputString");
|
||||
|
||||
@@ -154,7 +186,12 @@ public class Hill{
|
||||
|
||||
return cleanInputString;
|
||||
}
|
||||
//Validate and set character to add
|
||||
/**
|
||||
* Sets the character used for padding the input string.
|
||||
*
|
||||
* @param characterToAdd the character to be used for padding
|
||||
* @throws InvalidCharacterException if the character is not a letter
|
||||
*/
|
||||
protected void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
|
||||
logger.debug("Setting character to add {}", characterToAdd);
|
||||
|
||||
@@ -173,7 +210,11 @@ public class Hill{
|
||||
logger.debug("Cleaned character {}", characterToAdd);
|
||||
this.characterToAdd = characterToAdd;
|
||||
}
|
||||
//Add capitalization, whitespace, and symbols to the output based on the input
|
||||
/**
|
||||
* Restores capitalization, whitespace, and symbols to the output string based on the original input string.
|
||||
*
|
||||
* @return the polished output string
|
||||
*/
|
||||
protected String polishOutputString(){
|
||||
logger.debug("Polishing output string");
|
||||
|
||||
@@ -203,7 +244,11 @@ public class Hill{
|
||||
logger.debug("Polished string '{}'", cleanString);
|
||||
return cleanString;
|
||||
}
|
||||
//Get vectors representing the input string
|
||||
/**
|
||||
* Converts the cleaned input string into a list of column vectors based on the key matrix size.
|
||||
*
|
||||
* @return a list of vectors representing the input string
|
||||
*/
|
||||
protected ArrayList<ModMatrix> getInputVectors(){
|
||||
logger.debug("Generating input vectors");
|
||||
|
||||
@@ -237,7 +282,12 @@ public class Hill{
|
||||
//Return the array of vectors
|
||||
return vectors;
|
||||
}
|
||||
//Get the string represented by the vectors that were passed in
|
||||
/**
|
||||
* Converts a list of vectors back into a string representation.
|
||||
*
|
||||
* @param outputVectors the list of vectors to be converted
|
||||
* @return the resulting string
|
||||
*/
|
||||
protected String getOutputFromVectors(ArrayList<ModMatrix> outputVectors){
|
||||
logger.debug("Turning vectors into a string");
|
||||
|
||||
@@ -257,7 +307,9 @@ public class Hill{
|
||||
logger.debug("Converted string '{}'", convertedString);
|
||||
return convertedString;
|
||||
}
|
||||
//Encode inputString and store the value in outputString
|
||||
/**
|
||||
* Encodes the input string using the provided key matrix and stores the result in outputString.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -281,7 +333,9 @@ public class Hill{
|
||||
//Add the extra characters back to the output and remove the added characters
|
||||
outputString = polishOutputString();
|
||||
}
|
||||
//Decode inputString and store the value in outputString
|
||||
/**
|
||||
* Decodes the input string using the provided key matrix and stores the result in outputString.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -308,7 +362,12 @@ public class Hill{
|
||||
outputString = polishOutputString();
|
||||
}
|
||||
|
||||
//Constructors
|
||||
//?Constructors
|
||||
/**
|
||||
* Default constructor initializing the cipher with default settings.
|
||||
*
|
||||
* @throws InvalidCharacterException if the default padding character is invalid
|
||||
*/
|
||||
public Hill() throws InvalidCharacterException{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
@@ -316,6 +375,14 @@ public class Hill{
|
||||
setCharacterToAdd('x');
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructor initializing the cipher with specified settings.
|
||||
*
|
||||
* @param preserveCapitals if true, preserves capitals in the output string
|
||||
* @param preserveWhitespace if true, preserves whitespace in the output string
|
||||
* @param preserveSymbols if true, preserves symbols in the output string
|
||||
* @throws InvalidCharacterException if the default padding character is invalid
|
||||
*/
|
||||
public Hill(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -323,6 +390,15 @@ public class Hill{
|
||||
setCharacterToAdd('x');
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructor initializing the cipher with specified settings and padding character.
|
||||
*
|
||||
* @param preserveCapitals if true, preserves capitals in the output string
|
||||
* @param preserveWhitespace if true, preserves whitespace in the output string
|
||||
* @param preserveSymbols if true, preserves symbols in the output string
|
||||
* @param characterToAdd the character to use for padding
|
||||
* @throws InvalidCharacterException if the padding character is invalid
|
||||
*/
|
||||
public Hill(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char characterToAdd) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -331,20 +407,54 @@ public class Hill{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encode inputString using the provided key
|
||||
/**
|
||||
* Encodes the input string using the provided key matrix and returns the encoded string.
|
||||
*
|
||||
* @param key the matrix to use for encoding
|
||||
* @param inputString the message to encode
|
||||
* @return the encoded message
|
||||
* @throws InvalidKeyException if the key is invalid
|
||||
* @throws InvalidInputException if the input is invalid
|
||||
*/
|
||||
public String encode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{
|
||||
return encode(new ModMatrix(key, 26), inputString);
|
||||
}
|
||||
/**
|
||||
* Encodes the input string using the provided key matrix and returns the encoded string.
|
||||
*
|
||||
* @param key the matrix to use for encoding
|
||||
* @param inputString the message to encode
|
||||
* @return the encoded message
|
||||
* @throws InvalidKeyException if the key is invalid
|
||||
* @throws InvalidInputException if the input is invalid
|
||||
*/
|
||||
public String encode(ModMatrix key, String inputString) throws InvalidKeyException, InvalidInputException{
|
||||
setKey(key);
|
||||
setInputStringEncode(inputString);
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decode inputString using the provided key
|
||||
/**
|
||||
* Decodes the input string using the provided key matrix and returns the decoded string.
|
||||
*
|
||||
* @param key the matrix to use for decoding
|
||||
* @param inputString the message to decode
|
||||
* @return the decoded message
|
||||
* @throws InvalidKeyException if the key is invalid
|
||||
* @throws InvalidInputException if the input is invalid
|
||||
*/
|
||||
public String decode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{
|
||||
return decode(new ModMatrix(key, 26), inputString);
|
||||
}
|
||||
/**
|
||||
* Decodes the input string using the provided key matrix and returns the decoded string.
|
||||
*
|
||||
* @param key the matrix to use for decoding
|
||||
* @param inputString the message to decode
|
||||
* @return the decoded message
|
||||
* @throws InvalidKeyException if the key is invalid
|
||||
* @throws InvalidInputException if the input is invalid
|
||||
*/
|
||||
public String decode(ModMatrix key, String inputString) throws InvalidKeyException, InvalidInputException{
|
||||
setKey(key);
|
||||
setInputStringDecode(inputString);
|
||||
@@ -352,7 +462,9 @@ public class Hill{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets the Hill cipher by clearing all variables and setting the key to an empty matrix.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
@@ -361,13 +473,28 @@ public class Hill{
|
||||
key = new ModMatrix(26);
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Returns the input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Returns the output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Returns the key matrix used for encoding or decoding.
|
||||
*
|
||||
* @return the key matrix
|
||||
*/
|
||||
public ModMatrix getKey(){
|
||||
return key;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,23 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquare.java
|
||||
//Mattrixwv
|
||||
// Created: 04-21-23
|
||||
// Modified: 05-04-23
|
||||
// Modified: 08-11-24
|
||||
/*
|
||||
Copyright (C) 2024 Mattrixwv
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -15,11 +31,18 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class LargePolybiusSquare extends PolybiusSquare{
|
||||
/**
|
||||
* Represents the Polybius square cipher encryption and decryption.
|
||||
* The Polybius square cipher is a classical encryption method that uses a 6x6 grid
|
||||
* to encode and decode messages based on the positions of letters and numbers in the grid.
|
||||
*/
|
||||
public final class LargePolybiusSquare extends PolybiusSquare{
|
||||
private static final Logger logger = LoggerFactory.getLogger(LargePolybiusSquare.class);
|
||||
|
||||
|
||||
//Create the grid from the keyword
|
||||
/**
|
||||
* Creates the grid from the keyword.
|
||||
*/
|
||||
@Override
|
||||
protected void createGrid(){
|
||||
logger.debug("Creating grid");
|
||||
@@ -31,7 +54,13 @@ public class LargePolybiusSquare extends PolybiusSquare{
|
||||
}
|
||||
}
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
/**
|
||||
* Strips invalid characters from the string that needs encoding/decoding.
|
||||
*
|
||||
* @param inputString the input string to be cleaned
|
||||
* @throws InvalidCharacterException if an invalid character is found
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
@Override
|
||||
protected void setInputStringEncode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
if(inputString == null){
|
||||
@@ -75,7 +104,11 @@ public class LargePolybiusSquare extends PolybiusSquare{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Returns the input string ready for encoding
|
||||
/**
|
||||
* Returns the input string ready for encoding.
|
||||
*
|
||||
* @return the prepared input string
|
||||
*/
|
||||
@Override
|
||||
protected String getPreparedInputStringEncode(){
|
||||
logger.debug("Preparing input string for encoding");
|
||||
@@ -87,7 +120,11 @@ public class LargePolybiusSquare extends PolybiusSquare{
|
||||
|
||||
return cleanString;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
/**
|
||||
* Strips invalid characters from the keyword and creates the grid.
|
||||
*
|
||||
* @param keyword the keyword to be processed
|
||||
*/
|
||||
@Override
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
if(keyword == null){
|
||||
@@ -116,7 +153,11 @@ public class LargePolybiusSquare extends PolybiusSquare{
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Adds characters that aren't letters to the output
|
||||
/**
|
||||
* Adds characters that aren't letters to the output during encoding.
|
||||
*
|
||||
* @param cleanString the cleaned string to be formatted
|
||||
*/
|
||||
@Override
|
||||
protected void addCharactersToCleanStringEncode(String cleanString){
|
||||
logger.debug("Formatting output string");
|
||||
@@ -144,6 +185,11 @@ public class LargePolybiusSquare extends PolybiusSquare{
|
||||
outputString = fullOutput.toString();
|
||||
logger.debug("Saving output string {}", outputString);
|
||||
}
|
||||
/**
|
||||
* Adds characters that aren't letters to the output during decoding.
|
||||
*
|
||||
* @param cleanString the cleaned string to be formatted
|
||||
*/
|
||||
@Override
|
||||
protected void addCharactersToCleanStringDecode(String cleanString){
|
||||
logger.debug("Formatting output string");
|
||||
@@ -172,7 +218,9 @@ public class LargePolybiusSquare extends PolybiusSquare{
|
||||
logger.debug("Saving output string {}", outputString);
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all variables to their default values.
|
||||
*/
|
||||
@Override
|
||||
public void reset(){
|
||||
logger.debug("Resetting");
|
||||
@@ -183,11 +231,25 @@ public class LargePolybiusSquare extends PolybiusSquare{
|
||||
keyword = "";
|
||||
}
|
||||
|
||||
//Constructors
|
||||
//?Constructors
|
||||
/**
|
||||
* Constructs a PolybiusSquare cipher instance with default settings.
|
||||
*
|
||||
* @throws InvalidCharacterException if default characters are invalid
|
||||
*/
|
||||
public LargePolybiusSquare() throws InvalidCharacterException{
|
||||
super();
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a PolybiusSquare cipher instance with specified settings.
|
||||
*
|
||||
* @param preserveWhitespace whether to preserve whitespace
|
||||
* @param preserveSymbols whether to preserve symbols
|
||||
* @throws InvalidCharacterException if default characters are invalid
|
||||
*/
|
||||
public LargePolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
super(preserveWhitespace, preserveSymbols);
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Morse.java
|
||||
//Mattrixwv
|
||||
// Created: 07-28-21
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -15,17 +11,28 @@ import org.slf4j.LoggerFactory;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class Morse{
|
||||
/**
|
||||
* A class to handle encoding and decoding of Morse code.
|
||||
* <p>
|
||||
* This class provides methods to encode a string into Morse code and decode Morse code back into a string.
|
||||
* It supports alphanumeric characters (A-Z, 0-9) and validates input to ensure it contains valid characters.
|
||||
* </p>
|
||||
*/
|
||||
public final class Morse{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Morse.class);
|
||||
//Code representations
|
||||
//?Code representations
|
||||
/** Morse code representations for letters A-Z */
|
||||
private static final String[] letters = {
|
||||
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", //A-L
|
||||
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." //M-Z
|
||||
};
|
||||
/** Morse code representations for digits 0-9 */
|
||||
private static final String[] numbers = {
|
||||
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----." //0-9
|
||||
};
|
||||
private static final Map<String, Character> map; //The map to help convert from Morse to letters and numbers
|
||||
/** The map to help convert from Morse to letters and numbers */
|
||||
private static final Map<String, Character> map;
|
||||
/** Initialize the Morse Code map */
|
||||
static{
|
||||
//Setup the map
|
||||
map = new HashMap<>();
|
||||
@@ -38,12 +45,22 @@ public class Morse{
|
||||
map.put(number, (char)('0' + cnt));
|
||||
}
|
||||
}
|
||||
//Fields
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
//?Fields
|
||||
/** The string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded message */
|
||||
protected String outputString;
|
||||
|
||||
|
||||
//Validate and set the input string for encoding
|
||||
/**
|
||||
* Validates and prepares the input string for encoding.
|
||||
* <p>
|
||||
* This method converts the input string to uppercase, removes all non-alphanumeric characters, and stores the cleaned string.
|
||||
* </p>
|
||||
*
|
||||
* @param inputString the string to be encoded
|
||||
* @throws InvalidInputException if the input string is null or contains invalid characters
|
||||
*/
|
||||
protected void setInputStringEncode(String inputString){
|
||||
logger.debug("Setting input string for encoding '{}'", inputString);
|
||||
|
||||
@@ -70,7 +87,15 @@ public class Morse{
|
||||
//Save the input
|
||||
this.inputString = inputString;
|
||||
}
|
||||
//Validate and set the input string for decoding
|
||||
/**
|
||||
* Validates and prepares the input string for decoding.
|
||||
* <p>
|
||||
* This method ensures that the input string contains only valid Morse code characters (dots, dashes, and spaces).
|
||||
* </p>
|
||||
*
|
||||
* @param inputString the Morse code string to be decoded
|
||||
* @throws InvalidInputException if the input string is null or contains invalid Morse code characters
|
||||
*/
|
||||
protected void setInputStringDecode(String inputString){
|
||||
logger.debug("Setting input string for decoding '{}'", inputString);
|
||||
|
||||
@@ -94,7 +119,14 @@ public class Morse{
|
||||
logger.debug("Saving");
|
||||
this.inputString = inputString;
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the {@code inputString} into Morse code.
|
||||
* <p>
|
||||
* This method converts each alphanumeric character in the input string to its Morse code representation and stores the result.
|
||||
* </p>
|
||||
*
|
||||
* @throws InvalidInputException if the input string contains characters that cannot be encoded
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -123,7 +155,14 @@ public class Morse{
|
||||
this.outputString = output.toString();
|
||||
logger.debug("Saving encoded string '{}'", outputString);
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the {@code inputString} from Morse code to plain text.
|
||||
* <p>
|
||||
* This method converts each Morse code sequence in the input string back to its corresponding alphanumeric character.
|
||||
* </p>
|
||||
*
|
||||
* @throws InvalidInputException if the input string contains invalid Morse code sequences
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -149,19 +188,40 @@ public class Morse{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Resets the class by clearing the input and output strings.
|
||||
*/
|
||||
public Morse(){
|
||||
reset();
|
||||
}
|
||||
|
||||
|
||||
//Sets the inputString and encodes the message
|
||||
/**
|
||||
* Encodes the given input string into Morse code.
|
||||
* <p>
|
||||
* This method validates the input string, encodes it, and returns the Morse code representation.
|
||||
* </p>
|
||||
*
|
||||
* @param inputString the string to be encoded
|
||||
* @return the Morse code representation of the input string
|
||||
* @throws InvalidInputException if the input string contains invalid characters
|
||||
*/
|
||||
public String encode(String inputString){
|
||||
setInputStringEncode(inputString);
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Sets the inputString and decodes the message
|
||||
/**
|
||||
* Decodes the given Morse code string into plain text.
|
||||
* <p>
|
||||
* This method validates the Morse code input, decodes it, and returns the plain text representation.
|
||||
* </p>
|
||||
*
|
||||
* @param inputString the Morse code string to be decoded
|
||||
* @return the decoded plain text representation
|
||||
* @throws InvalidInputException if the input string contains invalid Morse code sequences
|
||||
*/
|
||||
public String decode(String inputString){
|
||||
setInputStringDecode(inputString);
|
||||
decode();
|
||||
@@ -169,14 +229,26 @@ public class Morse{
|
||||
}
|
||||
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Returns the current input string.
|
||||
*
|
||||
* @return the current input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Returns the current output string.
|
||||
*
|
||||
* @return the current output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets the internal fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Playfair.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-30-21
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -15,40 +11,77 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Playfair{
|
||||
/**
|
||||
* Represents the Playfair cipher encryption and decryption.
|
||||
* The Playfair cipher is a digraph substitution cipher that encrypts pairs of letters.
|
||||
*/
|
||||
public final class Playfair{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Playfair.class);
|
||||
|
||||
//A class representing the location of a character in the grid
|
||||
/** A class representing the location of a character in the grid */
|
||||
protected class CharLocation{
|
||||
/** The x location in the grid */
|
||||
protected int x;
|
||||
/** The y location in the grid */
|
||||
protected int y;
|
||||
/**
|
||||
* Constructs a CharLocation with the specified x and y coordinates.
|
||||
*
|
||||
* @param x the x-coordinate of the character's location
|
||||
* @param y the y-coordinate of the character's location
|
||||
*/
|
||||
public CharLocation(int x, int y){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
/**
|
||||
* Returns the x-coordinate of the character's location.
|
||||
*
|
||||
* @return the x-coordinate
|
||||
*/
|
||||
public int getX(){
|
||||
return x;
|
||||
}
|
||||
/**
|
||||
* Returns the y-coordinate of the character's location.
|
||||
*
|
||||
* @return the y-coordinate
|
||||
*/
|
||||
public int getY(){
|
||||
return y;
|
||||
}
|
||||
}
|
||||
|
||||
//Fields
|
||||
protected String inputString; //The message that needs to be encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
protected String keyword; //The keyword used to create the grid
|
||||
protected char[][] grid; //The grid used to encode/decode the message
|
||||
protected char replaced; //The letter that will need to be replaced in the grid and any input string or keyword
|
||||
protected char replacer; //The letter that replaced replaced in the input string or keyword
|
||||
protected char doubled; //The letter that will be placed between double letters in the input string if necessary or to make the string length even
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist captials in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//?Fields
|
||||
/** The message that needs to be encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded message */
|
||||
protected String outputString;
|
||||
/** The keyword used to create the grid */
|
||||
protected String keyword;
|
||||
/** The grid used to encode/decode the message */
|
||||
protected char[][] grid;
|
||||
/** The letter that will need to be replaced in the grid and any input string or keyword */
|
||||
protected char replaced;
|
||||
/** The letter that replaced replaced in the input string or keyword */
|
||||
protected char replacer;
|
||||
/** The letter that will be placed between double letters in the input string if necessary or to make the string length even */
|
||||
protected char doubled;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
|
||||
|
||||
//Set the doubled character
|
||||
/**
|
||||
* Sets the doubled character.
|
||||
*
|
||||
* @param doubled the character to be used as the doubled character
|
||||
* @throws InvalidCharacterException if the character is not a letter or is invalid
|
||||
*/
|
||||
protected void setDoubled(char doubled) throws InvalidCharacterException{
|
||||
logger.debug("Setting doubled");
|
||||
logger.debug("Original character {}", doubled);
|
||||
@@ -80,7 +113,12 @@ public class Playfair{
|
||||
logger.debug("Setting doubled to {}", doubled);
|
||||
this.doubled = doubled;
|
||||
}
|
||||
//Set the replacer character
|
||||
/**
|
||||
* Sets the replacer character.
|
||||
*
|
||||
* @param replacer the character to replace the replaced character
|
||||
* @throws InvalidCharacterException if the character is not a letter or is invalid
|
||||
*/
|
||||
protected void setReplacer(char replacer) throws InvalidCharacterException{
|
||||
logger.debug("Setting replacer");
|
||||
logger.debug("Original character {}", replacer);
|
||||
@@ -112,7 +150,12 @@ public class Playfair{
|
||||
logger.debug("Setting replacer to {}", replacer);
|
||||
this.replacer = replacer;
|
||||
}
|
||||
//Set the replaced character
|
||||
/**
|
||||
* Sets the replaced character.
|
||||
*
|
||||
* @param replaced the character to be replaced
|
||||
* @throws InvalidCharacterException if the character is not a letter or is invalid
|
||||
*/
|
||||
protected void setReplaced(char replaced) throws InvalidCharacterException{
|
||||
logger.debug("Setting replaced");
|
||||
logger.debug("Original character {}", replaced);
|
||||
@@ -144,7 +187,9 @@ public class Playfair{
|
||||
logger.debug("Setting replaced to {}", replaced);
|
||||
this.replaced = replaced;
|
||||
}
|
||||
//Create the grid from the keyword
|
||||
/**
|
||||
* Creates the grid from the keyword.
|
||||
*/
|
||||
protected void createGrid(){
|
||||
logger.debug("Creating grid from keyword");
|
||||
|
||||
@@ -158,7 +203,14 @@ public class Playfair{
|
||||
|
||||
logger.debug("Grid\n{}", getGrid());
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
/**
|
||||
* Strips invalid characters from the string that needs encoding/decoding.
|
||||
*
|
||||
* @param inputString the input string to be cleaned
|
||||
* @param encoding true if encoding, false if decoding
|
||||
* @throws InvalidCharacterException if an invalid character is found
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void setInputString(String inputString, boolean encoding) throws InvalidCharacterException, InvalidInputException{
|
||||
logger.debug("Setting input string");
|
||||
|
||||
@@ -214,6 +266,11 @@ public class Playfair{
|
||||
throw new InvalidInputException("Input must have at least 1 letter");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Cleans up the input string for encoding.
|
||||
*
|
||||
* @param inputString the input string to be cleaned
|
||||
*/
|
||||
protected void setInputStringEncode(String inputString){
|
||||
logger.debug("Cleaning up input string for encoding");
|
||||
|
||||
@@ -286,7 +343,11 @@ public class Playfair{
|
||||
this.inputString = cleanInput.toString();
|
||||
logger.debug("Cleaned input string '{}'", this.inputString);
|
||||
}
|
||||
//Returns the input string ready for encoding
|
||||
/**
|
||||
* Returns the input string ready for encoding.
|
||||
*
|
||||
* @return the prepared input string
|
||||
*/
|
||||
protected String getPreparedInputString(){
|
||||
logger.debug("Getting input string ready for encoding");
|
||||
|
||||
@@ -297,7 +358,12 @@ public class Playfair{
|
||||
|
||||
return cleanString;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
/**
|
||||
* Strips invalid characters from the keyword and creates the grid.
|
||||
*
|
||||
* @param keyword the keyword to be processed
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
*/
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
logger.debug("Setting keyword");
|
||||
|
||||
@@ -333,7 +399,13 @@ public class Playfair{
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Returns the location of the given character in the grid
|
||||
/**
|
||||
* Returns the location of the given character in the grid.
|
||||
*
|
||||
* @param letter the character whose location is to be found
|
||||
* @return the location of the character in the grid
|
||||
* @throws InvalidInputException if the character is not found in the grid
|
||||
*/
|
||||
protected CharLocation findChar(char letter) throws InvalidInputException{
|
||||
logger.debug("Finding character in grid {}", letter);
|
||||
|
||||
@@ -350,7 +422,13 @@ public class Playfair{
|
||||
//If it was not found something went wrong
|
||||
throw new InvalidInputException("The character '" + letter + "' was not found in the grid");
|
||||
}
|
||||
//Returns the location in the grid of x and y, adjusting for out of bounds
|
||||
/**
|
||||
* Returns the character in the grid at the specified coordinates.
|
||||
*
|
||||
* @param x the x-coordinate
|
||||
* @param y the y-coordinate
|
||||
* @return the character at the specified coordinates
|
||||
*/
|
||||
protected char getGridChar(int x, int y){
|
||||
logger.debug("Getting character from grid[{}][{}]", x, y);
|
||||
|
||||
@@ -365,7 +443,11 @@ public class Playfair{
|
||||
logger.debug("Character {}", letter);
|
||||
return letter;
|
||||
}
|
||||
//Adds characters that aren't letters to the output
|
||||
/**
|
||||
* Adds characters that aren't letters to the output.
|
||||
*
|
||||
* @param cleanString the cleaned string to be formatted
|
||||
*/
|
||||
protected void addCharactersToCleanString(String cleanString){
|
||||
logger.debug("Formatting output string");
|
||||
|
||||
@@ -394,7 +476,11 @@ public class Playfair{
|
||||
outputString = fullOutput.toString();
|
||||
logger.debug("Formatted output '{}'", outputString);
|
||||
}
|
||||
//Encodes inputString using the Playfair cipher and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the Playfair cipher and stores the result in the output string.
|
||||
*
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void encode() throws InvalidInputException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -438,7 +524,11 @@ public class Playfair{
|
||||
//Add other characters to the output string
|
||||
addCharactersToCleanString(output.toString());
|
||||
}
|
||||
//Decodes inputString using the Playfair cipher and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the Playfair cipher and stores the result in the output string.
|
||||
*
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void decode() throws InvalidInputException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -483,7 +573,12 @@ public class Playfair{
|
||||
addCharactersToCleanString(output.toString());
|
||||
}
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a Playfair cipher instance with default settings.
|
||||
*
|
||||
* @throws InvalidCharacterException if default characters are invalid
|
||||
*/
|
||||
public Playfair() throws InvalidCharacterException{
|
||||
reset();
|
||||
preserveCapitals = false;
|
||||
@@ -493,6 +588,14 @@ public class Playfair{
|
||||
setReplacer('i');
|
||||
setDoubled('x');
|
||||
}
|
||||
/**
|
||||
* Constructs a Playfair cipher instance with specified settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capital letters
|
||||
* @param preserveWhitespace whether to preserve whitespace
|
||||
* @param preserveSymbols whether to preserve symbols
|
||||
* @throws InvalidCharacterException if default characters are invalid
|
||||
*/
|
||||
public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
reset();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
@@ -502,6 +605,17 @@ public class Playfair{
|
||||
setReplacer('i');
|
||||
setDoubled('x');
|
||||
}
|
||||
/**
|
||||
* Constructs a Playfair cipher instance with specified settings and characters.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capital letters
|
||||
* @param preserveWhitespace whether to preserve whitespace
|
||||
* @param preserveSymbols whether to preserve symbols
|
||||
* @param replaced the character to be replaced
|
||||
* @param replacer the character to replace the replaced character
|
||||
* @param doubled the character to use for doubled letters
|
||||
* @throws InvalidCharacterException if any character is invalid
|
||||
*/
|
||||
public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char replaced, char replacer, char doubled) throws InvalidCharacterException{
|
||||
reset();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
@@ -512,7 +626,15 @@ public class Playfair{
|
||||
setDoubled(doubled);
|
||||
}
|
||||
|
||||
//Sets the keyword and inputString and encodes the message
|
||||
/**
|
||||
* Sets the keyword and input string and encodes the message.
|
||||
*
|
||||
* @param keyword the keyword for the cipher
|
||||
* @param input the message to encode
|
||||
* @return the encoded message
|
||||
* @throws InvalidCharacterException if any character is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
@@ -520,7 +642,15 @@ public class Playfair{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Sets the keyword and inputString and decodes the message
|
||||
/**
|
||||
* Sets the keyword and input string and decodes the message.
|
||||
*
|
||||
* @param keyword the keyword for the cipher
|
||||
* @param input the encoded message to decode
|
||||
* @return the decoded message
|
||||
* @throws InvalidCharacterException if any character is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
@@ -529,7 +659,9 @@ public class Playfair{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all variables to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
@@ -538,25 +670,60 @@ public class Playfair{
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
}
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Returns the replaced character.
|
||||
*
|
||||
* @return the replaced character
|
||||
*/
|
||||
public char getReplaced(){
|
||||
return replaced;
|
||||
}
|
||||
/**
|
||||
* Returns the replacer character.
|
||||
*
|
||||
* @return the replacer character
|
||||
*/
|
||||
public char getReplacer(){
|
||||
return replacer;
|
||||
}
|
||||
/**
|
||||
* Returns the doubled character.
|
||||
*
|
||||
* @return the doubled character
|
||||
*/
|
||||
public char getDoubled(){
|
||||
return doubled;
|
||||
}
|
||||
/**
|
||||
* Returns the keyword used in the cipher.
|
||||
*
|
||||
* @return the keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
/**
|
||||
* Returns the input string that was set for encoding/decoding.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Returns the output string after encoding/decoding.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Return the grid used for encoding/decoding.
|
||||
*
|
||||
* @return the grid as a string
|
||||
*/
|
||||
public String getGrid(){
|
||||
logger.debug("Creating string from grid");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/PolybiusSquare.java
|
||||
//Mattrixwv
|
||||
// Created: 01-04-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -15,41 +11,77 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
/**
|
||||
* Represents the Polybius square cipher encryption and decryption.
|
||||
* The Polybius square cipher is a classical encryption method that uses a 5x5 grid
|
||||
* to encode and decode messages based on the positions of letters in the grid.
|
||||
*/
|
||||
public class PolybiusSquare{
|
||||
private static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
|
||||
|
||||
//A class representing the location of a character in the grid
|
||||
/** A class representing the location of a character in the grid */
|
||||
protected class CharLocation{
|
||||
/** The x location in the grid */
|
||||
private int x;
|
||||
/** The y location in the grid */
|
||||
private int y;
|
||||
/**
|
||||
* Constructs a CharLocation with the specified x and y coordinates.
|
||||
*
|
||||
* @param x the x-coordinate of the character's location
|
||||
* @param y the y-coordinate of the character's location
|
||||
*/
|
||||
public CharLocation(int x, int y){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
/**
|
||||
* Returns the x-coordinate of the character's location.
|
||||
*
|
||||
* @return the x-coordinate
|
||||
*/
|
||||
public int getX(){
|
||||
return x;
|
||||
}
|
||||
/**
|
||||
* Returns the y-coordinate of the character's location.
|
||||
*
|
||||
* @return the y-coordinate
|
||||
*/
|
||||
public int getY(){
|
||||
return y;
|
||||
}
|
||||
}
|
||||
|
||||
//Fields
|
||||
protected String inputString; //The message that needs to be encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
protected String keyword; //The keyword used to create the grid
|
||||
protected char[][] grid; //The grid used to encode/decode the message
|
||||
protected char replaced; //The letter that will need to be replaced in the grid and any input string or keyword
|
||||
protected char replacer; //The letter that replaces replaced in the input string or keyword
|
||||
//Settings
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//?Fields
|
||||
/** The message that needs to be encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded message */
|
||||
protected String outputString;
|
||||
/** The keyword used to create the grid */
|
||||
protected String keyword;
|
||||
/** The grid used to encode/decode the message */
|
||||
protected char[][] grid;
|
||||
/** The letter that will need to be replaced in the grid and any input string or keyword */
|
||||
protected char replaced;
|
||||
/** The letter that replaces replaced in the input string or keyword */
|
||||
protected char replacer;
|
||||
//?Settings
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
|
||||
|
||||
//Setting the character to be replaced
|
||||
protected void setReplaced(char replaced) throws InvalidCharacterException{
|
||||
logger.debug("Setting replaced");
|
||||
logger.debug("Original character {}", replaced);
|
||||
/**
|
||||
* Validates the Replaced character.
|
||||
*
|
||||
* @param replaced the character to be validated
|
||||
* @throws InvalidCharacterException if the character is not a letter or is invalid
|
||||
* @return the validated character
|
||||
*/
|
||||
private char validateReplaced(char replaced) throws InvalidCharacterException{
|
||||
logger.debug("Validating replaced character {}", replaced);
|
||||
|
||||
if(!Character.isAlphabetic(replaced)){
|
||||
throw new InvalidCharacterException("The replaced character must be a letter");
|
||||
@@ -60,13 +92,30 @@ public class PolybiusSquare{
|
||||
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
|
||||
}
|
||||
|
||||
this.replaced = Character.toUpperCase(replaced);
|
||||
logger.debug("Cleaned character {}", this.replaced);
|
||||
replaced = Character.toUpperCase(replaced);
|
||||
logger.debug("Cleaned character {}", replaced);
|
||||
return replaced;
|
||||
}
|
||||
//Setting the character that replaces replaced
|
||||
protected void setReplacer(char replacer) throws InvalidCharacterException{
|
||||
logger.debug("Setting replacer");
|
||||
logger.debug("Original character {}", replacer);
|
||||
/**
|
||||
* Sets the replaced character.
|
||||
*
|
||||
* @param replaced the character to be replaced
|
||||
* @throws InvalidCharacterException if the character is not a letter or is invalid
|
||||
*/
|
||||
protected void setReplaced(char replaced) throws InvalidCharacterException{
|
||||
logger.debug("Setting replaced");
|
||||
|
||||
this.replaced = validateReplaced(replaced);
|
||||
}
|
||||
/**
|
||||
* Validates the Replacer character.
|
||||
*
|
||||
* @param replacer the character to be validated
|
||||
* @throws InvalidCharacterException if the character is not a letter or is invalid
|
||||
* @return the validated character
|
||||
*/
|
||||
private char validateReplacer(char replacer) throws InvalidCharacterException{
|
||||
logger.debug("Validating replacer character {}", replacer);
|
||||
|
||||
if(!Character.isAlphabetic(replacer)){
|
||||
throw new InvalidCharacterException("The replacer character must be a letter");
|
||||
@@ -77,10 +126,24 @@ public class PolybiusSquare{
|
||||
throw new InvalidCharacterException("The replacer letter cannot be the same as the replaced letter");
|
||||
}
|
||||
|
||||
this.replacer = Character.toUpperCase(replacer);
|
||||
logger.debug("Cleaned character {}", this.replacer);
|
||||
replacer = Character.toUpperCase(replacer);
|
||||
logger.debug("Cleaned character {}", replacer);
|
||||
return replacer;
|
||||
}
|
||||
//Create the grid from the keyword
|
||||
/**
|
||||
* Sets the replacer character.
|
||||
*
|
||||
* @param replacer the character the replaces replaced
|
||||
* @throws InvalidCharacterException if the character is not a letter or is invalid
|
||||
*/
|
||||
protected void setReplacer(char replacer) throws InvalidCharacterException{
|
||||
logger.debug("Setting replacer");
|
||||
|
||||
this.replacer = validateReplacer(replacer);
|
||||
}
|
||||
/**
|
||||
* Creates the grid from the keyword.
|
||||
*/
|
||||
protected void createGrid(){
|
||||
logger.debug("Creating grid from keyword");
|
||||
|
||||
@@ -93,7 +156,13 @@ public class PolybiusSquare{
|
||||
|
||||
logger.debug("Created grid\n{}", getGrid());
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
/**
|
||||
* Strips invalid characters from the string that needs encoding/decoding.
|
||||
*
|
||||
* @param inputString the input string to be cleaned
|
||||
* @throws InvalidCharacterException if an invalid character is found
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void setInputStringEncode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -148,6 +217,13 @@ public class PolybiusSquare{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Strips invalid characters from the string that needs decoding.
|
||||
*
|
||||
* @param inputString the input string to be cleaned
|
||||
* @throws InvalidCharacterException if an invalid character is found
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -193,7 +269,11 @@ public class PolybiusSquare{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Returns the input string ready for encoding
|
||||
/**
|
||||
* Returns the input string ready for encoding.
|
||||
*
|
||||
* @return the prepared input string
|
||||
*/
|
||||
protected String getPreparedInputStringEncode(){
|
||||
logger.debug("Preparing input string for encoding");
|
||||
|
||||
@@ -203,6 +283,11 @@ public class PolybiusSquare{
|
||||
logger.debug("Prepared string '{}'", cleanString);
|
||||
return cleanString;
|
||||
}
|
||||
/**
|
||||
* Returns the input string ready for decoding.
|
||||
*
|
||||
* @return the prepared input string
|
||||
*/
|
||||
protected String getPreparedInputStringDecode(){
|
||||
logger.debug("Preparing input string for decoding");
|
||||
|
||||
@@ -211,7 +296,11 @@ public class PolybiusSquare{
|
||||
logger.debug("Prepared string '{}'", cleanString);
|
||||
return cleanString;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
/**
|
||||
* Strips invalid characters from the keyword and creates the grid.
|
||||
*
|
||||
* @param keyword the keyword to be processed
|
||||
*/
|
||||
protected void setKeyword(String keyword){
|
||||
if(keyword == null){
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
@@ -244,7 +333,13 @@ public class PolybiusSquare{
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Returns the location of the given charcter in the grid
|
||||
/**
|
||||
* Returns the location of the given character in the grid.
|
||||
*
|
||||
* @param letter the character whose location is to be found
|
||||
* @return the location of the character in the grid
|
||||
* @throws InvalidInputException if the character is not found in the grid
|
||||
*/
|
||||
protected CharLocation findChar(char letter) throws InvalidInputException{
|
||||
logger.debug("Finding {} in grid", letter);
|
||||
|
||||
@@ -260,7 +355,11 @@ public class PolybiusSquare{
|
||||
//If it was not found something went wrong
|
||||
throw new InvalidInputException("The character '" + letter + "' was not found in the grid");
|
||||
}
|
||||
//Adds characters that aren't letters to the output
|
||||
/**
|
||||
* Adds characters that aren't letters to the output during encoding.
|
||||
*
|
||||
* @param cleanString the cleaned string to be formatted
|
||||
*/
|
||||
protected void addCharactersToCleanStringEncode(String cleanString){
|
||||
logger.debug("Formatting output string for encoding");
|
||||
|
||||
@@ -287,6 +386,11 @@ public class PolybiusSquare{
|
||||
outputString = fullOutput.toString();
|
||||
logger.debug("Formatted output '{}'", outputString);
|
||||
}
|
||||
/**
|
||||
* Adds characters that aren't letters to the output during decoding.
|
||||
*
|
||||
* @param cleanString the cleaned string to be formatted
|
||||
*/
|
||||
protected void addCharactersToCleanStringDecode(String cleanString){
|
||||
logger.debug("Formatting output string for decoding");
|
||||
|
||||
@@ -313,7 +417,11 @@ public class PolybiusSquare{
|
||||
outputString = fullOutput.toString();
|
||||
logger.debug("Formatted output '{}'", outputString);
|
||||
}
|
||||
//Encodes inputString using the Playfair cipher and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the Polybius cipher and stores the result in the output string.
|
||||
*
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void encode() throws InvalidInputException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -337,7 +445,11 @@ public class PolybiusSquare{
|
||||
//Add other characters to the output string
|
||||
addCharactersToCleanStringEncode(output.toString());
|
||||
}
|
||||
//Decodes inputString using the Playfair cipher and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the Polybius cipher and stores the result in the output string.
|
||||
*
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -363,33 +475,79 @@ public class PolybiusSquare{
|
||||
addCharactersToCleanStringDecode(output.toString());
|
||||
}
|
||||
|
||||
//Constructors
|
||||
//?Constructors
|
||||
/**
|
||||
* Constructs a PolybiusSquare cipher instance with default settings.
|
||||
*
|
||||
* @throws InvalidCharacterException if default characters are invalid
|
||||
*/
|
||||
public PolybiusSquare() throws InvalidCharacterException{
|
||||
reset();
|
||||
setReplaced('J');
|
||||
setReplacer('I');
|
||||
grid = new char[5][5];
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
this.replaced = validateReplaced('J');
|
||||
this.replacer = validateReplacer('I');
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
}
|
||||
/**
|
||||
* Constructs a PolybiusSquare cipher instance with specified settings.
|
||||
*
|
||||
* @param preserveWhitespace whether to preserve whitespace
|
||||
* @param preserveSymbols whether to preserve symbols
|
||||
* @throws InvalidCharacterException if default characters are invalid
|
||||
*/
|
||||
public PolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
reset();
|
||||
setReplaced('J');
|
||||
setReplacer('I');
|
||||
grid = new char[5][5];
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
this.replaced = validateReplaced('J');
|
||||
this.replacer = validateReplacer('I');
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
}
|
||||
/**
|
||||
* Constructs a PolybiusSquare cipher instance with specified settings and characters.
|
||||
*
|
||||
* @param preserveWhitespace whether to preserve whitespace
|
||||
* @param preserveSymbols whether to preserve symbols
|
||||
* @param replaced the character to be replaced
|
||||
* @param replacer the character to replace the replaced character
|
||||
* @throws InvalidCharacterException if any character is invalid
|
||||
*/
|
||||
public PolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols, char replaced, char replacer) throws InvalidCharacterException{
|
||||
reset();
|
||||
setReplaced(replaced);
|
||||
setReplacer(replacer);
|
||||
grid = new char[5][5];
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
this.replaced = validateReplaced(replaced);
|
||||
this.replacer = validateReplacer(replacer);
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
}
|
||||
|
||||
//Sets the keyword and inputString and encodes the message
|
||||
/**
|
||||
* Sets the keyword and input string and encodes the message.
|
||||
*
|
||||
* @param inputString the message to encode
|
||||
* @return the encoded message
|
||||
* @throws InvalidCharacterException if any character is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
return encode("", inputString);
|
||||
}
|
||||
/**
|
||||
* Sets the keyword and input string and encodes the message.
|
||||
*
|
||||
* @param keyword the keyword for the cipher
|
||||
* @param inputString the message to encode
|
||||
* @return the encoded message
|
||||
* @throws InvalidCharacterException if any character is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
@@ -397,10 +555,26 @@ public class PolybiusSquare{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Sets the keyword and inputString and decodes the message
|
||||
/**
|
||||
* Sets the keyword and input string and decodes the message.
|
||||
*
|
||||
* @param inputString the encoded message to decode
|
||||
* @return the decoded message
|
||||
* @throws InvalidCharacterException if any character is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
return decode("", inputString);
|
||||
}
|
||||
/**
|
||||
* Sets the keyword and input string and decodes the message.
|
||||
*
|
||||
* @param keyword the keyword for the cipher
|
||||
* @param inputString the encoded message to decode
|
||||
* @return the decoded message
|
||||
* @throws InvalidCharacterException if any character is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
@@ -409,7 +583,9 @@ public class PolybiusSquare{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all variables to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
@@ -419,22 +595,52 @@ public class PolybiusSquare{
|
||||
keyword = "";
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Returns the replaced character.
|
||||
*
|
||||
* @return the replaced character
|
||||
*/
|
||||
public char getReplaced(){
|
||||
return replaced;
|
||||
}
|
||||
/**
|
||||
* Returns the replacer character.
|
||||
*
|
||||
* @return the replacer character
|
||||
*/
|
||||
public char getReplacer(){
|
||||
return replacer;
|
||||
}
|
||||
/**
|
||||
* Returns the keyword used in the cipher.
|
||||
*
|
||||
* @return the keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
/**
|
||||
* Returns the input string that was set for encoding/decoding.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Returns the output string after encoding/decoding.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Returns a string representation of the grid.
|
||||
*
|
||||
* @return the grid as a string
|
||||
*/
|
||||
public String getGrid(){
|
||||
logger.debug("Creating string from grid");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/RailFence.java
|
||||
//Mattrixwv
|
||||
// Created: 03-21-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -14,19 +10,35 @@ import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class RailFence{
|
||||
/**
|
||||
* Represents the Rail Fence cipher encryption and decryption.
|
||||
* The Rail Fence cipher is a form of transposition cipher that writes the message in a zigzag pattern
|
||||
* across multiple "rails" and then reads it off row by row to encode or decode the message.
|
||||
*/
|
||||
public final class RailFence{
|
||||
private static final Logger logger = LoggerFactory.getLogger(RailFence.class);
|
||||
//Fields
|
||||
protected String inputString; //The message that needs to be encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
protected StringBuilder[] fence; //The fence used for encoding/decoding
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//?Fields
|
||||
/** The message that needs to be encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded message */
|
||||
protected String outputString;
|
||||
/** The fence used for encoding/decoding */
|
||||
protected StringBuilder[] fence;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
|
||||
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
/**
|
||||
* Strips invalid characters from the string that needs to be encoded/decoded.
|
||||
*
|
||||
* @param inputString the input string to be cleaned
|
||||
* @throws InvalidInputException if the input string is null or invalid
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
//Ensure the input string isn't null
|
||||
if(inputString == null){
|
||||
@@ -61,7 +73,12 @@ public class RailFence{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Ensures the number of rails is valid and sets up the fence
|
||||
/**
|
||||
* Ensures the number of rails is valid and sets up the fence.
|
||||
*
|
||||
* @param numRails the number of rails to be used
|
||||
* @throws InvalidBaseException if the number of rails is less than 2
|
||||
*/
|
||||
protected void setNumRails(int numRails) throws InvalidBaseException{
|
||||
if(numRails < 2){
|
||||
throw new InvalidBaseException("You must use at least 2 rails");
|
||||
@@ -74,13 +91,21 @@ public class RailFence{
|
||||
fence[cnt] = new StringBuilder();
|
||||
}
|
||||
}
|
||||
//Strip the inputString of all non-letter characters
|
||||
/**
|
||||
* Strips the input string of all non-letter characters.
|
||||
*
|
||||
* @return the cleaned input string
|
||||
*/
|
||||
protected String getCleanInputString(){
|
||||
logger.debug("Getting input string for encoding");
|
||||
|
||||
return inputString.replaceAll("[^a-zA-Z]", "");
|
||||
}
|
||||
//Ensures capitals, lowercase, and symbols are displayed in the output string
|
||||
/**
|
||||
* Ensures capitals, lowercase, and symbols are displayed in the output string.
|
||||
*
|
||||
* @param outputString the encoded/decoded output string to be formatted
|
||||
*/
|
||||
protected void formatOutput(String outputString){
|
||||
logger.debug("Formatting output string");
|
||||
|
||||
@@ -109,7 +134,11 @@ public class RailFence{
|
||||
this.outputString = output.toString();
|
||||
logger.debug("Formatted output '{}'", this.outputString);
|
||||
}
|
||||
//Returns the decoded string found in the fence after all characters are placed correctly
|
||||
/**
|
||||
* Returns the decoded string found in the fence after all characters are placed correctly.
|
||||
*
|
||||
* @return the decoded string
|
||||
*/
|
||||
protected String getDecodedStringFromFence(){
|
||||
logger.debug("Getting decoded string from the fence");
|
||||
|
||||
@@ -161,7 +190,9 @@ public class RailFence{
|
||||
logger.debug("Fence output '{}'", output);
|
||||
return output.toString();
|
||||
}
|
||||
//Encodes inputString using the RailFence cipher and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the Rail Fence cipher and stores the result in the output string.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -204,7 +235,9 @@ public class RailFence{
|
||||
//Format the output
|
||||
formatOutput(output.toString());
|
||||
}
|
||||
//Decodes inputString using the RailFence cipher and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the Rail Fence cipher and stores the result in the output string.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -256,13 +289,23 @@ public class RailFence{
|
||||
formatOutput(output);
|
||||
}
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a RailFence cipher instance with default settings.
|
||||
*/
|
||||
public RailFence(){
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a RailFence cipher instance with specified settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve uppercase letters
|
||||
* @param preserveWhitespace whether to preserve whitespace
|
||||
* @param preserveSymbols whether to preserve symbols
|
||||
*/
|
||||
public RailFence(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -270,7 +313,15 @@ public class RailFence{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using a Rail Fence of length numRails and returns the result
|
||||
/**
|
||||
* Encodes the input string using a Rail Fence of the specified number of rails and returns the result.
|
||||
*
|
||||
* @param numRails the number of rails to use
|
||||
* @param inputString the message to encode
|
||||
* @return the encoded message
|
||||
* @throws InvalidBaseException if the number of rails is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(int numRails, String inputString) throws InvalidBaseException, InvalidInputException{
|
||||
//Set the parameters
|
||||
setNumRails(numRails);
|
||||
@@ -280,7 +331,15 @@ public class RailFence{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using a Rail Fence of length numRails and returns the result
|
||||
/**
|
||||
* Decodes the input string using a Rail Fence of the specified number of rails and returns the result.
|
||||
*
|
||||
* @param numRails the number of rails to use
|
||||
* @param inputString the encoded message to decode
|
||||
* @return the decoded message
|
||||
* @throws InvalidBaseException if the number of rails is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(int numRails, String inputString) throws InvalidBaseException, InvalidInputException{
|
||||
//Set the parameters
|
||||
setNumRails(numRails);
|
||||
@@ -291,7 +350,9 @@ public class RailFence{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all variables to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
@@ -300,13 +361,28 @@ public class RailFence{
|
||||
fence = null;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Returns the input string that was set for encoding/decoding.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Returns the output string after encoding/decoding.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Returns the number of rails used in the Rail Fence cipher.
|
||||
*
|
||||
* @return the number of rails
|
||||
*/
|
||||
public int getNumRails(){
|
||||
return fence.length;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Trifid.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -17,46 +13,92 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Trifid{
|
||||
/**
|
||||
* The {@code Trifid} class implements the Trifid cipher, a polyalphabetic substitution cipher
|
||||
* that uses a 3x3x3 grid to encode and decode messages.
|
||||
*/
|
||||
public final class Trifid{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Trifid.class);
|
||||
|
||||
//A class representing the location of a character in the grid
|
||||
/** A class representing the location of a character in the grid */
|
||||
protected class CharLocation{
|
||||
/** The x location in the grid */
|
||||
protected int x;
|
||||
/** The y location in the grid */
|
||||
protected int y;
|
||||
/** The z location in the grid */
|
||||
protected int z;
|
||||
/**
|
||||
* Constructs a CharLocation with the specified x and y coordinates.
|
||||
*
|
||||
* @param x the x-coordinate of the character's location
|
||||
* @param y the y-coordinate of the character's location
|
||||
* @param z the z-coordinate of the character's location
|
||||
*/
|
||||
public CharLocation(int x, int y, int z){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
/**
|
||||
* Returns the x-coordinate of the character's location.
|
||||
*
|
||||
* @return the x-coordinate
|
||||
*/
|
||||
public int getX(){
|
||||
return x;
|
||||
}
|
||||
/**
|
||||
* Returns the y-coordinate of the character's location.
|
||||
*
|
||||
* @return the y-coordinate
|
||||
*/
|
||||
public int getY(){
|
||||
return y;
|
||||
}
|
||||
/**
|
||||
* Returns the z-coordinate of the character's location.
|
||||
*
|
||||
* @return the z-coordinate
|
||||
*/
|
||||
public int getZ(){
|
||||
return z;
|
||||
}
|
||||
/**
|
||||
* Prints out the location in a human readable format
|
||||
*/
|
||||
public String toString(){
|
||||
return "[" + (z + 1) + ", " + (x + 1) + ", " + (y + 1) + "]";
|
||||
}
|
||||
}
|
||||
|
||||
//Fields
|
||||
protected String inputString; //The message that needs to be encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
protected String keyword; //The keyword used to create the grid
|
||||
protected int groupSize; //The size of the groups used to break up the input
|
||||
protected char[][][] grid; //The grid used to encode/decode the message
|
||||
protected char fillIn; //The character added to the alphabet to meet the 27 character requirement
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//?Fields
|
||||
/** The message that needs to be encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded message */
|
||||
protected String outputString;
|
||||
/** The keyword used to create the grid */
|
||||
protected String keyword;
|
||||
/** The size of the groups used to break up the input */
|
||||
protected int groupSize;
|
||||
/** The grid used to encode/decode the message */
|
||||
protected char[][][] grid;
|
||||
/** The character added to the alphabet to meet the 27 character requirement */
|
||||
protected char fillIn;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
|
||||
//Makes sure the fillIn is a valid character
|
||||
/**
|
||||
* Sets the fill-in character used to complete the 27-character requirement for the grid.
|
||||
*
|
||||
* @param fillIn the fill-in character
|
||||
* @throws InvalidCharacterException if the fill-in character is not a printable non-letter character
|
||||
*/
|
||||
protected void setFillIn(char fillIn) throws InvalidCharacterException{
|
||||
//Make sure the character is a printing character
|
||||
if((fillIn < ' ') || (fillIn > '~')){
|
||||
@@ -72,7 +114,13 @@ public class Trifid{
|
||||
//Save the fillIn character
|
||||
this.fillIn = fillIn;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
/**
|
||||
* Processes the keyword to generate the 3D grid. The keyword is sanitized to include
|
||||
* only capital letters and the fill-in character.
|
||||
*
|
||||
* @param keyword the keyword used to create the grid
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
*/
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Ensure the keyword isn't null
|
||||
if(keyword == null){
|
||||
@@ -104,7 +152,9 @@ public class Trifid{
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Creates the grid from the keyword
|
||||
/**
|
||||
* Creates the 3D grid from the processed keyword.
|
||||
*/
|
||||
protected void createGrid(){
|
||||
logger.debug("Creating grid from keyword");
|
||||
|
||||
@@ -120,7 +170,12 @@ public class Trifid{
|
||||
|
||||
logger.debug("Completed grid\n{}", getGrid());
|
||||
}
|
||||
//Ensures groupSize constraints
|
||||
/**
|
||||
* Sets the group size used for encoding and decoding the input string.
|
||||
*
|
||||
* @param groupSize the group size
|
||||
* @throws InvalidBaseException if the group size is less than or equal to zero
|
||||
*/
|
||||
protected void setGroupSize(int groupSize) throws InvalidBaseException{
|
||||
if(groupSize <= 0){
|
||||
throw new InvalidBaseException("Group size must be > 0");
|
||||
@@ -130,7 +185,13 @@ public class Trifid{
|
||||
|
||||
this.groupSize = groupSize;
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
/**
|
||||
* Prepares the input string for encoding or decoding by applying preservation settings
|
||||
* and validating the input.
|
||||
*
|
||||
* @param inputString the input string to be processed
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
//Ensure the input string isn't null
|
||||
if(inputString == null){
|
||||
@@ -165,12 +226,22 @@ public class Trifid{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Returns the inputString with only letters
|
||||
/**
|
||||
* Returns the input string with only valid letters and fill-in characters.
|
||||
*
|
||||
* @return the cleaned input string
|
||||
*/
|
||||
protected String getCleanInputString(){
|
||||
logger.debug("Cleaning input string for encoding");
|
||||
return inputString.toUpperCase().replaceAll("[^A-Z" + fillIn + "]", "");
|
||||
}
|
||||
//Returns the location of the given character in the grid
|
||||
/**
|
||||
* Finds the location of a given character in the 3D grid.
|
||||
*
|
||||
* @param letter the character to find
|
||||
* @return the location of the character
|
||||
* @throws InvalidCharacterException if the character is not found in the grid
|
||||
*/
|
||||
protected CharLocation findChar(char letter) throws InvalidCharacterException{
|
||||
logger.debug("Finding character {} in grid", letter);
|
||||
|
||||
@@ -189,7 +260,13 @@ public class Trifid{
|
||||
//If it was not found something went wrong
|
||||
throw new InvalidCharacterException("The character '" + letter + "' was not found in the grid");
|
||||
}
|
||||
//Return the character from the location provided
|
||||
/**
|
||||
* Retrieves the character at the specified location in the 3D grid.
|
||||
*
|
||||
* @param location the location of the character
|
||||
* @return the character at the specified location
|
||||
* @throws InvalidCharacterException if the location is out of bounds
|
||||
*/
|
||||
protected char getChar(CharLocation location) throws InvalidCharacterException{
|
||||
if(location.getX() > 2){
|
||||
throw new InvalidCharacterException("x cannot be larget than 2");
|
||||
@@ -205,7 +282,11 @@ public class Trifid{
|
||||
|
||||
return grid[location.getZ()][location.getX()][location.getY()];
|
||||
}
|
||||
//Adds all non-letter characters back to the output string
|
||||
/**
|
||||
* Formats the output string according to the preservation settings.
|
||||
*
|
||||
* @param outputString the output string to format
|
||||
*/
|
||||
protected void formatOutput(String outputString){
|
||||
logger.debug("Formatting output");
|
||||
|
||||
@@ -242,7 +323,11 @@ public class Trifid{
|
||||
this.outputString = output.toString();
|
||||
logger.debug("Formatted output '{}'", this.outputString);
|
||||
}
|
||||
//Encodes inputString using a polybius square and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the Trifid cipher.
|
||||
*
|
||||
* @throws InvalidCharacterException if an invalid character is encountered
|
||||
*/
|
||||
protected void encode() throws InvalidCharacterException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -308,7 +393,11 @@ public class Trifid{
|
||||
//Format the output
|
||||
formatOutput(output.toString());
|
||||
}
|
||||
//Decodes inputString using a polybius square and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the Trifid cipher.
|
||||
*
|
||||
* @throws InvalidCharacterException if an invalid character is encountered
|
||||
*/
|
||||
protected void decode() throws InvalidCharacterException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -380,7 +469,10 @@ public class Trifid{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a Trifid object with default settings.
|
||||
*/
|
||||
public Trifid() throws InvalidCharacterException{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
@@ -388,6 +480,13 @@ public class Trifid{
|
||||
setFillIn('+');
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a Trifid object with default settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capital letters
|
||||
* @param preserveWhitespace whether to preserve whitespace
|
||||
* @param preserveSymbols whether to preserve symbols
|
||||
*/
|
||||
public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -395,6 +494,15 @@ public class Trifid{
|
||||
setFillIn('+');
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a Trifid object with default settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capital letters
|
||||
* @param preserveWhitespace whether to preserve whitespace
|
||||
* @param preserveSymbols whether to preserve symbols
|
||||
* @param fillIn the character to use for fill-in characters
|
||||
* @throws InvalidCharacterException if the fill-in character is invalid
|
||||
*/
|
||||
public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char fillIn) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -403,10 +511,32 @@ public class Trifid{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using keyword and groupSize and returns the result
|
||||
/**
|
||||
* Encodes the specified input string using the provided keyword and group size.
|
||||
*
|
||||
* @param keyword the keyword used for encoding
|
||||
* @param inputString the input string to encode
|
||||
* @return the encoded string
|
||||
* @throws InvalidBaseException if the group size is invalid
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
* @throws InvalidCharacterException if an invalid character is encountered
|
||||
*/
|
||||
public String encode(String keyword, String inputString) throws InvalidBaseException, InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
return encode(keyword, inputString.length(), inputString);
|
||||
}
|
||||
/**
|
||||
* Encodes the specified input string using the provided keyword and group size.
|
||||
*
|
||||
* @param keyword the keyword used for encoding
|
||||
* @param groupSize the size of groups
|
||||
* @param inputString the input string to encode
|
||||
* @return the encoded string
|
||||
* @throws InvalidBaseException if the group size is invalid
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
* @throws InvalidCharacterException if an invalid character is encountered
|
||||
*/
|
||||
public String encode(String keyword, int groupSize, String inputString) throws InvalidBaseException, InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
@@ -418,10 +548,32 @@ public class Trifid{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using keyword and groupSize and returns the result
|
||||
/**
|
||||
* Decodes the specified input string using the provided keyword and group size.
|
||||
*
|
||||
* @param keyword the keyword used for decoding
|
||||
* @param inputString the input string to decode
|
||||
* @return the decoded string
|
||||
* @throws InvalidBaseException if the group size is invalid
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
* @throws InvalidCharacterException if an invalid character is encountered
|
||||
*/
|
||||
public String decode(String keyword, String inputString) throws InvalidBaseException, InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
return decode(keyword, inputString.length(), inputString);
|
||||
}
|
||||
/**
|
||||
* Decodes the specified input string using the provided keyword and group size.
|
||||
*
|
||||
* @param keyword the keyword used for decoding
|
||||
* @param groupSize the size of groups
|
||||
* @param inputString the input string to decode
|
||||
* @return the decoded string
|
||||
* @throws InvalidBaseException if the group size is invalid
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
* @throws InvalidCharacterException if an invalid character is encountered
|
||||
*/
|
||||
public String decode(String keyword, int groupSize, String inputString) throws InvalidBaseException, InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
@@ -434,7 +586,9 @@ public class Trifid{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all internal variables to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
@@ -445,22 +599,52 @@ public class Trifid{
|
||||
grid = new char[3][3][3];
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Returns the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Returns the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Returns the current keyword.
|
||||
*
|
||||
* @return the keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
/**
|
||||
* Returns the current group size.
|
||||
*
|
||||
* @return the group size
|
||||
*/
|
||||
public int getGroupSize(){
|
||||
return groupSize;
|
||||
}
|
||||
/**
|
||||
* Returns the current fill-in character.
|
||||
*
|
||||
* @return the fill-in character
|
||||
*/
|
||||
public char getFillIn(){
|
||||
return fillIn;
|
||||
}
|
||||
/**
|
||||
* Returns a string representation of the 3D grid.
|
||||
*
|
||||
* @return the grid as a string
|
||||
*/
|
||||
public String getGrid(){
|
||||
logger.debug("Creating string from grid");
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGVX.java
|
||||
//Mattrixwv
|
||||
// Created: 01-26-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.combination;
|
||||
|
||||
|
||||
@@ -29,12 +25,12 @@ public class ADFGVXTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "Message to^encode";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODE";
|
||||
private static final String encodedString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
|
||||
private static final String encodedStringClean = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
|
||||
private static final String keyword = "keyword";
|
||||
private static final String squareKeyword = "SquareKeyword";
|
||||
private static final String DECODED_STRING = "Message to^encode";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String ENCODED_STRING = "AXgvdavfxgagfa afag^aaxdxfgdagda";
|
||||
private static final String ENCODED_STRING_CLEAN = "AXGVDAVFXGAGFAAFAGAAXDXFGDAGDA";
|
||||
private static final String KEYWORD = "keyword";
|
||||
private static final String SQUARE_KEYWORD = "SquareKeyword";
|
||||
|
||||
|
||||
@Test
|
||||
@@ -99,10 +95,10 @@ public class ADFGVXTest{
|
||||
|
||||
@Test
|
||||
public void testSetSquareKeyword(){
|
||||
cipher.setSquareKeyword(squareKeyword);
|
||||
cipher.setSquareKeyword(SQUARE_KEYWORD);
|
||||
|
||||
assertEquals(squareKeyword, cipher.squareKeyword);
|
||||
verify(logger, times(1)).debug("squareKeyword '{}'", squareKeyword);
|
||||
assertEquals(SQUARE_KEYWORD, cipher.squareKeyword);
|
||||
verify(logger, times(1)).debug("squareKeyword '{}'", SQUARE_KEYWORD);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -117,10 +113,10 @@ public class ADFGVXTest{
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keyword, cipher.keyword);
|
||||
verify(logger, times(1)).debug("keyword '{}'", keyword);
|
||||
assertEquals(KEYWORD, cipher.keyword);
|
||||
verify(logger, times(1)).debug("keyword '{}'", KEYWORD);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -139,14 +135,14 @@ public class ADFGVXTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -155,14 +151,14 @@ public class ADFGVXTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -171,14 +167,14 @@ public class ADFGVXTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -187,14 +183,14 @@ public class ADFGVXTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -235,45 +231,45 @@ public class ADFGVXTest{
|
||||
|
||||
@Test
|
||||
public void testFormateOutputStringEncode(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedStringClean;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING_CLEAN;
|
||||
|
||||
cipher.formatOutputStringEncode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output string to match input string");
|
||||
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Converting output to uppercase");
|
||||
verify(logger, times(14)).debug("Converting output to lowercase");
|
||||
verify(logger, times(2)).debug("Appending symbol to output");
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatOutputStringDecode(){
|
||||
cipher.outputString = decodedStringClean;
|
||||
cipher.inputString = encodedString;
|
||||
cipher.outputString = DECODED_STRING_CLEAN;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
|
||||
cipher.formatOutputStringDecode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output string to match input string");
|
||||
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Converting output to uppercase");
|
||||
verify(logger, times(14)).debug("Converting output to lowercase");
|
||||
verify(logger, times(2)).debug("Appending symbol to output");
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.squareKeyword = squareKeyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.squareKeyword = SQUARE_KEYWORD;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding using Polybius Square");
|
||||
verify(logger, times(1)).debug("Replacing coordinates with letters");
|
||||
verify(logger, times(1)).debug("Encoding using columnar");
|
||||
@@ -281,13 +277,13 @@ public class ADFGVXTest{
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.inputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.squareKeyword = squareKeyword;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.squareKeyword = SQUARE_KEYWORD;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding using columnar");
|
||||
verify(logger, times(1)).debug("Replacing letters with coordinates");
|
||||
verify(logger, times(1)).debug("Decoding using Polybius Square");
|
||||
@@ -295,25 +291,25 @@ public class ADFGVXTest{
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.squareKeyword = squareKeyword;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.squareKeyword = SQUARE_KEYWORD;
|
||||
cipher.keyword = KEYWORD;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(squareKeyword, cipher.getSquareKeyword());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
assertEquals(SQUARE_KEYWORD, cipher.getSquareKeyword());
|
||||
assertEquals(KEYWORD, cipher.getKeyword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
LargePolybiusSquare ps = cipher.largePolybiusSquare;
|
||||
Columnar columnar = cipher.columnar;
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.squareKeyword = squareKeyword;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.squareKeyword = SQUARE_KEYWORD;
|
||||
cipher.keyword = KEYWORD;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -330,47 +326,47 @@ public class ADFGVXTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new ADFGVX(true, true, true);
|
||||
|
||||
String output = cipher.encode(squareKeyword, keyword, decodedString);
|
||||
String output = cipher.encode(SQUARE_KEYWORD, KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(keyword, cipher.keyword);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new ADFGVX(false, false, false);
|
||||
|
||||
String output = cipher.encode(squareKeyword, keyword, decodedString);
|
||||
String output = cipher.encode(SQUARE_KEYWORD, KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(keyword, cipher.keyword);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new ADFGVX(true, true, true);
|
||||
|
||||
String output = cipher.decode(squareKeyword, keyword, encodedString);
|
||||
String output = cipher.decode(SQUARE_KEYWORD, KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(keyword, cipher.keyword);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD, cipher.keyword);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new ADFGVX(false, false, false);
|
||||
|
||||
String output = cipher.decode(squareKeyword, keyword, encodedString);
|
||||
String output = cipher.decode(SQUARE_KEYWORD, KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(keyword, cipher.keyword);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/combination/TestADFGX.java
|
||||
//Mattrixwv
|
||||
// Created: 01-25-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.combination;
|
||||
|
||||
|
||||
@@ -29,12 +25,12 @@ public class ADFGXTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "Message to^encode";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODE";
|
||||
private static final String encodedString = "AAgagadfagaxxd axdx^adafafxddgdf";
|
||||
private static final String encodedStringClean = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF";
|
||||
private static final String keyword = "keyword";
|
||||
private static final String squareKeyword = "SquareKeyword";
|
||||
private static final String DECODED_STRING = "Message to^encode";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String ENCODED_STRING = "AAgagadfagaxxd axdx^adafafxddgdf";
|
||||
private static final String ENCODED_STRING_CLEAN = "AAGAGADFAGAXXDAXDXADAFAFXDDGDF";
|
||||
private static final String KEYWORD = "keyword";
|
||||
private static final String SQUARE_KEYWORD = "SquareKeyword";
|
||||
|
||||
|
||||
@Test
|
||||
@@ -99,9 +95,9 @@ public class ADFGXTest{
|
||||
|
||||
@Test
|
||||
public void testSetSquareKeyword(){
|
||||
cipher.setSquareKeyword(squareKeyword);
|
||||
assertEquals(squareKeyword, cipher.squareKeyword);
|
||||
verify(logger, times(1)).debug("Square keyword '{}'", squareKeyword);
|
||||
cipher.setSquareKeyword(SQUARE_KEYWORD);
|
||||
assertEquals(SQUARE_KEYWORD, cipher.squareKeyword);
|
||||
verify(logger, times(1)).debug("Square keyword '{}'", SQUARE_KEYWORD);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -116,10 +112,10 @@ public class ADFGXTest{
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keyword, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Keyword '{}'", keyword);
|
||||
assertEquals(KEYWORD, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Keyword '{}'", KEYWORD);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -138,14 +134,14 @@ public class ADFGXTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -154,14 +150,14 @@ public class ADFGXTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -170,14 +166,14 @@ public class ADFGXTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -186,14 +182,14 @@ public class ADFGXTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -234,45 +230,45 @@ public class ADFGXTest{
|
||||
|
||||
@Test
|
||||
public void testFormatOutputStringEncode(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedStringClean;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING_CLEAN;
|
||||
|
||||
cipher.formatOutputStringEncode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output string to match input string");
|
||||
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Converting output to uppercase");
|
||||
verify(logger, times(14)).debug("Converting output to lowercase");
|
||||
verify(logger, times(2)).debug("Appending symbol to output");
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatOutputStringDecode(){
|
||||
cipher.outputString = decodedStringClean;
|
||||
cipher.inputString = encodedString;
|
||||
cipher.outputString = DECODED_STRING_CLEAN;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
|
||||
cipher.formatOutputStringDecode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output string to match input string");
|
||||
verify(logger, times(17)).debug(eq("Input character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Converting output to uppercase");
|
||||
verify(logger, times(14)).debug("Converting output to lowercase");
|
||||
verify(logger, times(2)).debug("Appending symbol to output");
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.squareKeyword = squareKeyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.squareKeyword = SQUARE_KEYWORD;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding using Polybius Square");
|
||||
verify(logger, times(1)).debug("Replacing coordinates with letters");
|
||||
verify(logger, times(1)).debug("Encoding using columnar");
|
||||
@@ -280,13 +276,13 @@ public class ADFGXTest{
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.inputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.squareKeyword = squareKeyword;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.squareKeyword = SQUARE_KEYWORD;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding using columnar");
|
||||
verify(logger, times(1)).debug("Replacing letters with coordinates");
|
||||
verify(logger, times(1)).debug("Decoding using Polybius Square");
|
||||
@@ -294,25 +290,25 @@ public class ADFGXTest{
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.squareKeyword = squareKeyword;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.squareKeyword = SQUARE_KEYWORD;
|
||||
cipher.keyword = KEYWORD;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(squareKeyword, cipher.getSquareKeyword());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
assertEquals(SQUARE_KEYWORD, cipher.getSquareKeyword());
|
||||
assertEquals(KEYWORD, cipher.getKeyword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
PolybiusSquare polybius = cipher.polybiusSquare;
|
||||
Columnar columnar = cipher.columnar;
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.squareKeyword = squareKeyword;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.squareKeyword = SQUARE_KEYWORD;
|
||||
cipher.keyword = KEYWORD;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -329,47 +325,47 @@ public class ADFGXTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new ADFGX(true, true, true);
|
||||
|
||||
String output = cipher.encode(squareKeyword, keyword, decodedString);
|
||||
String output = cipher.encode(SQUARE_KEYWORD, KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new ADFGX(false, false, false);
|
||||
|
||||
String output = cipher.encode(squareKeyword, keyword, decodedString);
|
||||
String output = cipher.encode(SQUARE_KEYWORD, KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new ADFGX(true, true, true);
|
||||
|
||||
String output = cipher.decode(squareKeyword, keyword, encodedString);
|
||||
String output = cipher.decode(SQUARE_KEYWORD, KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new ADFGX(false, false, false);
|
||||
|
||||
String output = cipher.decode(squareKeyword, keyword, encodedString);
|
||||
String output = cipher.decode(SQUARE_KEYWORD, KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/exceptions/TestInvalidBaseException.java
|
||||
//Mattrixwv
|
||||
// Created: 04-14-23
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.exceptions;
|
||||
|
||||
|
||||
@@ -11,8 +7,8 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
public class InvalidBaseExceptionTest{
|
||||
private static final String message = "message";
|
||||
private static final Throwable cause = new Exception();
|
||||
private static final String MESSAGE = "message";
|
||||
private static final Throwable CAUSE = new Exception();
|
||||
|
||||
|
||||
@Test
|
||||
@@ -24,22 +20,22 @@ public class InvalidBaseExceptionTest{
|
||||
|
||||
@Test
|
||||
public void testConstructor_message(){
|
||||
InvalidBaseException exception = new InvalidBaseException(message);
|
||||
assertEquals(message, exception.getMessage());
|
||||
InvalidBaseException exception = new InvalidBaseException(MESSAGE);
|
||||
assertEquals(MESSAGE, exception.getMessage());
|
||||
assertNull(exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_cause(){
|
||||
InvalidBaseException exception = new InvalidBaseException(cause);
|
||||
assertEquals(cause.toString(), exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
InvalidBaseException exception = new InvalidBaseException(CAUSE);
|
||||
assertEquals(CAUSE.toString(), exception.getMessage());
|
||||
assertEquals(CAUSE, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_messageAndCause(){
|
||||
InvalidBaseException exception = new InvalidBaseException(message, cause);
|
||||
assertEquals(message, exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
InvalidBaseException exception = new InvalidBaseException(MESSAGE, CAUSE);
|
||||
assertEquals(MESSAGE, exception.getMessage());
|
||||
assertEquals(CAUSE, exception.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/exceptions/TestInvalidcharacterException.java
|
||||
//Mattrixwv
|
||||
// Created: 04-14-23
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.exceptions;
|
||||
|
||||
|
||||
@@ -11,8 +7,8 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
public class InvalidCharacterExceptionTest{
|
||||
private String message = "message";
|
||||
private Throwable cause = new Exception();
|
||||
private static final String MESSAGE = "message";
|
||||
private static final Throwable CAUSE = new Exception();
|
||||
|
||||
|
||||
@Test
|
||||
@@ -24,22 +20,22 @@ public class InvalidCharacterExceptionTest{
|
||||
|
||||
@Test
|
||||
public void testConstructor_message(){
|
||||
InvalidCharacterException exception = new InvalidCharacterException(message);
|
||||
assertEquals(message, exception.getMessage());
|
||||
InvalidCharacterException exception = new InvalidCharacterException(MESSAGE);
|
||||
assertEquals(MESSAGE, exception.getMessage());
|
||||
assertNull(exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_cause(){
|
||||
InvalidCharacterException exception = new InvalidCharacterException(cause);
|
||||
assertEquals(cause.toString(), exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
InvalidCharacterException exception = new InvalidCharacterException(CAUSE);
|
||||
assertEquals(CAUSE.toString(), exception.getMessage());
|
||||
assertEquals(CAUSE, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_messageAndCause(){
|
||||
InvalidCharacterException exception = new InvalidCharacterException(message, cause);
|
||||
assertEquals(message, exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
InvalidCharacterException exception = new InvalidCharacterException(MESSAGE, CAUSE);
|
||||
assertEquals(MESSAGE, exception.getMessage());
|
||||
assertEquals(CAUSE, exception.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/exception/TestInvalidInputException.java
|
||||
//Mattrixwv
|
||||
// Created: 04-14-23
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.exceptions;
|
||||
|
||||
|
||||
@@ -11,8 +7,8 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
public class InvalidInputExceptionTest{
|
||||
private String message = "message";
|
||||
private Throwable cause = new Exception();
|
||||
private static final String MESSAGE = "message";
|
||||
private static final Throwable CAUSE = new Exception();
|
||||
|
||||
|
||||
@Test
|
||||
@@ -24,22 +20,22 @@ public class InvalidInputExceptionTest{
|
||||
|
||||
@Test
|
||||
public void testConstructor_message(){
|
||||
InvalidInputException exception = new InvalidInputException(message);
|
||||
assertEquals(message, exception.getMessage());
|
||||
InvalidInputException exception = new InvalidInputException(MESSAGE);
|
||||
assertEquals(MESSAGE, exception.getMessage());
|
||||
assertNull(exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_cause(){
|
||||
InvalidInputException exception = new InvalidInputException(cause);
|
||||
assertEquals(cause.toString(), exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
InvalidInputException exception = new InvalidInputException(CAUSE);
|
||||
assertEquals(CAUSE.toString(), exception.getMessage());
|
||||
assertEquals(CAUSE, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_messageAndCause(){
|
||||
InvalidInputException exception = new InvalidInputException(message, cause);
|
||||
assertEquals(message, exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
InvalidInputException exception = new InvalidInputException(MESSAGE, CAUSE);
|
||||
assertEquals(MESSAGE, exception.getMessage());
|
||||
assertEquals(CAUSE, exception.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/exceptions/TestInvalidKeyException.java
|
||||
//Mattrixwv
|
||||
// Created: 04-14-23
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.exceptions;
|
||||
|
||||
|
||||
@@ -11,8 +7,8 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
public class InvalidKeyExceptionTest{
|
||||
public String message = "message";
|
||||
public Throwable cause = new Exception();
|
||||
public static final String MESSAGE = "message";
|
||||
public static final Throwable CAUSE = new Exception();
|
||||
|
||||
|
||||
@Test
|
||||
@@ -24,22 +20,22 @@ public class InvalidKeyExceptionTest{
|
||||
|
||||
@Test
|
||||
public void testConstructor_message(){
|
||||
InvalidKeyException exception = new InvalidKeyException(message);
|
||||
assertEquals(message, exception.getMessage());
|
||||
InvalidKeyException exception = new InvalidKeyException(MESSAGE);
|
||||
assertEquals(MESSAGE, exception.getMessage());
|
||||
assertNull(exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_cause(){
|
||||
InvalidKeyException exception = new InvalidKeyException(cause);
|
||||
assertEquals(cause.toString(), exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
InvalidKeyException exception = new InvalidKeyException(CAUSE);
|
||||
assertEquals(CAUSE.toString(), exception.getMessage());
|
||||
assertEquals(CAUSE, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_messageAndCause(){
|
||||
InvalidKeyException exception = new InvalidKeyException(message, cause);
|
||||
assertEquals(message, exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
InvalidKeyException exception = new InvalidKeyException(MESSAGE, CAUSE);
|
||||
assertEquals(MESSAGE, exception.getMessage());
|
||||
assertEquals(CAUSE, exception.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/exceptions/TestInvalidKeywordException.java
|
||||
//Mattrixwv
|
||||
// Created: 04-14-23
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.exceptions;
|
||||
|
||||
|
||||
@@ -11,8 +7,8 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
public class InvalidKeywordExceptionTest{
|
||||
private String message = "message";
|
||||
private Throwable cause = new Exception();
|
||||
private static final String MESSAGE = "message";
|
||||
private static final Throwable CAUSE = new Exception();
|
||||
|
||||
|
||||
@Test
|
||||
@@ -24,22 +20,22 @@ public class InvalidKeywordExceptionTest{
|
||||
|
||||
@Test
|
||||
public void testConstructor_message(){
|
||||
InvalidKeywordException exception = new InvalidKeywordException(message);
|
||||
assertEquals(message, exception.getMessage());
|
||||
InvalidKeywordException exception = new InvalidKeywordException(MESSAGE);
|
||||
assertEquals(MESSAGE, exception.getMessage());
|
||||
assertNull(exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_cause(){
|
||||
InvalidKeywordException exception = new InvalidKeywordException(cause);
|
||||
assertEquals(cause.toString(), exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
InvalidKeywordException exception = new InvalidKeywordException(CAUSE);
|
||||
assertEquals(CAUSE.toString(), exception.getMessage());
|
||||
assertEquals(CAUSE, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_messageAndCause(){
|
||||
InvalidKeywordException exception = new InvalidKeywordException(message, cause);
|
||||
assertEquals(message, exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
InvalidKeywordException exception = new InvalidKeywordException(MESSAGE, CAUSE);
|
||||
assertEquals(MESSAGE, exception.getMessage());
|
||||
assertEquals(CAUSE, exception.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/polysubstitution/AffineTest.java
|
||||
//Mattrixwv
|
||||
// Created: 01-26-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -27,12 +23,12 @@ public class AffineTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "MEssage to^encode";
|
||||
private static final String decodedStringClean = "messagetoencode";
|
||||
private static final String encodedString = "PBtthlb yz^burzwb";
|
||||
private static final String encodedStringClean = "pbtthlbyzburzwb";
|
||||
private static final int key1 = 5;
|
||||
private static final int key2 = 7;
|
||||
private static final String DECODED_STRING = "MEssage to^encode";
|
||||
private static final String DECODED_STRING_CLEAN = "messagetoencode";
|
||||
private static final String ENCODED_STRING = "PBtthlb yz^burzwb";
|
||||
private static final String ENCODED_STRING_CLEAN = "pbtthlbyzburzwb";
|
||||
private static final int KEY_1 = 5;
|
||||
private static final int KEY_2 = 7;
|
||||
|
||||
|
||||
@Test
|
||||
@@ -89,11 +85,11 @@ public class AffineTest{
|
||||
|
||||
@Test
|
||||
public void testKey1(){
|
||||
cipher.setKey1(key1);
|
||||
cipher.setKey1(KEY_1);
|
||||
|
||||
assertEquals(key1, cipher.key1);
|
||||
verify(logger, times(1)).debug("Setting key1 {}", key1);
|
||||
verify(logger, times(1)).debug("Cleaned key1 {}", key1);
|
||||
assertEquals(KEY_1, cipher.key1);
|
||||
verify(logger, times(1)).debug("Setting key1 {}", KEY_1);
|
||||
verify(logger, times(1)).debug("Cleaned key1 {}", KEY_1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -117,20 +113,20 @@ public class AffineTest{
|
||||
|
||||
@Test
|
||||
public void testSetKey1_large(){
|
||||
cipher.setKey1(key1 + 26);
|
||||
cipher.setKey1(KEY_1 + 26);
|
||||
|
||||
assertEquals(key1, cipher.key1);
|
||||
verify(logger, times(1)).debug("Setting key1 {}", key1 + 26);
|
||||
verify(logger, times(1)).debug("Cleaned key1 {}", key1);
|
||||
assertEquals(KEY_1, cipher.key1);
|
||||
verify(logger, times(1)).debug("Setting key1 {}", KEY_1 + 26);
|
||||
verify(logger, times(1)).debug("Cleaned key1 {}", KEY_1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKey2(){
|
||||
cipher.setKey2(key2);
|
||||
cipher.setKey2(KEY_2);
|
||||
|
||||
assertEquals(key2, cipher.key2);
|
||||
verify(logger, times(1)).debug("Setting key2 {}", key2);
|
||||
verify(logger, times(1)).debug("Cleaned key2 {}", key2);
|
||||
assertEquals(KEY_2, cipher.key2);
|
||||
verify(logger, times(1)).debug("Setting key2 {}", KEY_2);
|
||||
verify(logger, times(1)).debug("Cleaned key2 {}", KEY_2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -144,11 +140,11 @@ public class AffineTest{
|
||||
|
||||
@Test
|
||||
public void testSetKey2_large(){
|
||||
cipher.setKey2(key2 + 26);
|
||||
cipher.setKey2(KEY_2 + 26);
|
||||
|
||||
assertEquals(key2, cipher.key2);
|
||||
verify(logger, times(1)).debug("Setting key2 {}", key2 + 26);
|
||||
verify(logger, times(1)).debug("Cleaned key2 {}", key2);
|
||||
assertEquals(KEY_2, cipher.key2);
|
||||
verify(logger, times(1)).debug("Setting key2 {}", KEY_2 + 26);
|
||||
verify(logger, times(1)).debug("Cleaned key2 {}", KEY_2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -157,14 +153,14 @@ public class AffineTest{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -173,14 +169,14 @@ public class AffineTest{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toLowerCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toLowerCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toLowerCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toLowerCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -189,14 +185,14 @@ public class AffineTest{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.preserveWhitespace = false;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing sybols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -205,14 +201,14 @@ public class AffineTest{
|
||||
cipher.preserveSymbols = false;
|
||||
cipher.preserveWhitespace = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -256,17 +252,17 @@ public class AffineTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.inputString = decodedString;
|
||||
cipher.key1 = key1;
|
||||
cipher.key2 = key2;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.key1 = KEY_1;
|
||||
cipher.key2 = KEY_2;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(17)).debug(eq("Current char {}"), anyChar());
|
||||
verify(logger, times(15)).debug(eq("Encoded char {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -274,39 +270,39 @@ public class AffineTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.inputString = encodedString;
|
||||
cipher.key1 = key1;
|
||||
cipher.key2 = key2;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
cipher.key1 = KEY_1;
|
||||
cipher.key2 = KEY_2;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(1)).debug("Key1 inverse {}", 21);
|
||||
verify(logger, times(17)).debug(eq("Current char {}"), anyChar());
|
||||
verify(logger, times(15)).debug(eq("Decoded char {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.key1 = key1;
|
||||
cipher.key2 = key2;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.key1 = KEY_1;
|
||||
cipher.key2 = KEY_2;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(key1, cipher.getKey1());
|
||||
assertEquals(key2, cipher.getKey2());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
assertEquals(KEY_1, cipher.getKey1());
|
||||
assertEquals(KEY_2, cipher.getKey2());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.key1 = key1;
|
||||
cipher.key2 = key2;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.key1 = KEY_1;
|
||||
cipher.key2 = KEY_2;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -321,51 +317,51 @@ public class AffineTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Affine(true, true, true);
|
||||
|
||||
String output = cipher.encode(key1, key2, decodedString);
|
||||
String output = cipher.encode(KEY_1, KEY_2, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(key1, cipher.key1);
|
||||
assertEquals(key2, cipher.key2);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(KEY_1, cipher.key1);
|
||||
assertEquals(KEY_2, cipher.key2);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Affine(false, false, false);
|
||||
|
||||
String output = cipher.encode(key1, key2, decodedString);
|
||||
String output = cipher.encode(KEY_1, KEY_2, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(key1, cipher.key1);
|
||||
assertEquals(key2, cipher.key2);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEY_1, cipher.key1);
|
||||
assertEquals(KEY_2, cipher.key2);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Affine(true, true, true);
|
||||
|
||||
String output = cipher.decode(key1, key2, encodedString);
|
||||
String output = cipher.decode(KEY_1, KEY_2, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(key1, cipher.key1);
|
||||
assertEquals(key2, cipher.key2);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEY_1, cipher.key1);
|
||||
assertEquals(KEY_2, cipher.key2);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testpracticalDecoding_clean(){
|
||||
cipher = new Affine(false, false, false);
|
||||
|
||||
String output = cipher.decode(key1, key2, encodedString);
|
||||
String output = cipher.decode(KEY_1, KEY_2, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(key1, cipher.key1);
|
||||
assertEquals(key2, cipher.key2);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEY_1, cipher.key1);
|
||||
assertEquals(KEY_2, cipher.key2);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/monosubstitution/AtbashTest.java
|
||||
//Mattrixwv
|
||||
// Created: 07-25-21
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -26,10 +22,10 @@ public class AtbashTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "Message to^encode";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODE";
|
||||
private static final String encodedString = "Nvhhztv gl^vmxlwv";
|
||||
private static final String encodedStringClean = "NVHHZTVGLVMXLWV";
|
||||
private static final String DECODED_STRING = "Message to^encode";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String ENCODED_STRING = "Nvhhztv gl^vmxlwv";
|
||||
private static final String ENCODED_STRING_CLEAN = "NVHHZTVGLVMXLWV";
|
||||
|
||||
|
||||
@Test
|
||||
@@ -78,17 +74,17 @@ public class AtbashTest{
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(17)).debug(eq("Encoding char {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Encoding uppercase");
|
||||
verify(logger, times(14)).debug("Encoding lowercase");
|
||||
verify(logger, times(2)).debug("Appending symbol");
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -97,14 +93,14 @@ public class AtbashTest{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -113,14 +109,14 @@ public class AtbashTest{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -129,14 +125,14 @@ public class AtbashTest{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.preserveWhitespace = false;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -145,14 +141,14 @@ public class AtbashTest{
|
||||
cipher.preserveSymbols = false;
|
||||
cipher.preserveWhitespace = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -178,7 +174,7 @@ public class AtbashTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.inputString = decodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputString("");
|
||||
@@ -194,17 +190,17 @@ public class AtbashTest{
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -217,43 +213,43 @@ public class AtbashTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Atbash(true, true, true);
|
||||
|
||||
String output = cipher.encode(decodedString);
|
||||
String output = cipher.encode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Atbash(false, false, false);
|
||||
|
||||
String output = cipher.encode(decodedString);
|
||||
String output = cipher.encode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Atbash(true, true, true);
|
||||
|
||||
String output = cipher.decode(encodedString);
|
||||
String output = cipher.decode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new Atbash(false, false, false);
|
||||
|
||||
String output = cipher.decode(encodedString);
|
||||
String output = cipher.decode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/monosubstitution/AutokeyTest.java
|
||||
//Mattrixwv
|
||||
// Created: 07-26-21
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -27,12 +23,12 @@ public class AutokeyTest{
|
||||
@Mock(name = "com.mattrixwv.cipherstream.monosubstitution.Autokey")
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "MeSsage to^encode";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODE";
|
||||
private static final String encodedString = "WiQooxh fs^wfcuhx";
|
||||
private static final String encodedStringClean = "WIQOOXHFSWFCUHX";
|
||||
private static final String keyword = "keyword";
|
||||
private static final ArrayList<Integer> offset = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3, 12, 4, 18, 18, 0, 6, 4, 19));
|
||||
private static final String DECODED_STRING = "MeSsage to^encode";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String ENCODED_STRING = "WiQooxh fs^wfcuhx";
|
||||
private static final String ENCODED_STRING_CLEAN = "WIQOOXHFSWFCUHX";
|
||||
private static final String KEYWORD = "keyword";
|
||||
private static final ArrayList<Integer> OFFSET = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3, 12, 4, 18, 18, 0, 6, 4, 19));
|
||||
|
||||
|
||||
@Test
|
||||
@@ -93,11 +89,11 @@ public class AutokeyTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.encodeSet(keyword, decodedString);
|
||||
cipher.encodeSet(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals((keyword + decodedString.replaceAll("\\s", "").replaceAll("[^a-zA-Z\\s]", "").substring(0, 8)).toUpperCase(), cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals((KEYWORD + DECODED_STRING.replaceAll("\\s", "").replaceAll("[^a-zA-Z\\s]", "").substring(0, 8)).toUpperCase(), cipher.keyword);
|
||||
assertEquals(OFFSET, cipher.offset);
|
||||
verify(logger, times(1)).debug("Setting fields for encoding");
|
||||
verify(logger, times(1)).debug("Setting keyword");
|
||||
verify(logger, times(1)).debug("Adding input to keyword");
|
||||
@@ -110,10 +106,10 @@ public class AutokeyTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.decodeSet(keyword, decodedString);
|
||||
cipher.decodeSet(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting fields for decoding");
|
||||
verify(logger, times(1)).debug("Setting keyword");
|
||||
verify(logger, times(1)).debug("Setting input string");
|
||||
@@ -124,11 +120,11 @@ public class AutokeyTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.decodeSet(keyword, encodedString);
|
||||
cipher.decodeSet(KEYWORD, ENCODED_STRING);
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(2)).debug("Appending partial output to keyword");
|
||||
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
|
||||
@@ -137,27 +133,27 @@ public class AutokeyTest{
|
||||
verify(logger, times(13)).debug("Appending lowercase");
|
||||
verify(logger, times(3)).debug("Wrapping around to z");
|
||||
verify(logger, times(17)).debug(eq("Decoded letter {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.offset.add(1);
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
assertEquals(KEYWORD, cipher.getKeyword());
|
||||
assertEquals(new ArrayList<>(List.of(1)), cipher.getOffsets());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.offset.add(1);
|
||||
|
||||
cipher.reset();
|
||||
@@ -172,47 +168,47 @@ public class AutokeyTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Autokey(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals((keyword + decodedStringClean.substring(0, 8)).toUpperCase(), cipher.keyword);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals((KEYWORD + DECODED_STRING_CLEAN.substring(0, 8)).toUpperCase(), cipher.keyword);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Autokey(false, false, false);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals((keyword + decodedStringClean.substring(0, 8)).toUpperCase(), cipher.keyword);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals((KEYWORD + DECODED_STRING_CLEAN.substring(0, 8)).toUpperCase(), cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Autokey(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals((keyword + decodedStringClean.substring(0, 14)).toUpperCase(), cipher.keyword);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals((KEYWORD + DECODED_STRING_CLEAN.substring(0, 14)).toUpperCase(), cipher.keyword);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testpracticalDecoding_clean(){
|
||||
cipher = new Autokey(false, false, false);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals((keyword + decodedStringClean.substring(0, 14)).toUpperCase(), cipher.keyword);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals((KEYWORD + DECODED_STRING_CLEAN.substring(0, 14)).toUpperCase(), cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/BaconianTest.java
|
||||
//Mattrixwv
|
||||
// Created: 01-12-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -27,10 +23,10 @@ public class BaconianTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "Message to-encode";
|
||||
private static final String decodedStringClean = "Messagetoencode";
|
||||
private static final String decodedStringCleanLower = "messagetoencode";
|
||||
private static final String encodedString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
||||
private static final String DECODED_STRING = "Message to-encode";
|
||||
private static final String DECODED_STRING_CLEAN = "Messagetoencode";
|
||||
private static final String DECODED_STRING_CLEAN_LOWER = "messagetoencode";
|
||||
private static final String ENCODED_STRING = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
|
||||
|
||||
|
||||
@Test
|
||||
@@ -55,24 +51,24 @@ public class BaconianTest{
|
||||
public void testSetInputStringEncode(){
|
||||
cipher.preserveCapitals = true;
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringClean);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputStringEncode_noCapitals(){
|
||||
cipher.preserveCapitals = false;
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringCleanLower, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING_CLEAN_LOWER, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringCleanLower);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_CLEAN_LOWER);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -107,30 +103,30 @@ public class BaconianTest{
|
||||
public void testSetInputStringDecode(){
|
||||
cipher.preserveCapitals = true;
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", ENCODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Ensuring all 'letters' contain 5 characters");
|
||||
verify(logger, times(15)).debug(eq("Current 'letter' {}"), anyString());
|
||||
verify(logger, times(15)).debug("Replacing all non-abAB characters");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputStringDecode_noCapitals(){
|
||||
cipher.preserveCapitals = false;
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString.toLowerCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString);
|
||||
assertEquals(ENCODED_STRING.toLowerCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", ENCODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Ensuring all 'letters' contain 5 characters");
|
||||
verify(logger, times(15)).debug(eq("Current 'letter' {}"), anyString());
|
||||
verify(logger, times(15)).debug("Replacing all non-abAB characters");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString.toLowerCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING.toLowerCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -204,49 +200,49 @@ public class BaconianTest{
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.inputString = decodedStringClean;
|
||||
cipher.inputString = DECODED_STRING_CLEAN;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(15)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Encoding uppercase");
|
||||
verify(logger, times(14)).debug("Encoding lowercase");
|
||||
verify(logger, times(15)).debug(eq("Output letter {}"), anyString());
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.inputString = encodedString;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(15)).debug(eq("Working letter {}"), anyString());
|
||||
verify(logger, times(15)).debug(eq("Location of letter {}"), anyInt());
|
||||
verify(logger, times(1)).debug("Decoding uppercase");
|
||||
verify(logger, times(14)).debug("Decoding lowercase");
|
||||
verify(logger, times(15)).debug(eq("Decoded character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", decodedStringClean);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", DECODED_STRING_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -259,65 +255,65 @@ public class BaconianTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher.preserveCapitals = true;
|
||||
|
||||
String output = cipher.encode(decodedString);
|
||||
String output = cipher.encode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher.preserveCapitals = false;
|
||||
|
||||
String output = cipher.encode(decodedString);
|
||||
String output = cipher.encode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringCleanLower, cipher.inputString);
|
||||
assertEquals(encodedString.toLowerCase(), cipher.outputString);
|
||||
assertEquals(encodedString.toLowerCase(), output);
|
||||
assertEquals(DECODED_STRING_CLEAN_LOWER, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING.toLowerCase(), cipher.outputString);
|
||||
assertEquals(ENCODED_STRING.toLowerCase(), output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher.preserveCapitals = true;
|
||||
|
||||
String output = cipher.decode(encodedString);
|
||||
String output = cipher.decode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher.preserveCapitals = false;
|
||||
|
||||
String output = cipher.decode(encodedString);
|
||||
String output = cipher.decode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString.toLowerCase(), cipher.inputString);
|
||||
assertEquals(decodedStringCleanLower, cipher.outputString);
|
||||
assertEquals(decodedStringCleanLower, output);
|
||||
assertEquals(ENCODED_STRING.toLowerCase(), cipher.inputString);
|
||||
assertEquals(DECODED_STRING_CLEAN_LOWER, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN_LOWER, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_upper(){
|
||||
cipher.preserveCapitals = false;
|
||||
|
||||
String output = cipher.decode(encodedString.toUpperCase());
|
||||
String output = cipher.decode(ENCODED_STRING.toUpperCase());
|
||||
|
||||
assertEquals(encodedString.toLowerCase(), cipher.inputString);
|
||||
assertEquals(decodedStringCleanLower, cipher.outputString);
|
||||
assertEquals(decodedStringCleanLower, output);
|
||||
assertEquals(ENCODED_STRING.toLowerCase(), cipher.inputString);
|
||||
assertEquals(DECODED_STRING_CLEAN_LOWER, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN_LOWER, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_lower(){
|
||||
cipher.preserveCapitals = false;
|
||||
|
||||
String output = cipher.decode(encodedString.toLowerCase());
|
||||
String output = cipher.decode(ENCODED_STRING.toLowerCase());
|
||||
|
||||
assertEquals(encodedString.toLowerCase(), cipher.inputString);
|
||||
assertEquals(decodedStringCleanLower, cipher.outputString);
|
||||
assertEquals(decodedStringCleanLower, output);
|
||||
assertEquals(ENCODED_STRING.toLowerCase(), cipher.inputString);
|
||||
assertEquals(DECODED_STRING_CLEAN_LOWER, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN_LOWER, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/monosubstitution/BaseXTest.java
|
||||
//Mattrixwv
|
||||
// Created: 01-08-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -28,11 +24,11 @@ public class BaseXTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "A+B@C d\te\nf";
|
||||
private static final String encodedString_2 = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
|
||||
private static final String encodedString_8 = "101 53 102 100 103 40 144 11 145 12 146";
|
||||
private static final String encodedString_10 = "65 43 66 64 67 32 100 9 101 10 102";
|
||||
private static final String encodedString_16 = "41 2B 42 40 43 20 64 9 65 A 66";
|
||||
private static final String DECODED_STRING = "A+B@C d\te\nf";
|
||||
private static final String ENCODED_STRING_2 = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
|
||||
private static final String ENCODED_STRING_8 = "101 53 102 100 103 40 144 11 145 12 146";
|
||||
private static final String ENCODED_STRING_10 = "65 43 66 64 67 32 100 9 101 10 102";
|
||||
private static final String ENCODED_STRING_16 = "41 2B 42 40 43 20 64 9 65 A 66";
|
||||
|
||||
|
||||
@Test
|
||||
@@ -55,10 +51,10 @@ public class BaseXTest{
|
||||
|
||||
@Test
|
||||
public void testSetInputStringEncode(){
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -78,7 +74,7 @@ public class BaseXTest{
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
verify(logger, never()).debug("Setting input string for encoding '{}'", decodedString);
|
||||
verify(logger, never()).debug("Setting input string for encoding '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug(anyString(), anyString());
|
||||
verify(logger, never()).debug(anyString());
|
||||
}
|
||||
@@ -87,14 +83,14 @@ public class BaseXTest{
|
||||
public void testSetInputStringDecode(){
|
||||
cipher.base = 16;
|
||||
|
||||
cipher.setInputStringDecode(encodedString_16);
|
||||
cipher.setInputStringDecode(ENCODED_STRING_16);
|
||||
|
||||
assertEquals(encodedString_16, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString_16);
|
||||
assertEquals(ENCODED_STRING_16, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", ENCODED_STRING_16);
|
||||
verify(logger, times(1)).debug("Creating string of valid 'numbers'");
|
||||
verify(logger, times(16)).debug(eq("Current number {}, converted {}"), anyInt(), anyString());
|
||||
verify(logger, times(1)).debug("Checking for invalid characters");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString_16);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING_16);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -176,29 +172,29 @@ public class BaseXTest{
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.base = 16;
|
||||
cipher.inputString = decodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString_16, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_16, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(11)).debug(eq("Working number {}"), anyChar());
|
||||
verify(logger, times(11)).debug(eq("Converted number {}"), anyString());
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", encodedString_16);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", ENCODED_STRING_16);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.base = 16;
|
||||
cipher.inputString = encodedString_16;
|
||||
cipher.inputString = ENCODED_STRING_16;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(11)).debug(eq("Current number {}"), anyString());
|
||||
verify(logger, times(11)).debug(eq("Decoded number {}"), anyInt());
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -218,19 +214,19 @@ public class BaseXTest{
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString_2;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING_2;
|
||||
cipher.base = 8;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString_2, cipher.getOutputString());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING_2, cipher.getOutputString());
|
||||
assertEquals(8, cipher.getBase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString_2;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING_2;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -241,101 +237,101 @@ public class BaseXTest{
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_2(){
|
||||
String output = cipher.encode(2, decodedString);
|
||||
String output = cipher.encode(2, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(2, cipher.base);
|
||||
assertEquals(encodedString_2, cipher.outputString);
|
||||
assertEquals(encodedString_2, output);
|
||||
assertEquals(ENCODED_STRING_2, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_2, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_8(){
|
||||
String output = cipher.encode(8, decodedString);
|
||||
String output = cipher.encode(8, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(8, cipher.base);
|
||||
assertEquals(encodedString_8, cipher.outputString);
|
||||
assertEquals(encodedString_8, output);
|
||||
assertEquals(ENCODED_STRING_8, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_8, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_10(){
|
||||
String output = cipher.encode(10, decodedString);
|
||||
String output = cipher.encode(10, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(10, cipher.base);
|
||||
assertEquals(encodedString_10, cipher.outputString);
|
||||
assertEquals(encodedString_10, output);
|
||||
assertEquals(ENCODED_STRING_10, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_10, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_16(){
|
||||
String output = cipher.encode(16, decodedString);
|
||||
String output = cipher.encode(16, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(16, cipher.base);
|
||||
assertEquals(encodedString_16, cipher.outputString);
|
||||
assertEquals(encodedString_16, output);
|
||||
assertEquals(ENCODED_STRING_16, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_16, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_inputOnly(){
|
||||
String output = cipher.encode(decodedString);
|
||||
String output = cipher.encode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(2, cipher.base);
|
||||
assertEquals(encodedString_2, cipher.outputString);
|
||||
assertEquals(encodedString_2, output);
|
||||
assertEquals(ENCODED_STRING_2, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_2, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_2(){
|
||||
String output = cipher.decode(2, encodedString_2);
|
||||
String output = cipher.decode(2, ENCODED_STRING_2);
|
||||
|
||||
assertEquals(encodedString_2, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING_2, cipher.inputString);
|
||||
assertEquals(2, cipher.base);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_8(){
|
||||
String output = cipher.decode(8, encodedString_8);
|
||||
String output = cipher.decode(8, ENCODED_STRING_8);
|
||||
|
||||
assertEquals(encodedString_8, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING_8, cipher.inputString);
|
||||
assertEquals(8, cipher.base);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_10(){
|
||||
String output = cipher.decode(10, encodedString_10);
|
||||
String output = cipher.decode(10, ENCODED_STRING_10);
|
||||
|
||||
assertEquals(encodedString_10, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING_10, cipher.inputString);
|
||||
assertEquals(10, cipher.base);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_16(){
|
||||
String output = cipher.decode(16, encodedString_16);
|
||||
String output = cipher.decode(16, ENCODED_STRING_16);
|
||||
|
||||
assertEquals(encodedString_16, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING_16, cipher.inputString);
|
||||
assertEquals(16, cipher.base);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_inputOnly(){
|
||||
String output = cipher.decode(encodedString_2);
|
||||
String output = cipher.decode(ENCODED_STRING_2);
|
||||
|
||||
assertEquals(encodedString_2, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING_2, cipher.inputString);
|
||||
assertEquals(2, cipher.base);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/monosubstitution/BeaufortTest.java
|
||||
//Mattrixwv
|
||||
// Created: 02-23-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -27,12 +23,12 @@ public class BeaufortTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "Message to^encode";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODE";
|
||||
private static final String encodedString = "Yageolz rq^ujmdag";
|
||||
private static final String encodedStringClean = "YAGEOLZRQUJMDAG";
|
||||
private static final String keyword = "Ke*y word";
|
||||
private static final String keywordClean = "KEYWORD";
|
||||
private static final String DECODED_STRING = "Message to^encode";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String ENCODED_STRING = "Yageolz rq^ujmdag";
|
||||
private static final String ENCODED_STRING_CLEAN = "YAGEOLZRQUJMDAG";
|
||||
private static final String KEYWORD = "Ke*y word";
|
||||
private static final String KEYWORD_CLEAN = "KEYWORD";
|
||||
|
||||
|
||||
@Test
|
||||
@@ -125,14 +121,14 @@ public class BeaufortTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -141,14 +137,14 @@ public class BeaufortTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -157,14 +153,14 @@ public class BeaufortTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -173,14 +169,14 @@ public class BeaufortTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -217,13 +213,13 @@ public class BeaufortTest{
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", keyword);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", KEYWORD);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing all non-letters");
|
||||
verify(logger, times(1)).debug("Cleaned keyword '{}'", keywordClean);
|
||||
verify(logger, times(1)).debug("Cleaned keyword '{}'", KEYWORD_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -268,51 +264,51 @@ public class BeaufortTest{
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher = new Beaufort(true, true, true);
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = decodedString;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(1)).debug("Encoding with Atbash");
|
||||
verify(logger, times(1)).debug("Shifting all letters by 1");
|
||||
verify(logger, times(1)).debug("Encoding with Vigenere");
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher = new Beaufort(true, true, true);
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = decodedString;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(1)).debug("Encoding with Atbash");
|
||||
verify(logger, times(1)).debug("Shifting all letters by 1");
|
||||
verify(logger, times(1)).debug("Encoding with Vigenere");
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
assertEquals(KEYWORD, cipher.getKeyword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -325,43 +321,43 @@ public class BeaufortTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Beaufort(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Beaufort(false, false, false);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Beaufort(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new Beaufort(false, false, false);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/CaesarTest.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -26,11 +22,11 @@ public class CaesarTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "The quick brown fox jumps over - the lAzy dog";
|
||||
private static final String decodedStringClean = "thequickbrownfoxjumpsoverthelazydog";
|
||||
private static final String encodedString = "Qeb nrfzh yoltk clu grjmp lsbo - qeb iXwv ald";
|
||||
private static final String encodedStringClean = "qebnrfzhyoltkclugrjmplsboqebixwvald";
|
||||
private static final int shift = 23;
|
||||
private static final String DECODED_STRING = "The quick brown fox jumps over - the lAzy dog";
|
||||
private static final String DECODED_STRING_CLEAN = "thequickbrownfoxjumpsoverthelazydog";
|
||||
private static final String ENCODED_STRING = "Qeb nrfzh yoltk clu grjmp lsbo - qeb iXwv ald";
|
||||
private static final String ENCODED_STRING_CLEAN = "qebnrfzhyoltkclugrjmplsboqebixwvald";
|
||||
private static final int SHIFT = 23;
|
||||
|
||||
|
||||
@Test
|
||||
@@ -83,29 +79,29 @@ public class CaesarTest{
|
||||
|
||||
@Test
|
||||
public void testSetShift(){
|
||||
cipher.setShift(shift);
|
||||
cipher.setShift(SHIFT);
|
||||
|
||||
assertEquals(shift, cipher.shift);
|
||||
verify(logger, times(1)).debug("Setting shift {}", shift);
|
||||
verify(logger, times(1)).debug("Cleaned shift {}", shift);
|
||||
assertEquals(SHIFT, cipher.shift);
|
||||
verify(logger, times(1)).debug("Setting shift {}", SHIFT);
|
||||
verify(logger, times(1)).debug("Cleaned shift {}", SHIFT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetShift_large(){
|
||||
cipher.setShift(shift + 26);
|
||||
cipher.setShift(SHIFT + 26);
|
||||
|
||||
assertEquals(shift, cipher.shift);
|
||||
verify(logger, times(1)).debug("Setting shift {}", shift + 26);
|
||||
verify(logger, times(1)).debug("Cleaned shift {}", shift);
|
||||
assertEquals(SHIFT, cipher.shift);
|
||||
verify(logger, times(1)).debug("Setting shift {}", SHIFT + 26);
|
||||
verify(logger, times(1)).debug("Cleaned shift {}", SHIFT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetShift_negative(){
|
||||
cipher.setShift(shift - 26);
|
||||
cipher.setShift(SHIFT - 26);
|
||||
|
||||
assertEquals(shift - 26, cipher.shift);
|
||||
verify(logger, times(1)).debug("Setting shift {}", shift - 26);
|
||||
verify(logger, times(1)).debug("Cleaned shift {}", shift - 26);
|
||||
assertEquals(SHIFT, cipher.shift);
|
||||
verify(logger, times(1)).debug("Setting shift {}", SHIFT - 26);
|
||||
verify(logger, times(1)).debug("Cleaned shift {}", SHIFT);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -114,14 +110,14 @@ public class CaesarTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -130,14 +126,14 @@ public class CaesarTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toLowerCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toLowerCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toLowerCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toLowerCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -146,14 +142,14 @@ public class CaesarTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -162,14 +158,14 @@ public class CaesarTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -210,12 +206,12 @@ public class CaesarTest{
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.shift = shift;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.shift = SHIFT;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(2)).debug("Encoding uppercase");
|
||||
@@ -225,17 +221,17 @@ public class CaesarTest{
|
||||
verify(logger, never()).debug("Wrapping around to z");
|
||||
verify(logger, times(31)).debug("Wrapping around to a");
|
||||
verify(logger, times(45)).debug(eq("Encoded character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Saving encoded string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Saving encoded string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncode_negative(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.shift = shift - 26;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.shift = SHIFT - 26;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(2)).debug("Encoding uppercase");
|
||||
@@ -245,17 +241,17 @@ public class CaesarTest{
|
||||
verify(logger, times(2)).debug("Wrapping around to z");
|
||||
verify(logger, never()).debug("Wrapping around to a");
|
||||
verify(logger, times(45)).debug(eq("Encoded character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Saving encoded string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Saving encoded string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.inputString = encodedString;
|
||||
cipher.shift = shift;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
cipher.shift = SHIFT;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(2)).debug("Decoding uppercase");
|
||||
@@ -265,17 +261,17 @@ public class CaesarTest{
|
||||
verify(logger, times(31)).debug("Wrapping around to z");
|
||||
verify(logger, never()).debug("Wrapping around to a");
|
||||
verify(logger, times(45)).debug(eq("Decoded character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Saving decoded string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Saving decoded string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecode_negative(){
|
||||
cipher.inputString = encodedString;
|
||||
cipher.shift = shift - 26;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
cipher.shift = SHIFT - 26;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(45)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(2)).debug("Decoding uppercase");
|
||||
@@ -285,25 +281,25 @@ public class CaesarTest{
|
||||
verify(logger, never()).debug("Wrapping around to z");
|
||||
verify(logger, times(2)).debug("Wrapping around to a");
|
||||
verify(logger, times(45)).debug(eq("Decoded character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Saving decoded string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Saving decoded string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.shift = shift;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.shift = SHIFT;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(shift, cipher.getShift());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
assertEquals(SHIFT, cipher.getShift());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.shift = shift;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.shift = SHIFT;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -317,70 +313,70 @@ public class CaesarTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Caesar(true, true, true);
|
||||
|
||||
String output = cipher.encode(shift, decodedString);
|
||||
String output = cipher.encode(SHIFT, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(shift, cipher.shift);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(SHIFT, cipher.shift);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Caesar(false, false, false);
|
||||
|
||||
String output = cipher.encode(shift, decodedString);
|
||||
String output = cipher.encode(SHIFT, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(shift, cipher.shift);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(SHIFT, cipher.shift);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_negative(){
|
||||
cipher = new Caesar(true, true, true);
|
||||
|
||||
String output = cipher.encode(shift, decodedString);
|
||||
String output = cipher.encode(-3, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(shift, cipher.shift);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(SHIFT, cipher.shift);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Caesar(true, true, true);
|
||||
|
||||
String output = cipher.decode(shift, encodedString);
|
||||
String output = cipher.decode(SHIFT, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(shift, cipher.shift);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(SHIFT, cipher.shift);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new Caesar(false, false, false);
|
||||
|
||||
String output = cipher.decode(shift, encodedString);
|
||||
String output = cipher.decode(SHIFT, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(shift, cipher.shift);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(SHIFT, cipher.shift);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_negative(){
|
||||
cipher = new Caesar(true, true, true);
|
||||
String output = cipher.decode(shift - 26, encodedString);
|
||||
String output = cipher.decode(SHIFT - 26, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(shift - 26, cipher.shift);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(SHIFT, cipher.shift);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/monosubstitution/OneTimePadTest.java
|
||||
//Mattrixwv
|
||||
// Created: 02-23-22
|
||||
//Modified: 05-04-23
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -29,12 +25,12 @@ public class OneTimePadTest{
|
||||
@Mock(name = "com.mattrixwv.cipherstream.monosubstitution.OneTimePad")
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "Message to^encode";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODE";
|
||||
private static final String encodedString = "Wiqooxh mv^egkgws";
|
||||
private static final String encodedStringClean = "WIQOOXHMVEGKGWS";
|
||||
private static final String keyword = "keywordThatIsTotallyRandom";
|
||||
private static final ArrayList<Integer> offset = new ArrayList<>(Arrays.asList(10, 4, 24, 22, 14, 17, 3, 19, 7, 0, 19, 8, 18, 19, 14, 19, 0, 11, 11, 24, 17, 0, 13, 3, 14, 12));
|
||||
private static final String DECODED_STRING = "Message to^encode";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String ENCODED_STRING = "Wiqooxh mv^egkgws";
|
||||
private static final String ENCODED_STRING_CLEAN = "WIQOOXHMVEGKGWS";
|
||||
private static final String KEYWORD = "keywordThatIsTotallyRandom";
|
||||
private static final ArrayList<Integer> OFFSET = new ArrayList<>(Arrays.asList(10, 4, 24, 22, 14, 17, 3, 19, 7, 0, 19, 8, 18, 19, 14, 19, 0, 11, 11, 24, 17, 0, 13, 3, 14, 12));
|
||||
|
||||
|
||||
@Test
|
||||
@@ -95,13 +91,13 @@ public class OneTimePadTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
assertEquals(OFFSET, cipher.offset);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
}
|
||||
|
||||
@@ -111,13 +107,13 @@ public class OneTimePadTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
assertEquals(OFFSET, cipher.offset);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
}
|
||||
|
||||
@@ -128,7 +124,7 @@ public class OneTimePadTest{
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidKeywordException.class, () -> {
|
||||
cipher.encode("keyword", decodedString);
|
||||
cipher.encode("keyword", DECODED_STRING);
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
@@ -144,13 +140,13 @@ public class OneTimePadTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
assertEquals(OFFSET, cipher.offset);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
}
|
||||
|
||||
@@ -160,20 +156,20 @@ public class OneTimePadTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
assertEquals(OFFSET, cipher.offset);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecode_short(){
|
||||
assertThrows(InvalidKeywordException.class, () -> {
|
||||
cipher.decode("keyword", encodedString);
|
||||
cipher.decode("keyword", ENCODED_STRING);
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
@@ -187,47 +183,47 @@ public class OneTimePadTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new OneTimePad(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new OneTimePad(false, false, false);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new OneTimePad(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new OneTimePad(false, false, false);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/monosubstitution/PortaTest.java
|
||||
//Mattrixwv
|
||||
// Created: 02-28-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -27,12 +23,12 @@ public class PortaTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "Message to^encode";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODE";
|
||||
private static final String encodedString = "Rtghuos bm^qcwgrw";
|
||||
private static final String encodedStringClean = "RTGHUOSBMQCWGRW";
|
||||
private static final String keyword = "keyword";
|
||||
private static final String keywordDirty = "Ke yw*ord";
|
||||
private static final String DECODED_STRING = "Message to^encode";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String ENCODED_STRING = "Rtghuos bm^qcwgrw";
|
||||
private static final String ENCODED_STRING_CLEAN = "RTGHUOSBMQCWGRW";
|
||||
private static final String KEYWORD = "keyword";
|
||||
private static final String KEYWORD_DIRTY = "Ke yw*ord";
|
||||
|
||||
|
||||
@Test
|
||||
@@ -85,24 +81,24 @@ public class PortaTest{
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", keyword);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", KEYWORD);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing all non-letters");
|
||||
verify(logger, times(1)).debug("Cleaned keyword '{}'", keyword.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned keyword '{}'", KEYWORD.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKeyword_dirty(){
|
||||
cipher.setKeyword(keywordDirty);
|
||||
cipher.setKeyword(KEYWORD_DIRTY);
|
||||
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", keywordDirty);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", KEYWORD_DIRTY);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing all non-letters");
|
||||
verify(logger, times(1)).debug("Cleaned keyword '{}'", keyword.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned keyword '{}'", KEYWORD.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -148,11 +144,11 @@ public class PortaTest{
|
||||
public void testSetKeyword_symbols(){
|
||||
cipher.setKeyword("key*word^");
|
||||
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", "key*word^");
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing all non-letters");
|
||||
verify(logger, times(1)).debug("Cleaned keyword '{}'", keyword.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned keyword '{}'", KEYWORD.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -161,14 +157,14 @@ public class PortaTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string {}", decodedString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string {}", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -177,14 +173,14 @@ public class PortaTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string {}", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string {}", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -193,14 +189,14 @@ public class PortaTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string {}", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string {}", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -209,14 +205,14 @@ public class PortaTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string {}", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string {}", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -639,18 +635,18 @@ public class PortaTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keyword.toUpperCase();
|
||||
cipher.inputString = decodedString;
|
||||
cipher.keyword = KEYWORD.toUpperCase();
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Encoding uppercase");
|
||||
verify(logger, times(14)).debug("Encoding lowercase");
|
||||
verify(logger, times(17)).debug(eq("Encoded letter {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -658,36 +654,36 @@ public class PortaTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keyword.toUpperCase();
|
||||
cipher.inputString = encodedString;
|
||||
cipher.keyword = KEYWORD.toUpperCase();
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Encoding uppercase");
|
||||
verify(logger, times(14)).debug("Encoding lowercase");
|
||||
verify(logger, times(17)).debug(eq("Encoded letter {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Saving output string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
assertEquals(KEYWORD, cipher.getKeyword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRest(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -700,47 +696,47 @@ public class PortaTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Porta(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Porta(false, false, false);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Porta(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new Porta(false, false, false);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(keyword.toUpperCase(), cipher.keyword);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD.toUpperCase(), cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/monosubstitution/SubstitutionTest.java
|
||||
//Mattrixwv
|
||||
// Created: 02-22-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -27,18 +23,18 @@ public class SubstitutionTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "Message to^encode";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODE";
|
||||
private static final String decodedStringAlNum = "Message to^encode 123";
|
||||
private static final String decodedStringAlNumClean = "MESSAGETOENCODE";
|
||||
private static final String encodedString = "Oguucig vq^gpeqfg";
|
||||
private static final String encodedStringClean = "OGUUCIGVQGPEQFG";
|
||||
private static final String encodedStringAlNum = "Oguucig vq^gpeqfg 876";
|
||||
private static final String encodedStringAlNumClean = "OGUUCIGVQGPEQFG";
|
||||
private static final String keyword = "cdefghijklmnopqrstuvwxyzab";
|
||||
private static final String keywordClean = "CDEFGHIJKLMNOPQRSTUVWXYZAB";
|
||||
private static final String keywordAlNum = "cdefghijklmnopqrstuvwxyzab9876543210";
|
||||
private static final String keywordAlNumClean = "CDEFGHIJKLMNOPQRSTUVWXYZAB9876543210";
|
||||
private static final String DECODED_STRING = "Message to^encode";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String DECODED_STRING_AL_NUM = "Message to^encode 123";
|
||||
private static final String DECODED_STRING_AL_NUM_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String ENCODED_STRING = "Oguucig vq^gpeqfg";
|
||||
private static final String ENCODED_STRING_CLEAN = "OGUUCIGVQGPEQFG";
|
||||
private static final String ENCODED_STRING_AL_NUM = "Oguucig vq^gpeqfg 876";
|
||||
private static final String ENCODED_STRING_AL_NUM_CLEAN = "OGUUCIGVQGPEQFG";
|
||||
private static final String KEYWORD = "cdefghijklmnopqrstuvwxyzab";
|
||||
private static final String KEYWORD_CLEAN = "CDEFGHIJKLMNOPQRSTUVWXYZAB";
|
||||
private static final String KEYWORD_AL_NUM = "cdefghijklmnopqrstuvwxyzab9876543210";
|
||||
private static final String KEYWORD_AL_NUM_CLEAN = "CDEFGHIJKLMNOPQRSTUVWXYZAB9876543210";
|
||||
|
||||
|
||||
@Test
|
||||
@@ -91,28 +87,28 @@ public class SubstitutionTest{
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original key '{}'", keyword);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original key '{}'", KEYWORD);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
|
||||
verify(logger, times(1)).debug("Ensuring there are only letters in the key");
|
||||
verify(logger, never()).debug("Ensuring there are only alpha-numeric characters in the key");
|
||||
verify(logger, times(1)).debug("Cleaned key '{}'", keywordClean);
|
||||
verify(logger, times(1)).debug("Cleaned key '{}'", KEYWORD_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKeyword_alNum(){
|
||||
cipher.setKeyword(keywordAlNum);
|
||||
cipher.setKeyword(KEYWORD_AL_NUM);
|
||||
|
||||
assertEquals(keywordAlNumClean, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original key '{}'", keywordAlNum);
|
||||
assertEquals(KEYWORD_AL_NUM_CLEAN, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original key '{}'", KEYWORD_AL_NUM);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Ensuring there are no duplicate mappings");
|
||||
verify(logger, never()).debug("Ensuring there are only letters in the key");
|
||||
verify(logger, times(1)).debug("Ensuring there are only alpha-numeric characters in the key");
|
||||
verify(logger, times(1)).debug("Cleaned key '{}'", keywordAlNumClean);
|
||||
verify(logger, times(1)).debug("Cleaned key '{}'", KEYWORD_AL_NUM_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -196,14 +192,14 @@ public class SubstitutionTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -212,14 +208,14 @@ public class SubstitutionTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -228,14 +224,14 @@ public class SubstitutionTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -244,14 +240,14 @@ public class SubstitutionTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -295,19 +291,19 @@ public class SubstitutionTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.inputString = decodedStringAlNum;
|
||||
cipher.keyword = keywordAlNumClean;
|
||||
cipher.inputString = DECODED_STRING_AL_NUM;
|
||||
cipher.keyword = KEYWORD_AL_NUM_CLEAN;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedStringAlNum, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_AL_NUM, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(21)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Encoding uppercase");
|
||||
verify(logger, times(14)).debug("Encoding lowercase");
|
||||
verify(logger, times(3)).debug("Encoding digit");
|
||||
verify(logger, times(3)).debug("Passing symbol through");
|
||||
verify(logger, times(1)).debug("Encoded message '{}'", encodedStringAlNum);
|
||||
verify(logger, times(1)).debug("Encoded message '{}'", ENCODED_STRING_AL_NUM);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -315,37 +311,37 @@ public class SubstitutionTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.inputString = encodedStringAlNum;
|
||||
cipher.keyword = keywordAlNumClean;
|
||||
cipher.inputString = ENCODED_STRING_AL_NUM;
|
||||
cipher.keyword = KEYWORD_AL_NUM_CLEAN;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedStringAlNum, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_AL_NUM, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(21)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Encoding uppercase");
|
||||
verify(logger, times(14)).debug("Encoding lowercase");
|
||||
verify(logger, times(3)).debug("Encoding digit");
|
||||
verify(logger, times(3)).debug("Passing symbol through");
|
||||
verify(logger, times(1)).debug("Decoded message '{}'", decodedStringAlNum);
|
||||
verify(logger, times(1)).debug("Decoded message '{}'", DECODED_STRING_AL_NUM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
assertEquals(KEYWORD, cipher.getKeyword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -359,119 +355,119 @@ public class SubstitutionTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Substitution(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Substitution(false, false, false);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_alNum(){
|
||||
cipher = new Substitution(true, true, true);
|
||||
|
||||
String output = cipher.encode(keywordAlNum, decodedStringAlNum);
|
||||
String output = cipher.encode(KEYWORD_AL_NUM, DECODED_STRING_AL_NUM);
|
||||
|
||||
assertEquals(decodedStringAlNum, cipher.inputString);
|
||||
assertEquals(keywordAlNumClean, cipher.keyword);
|
||||
assertEquals(encodedStringAlNum, cipher.outputString);
|
||||
assertEquals(encodedStringAlNum, output);
|
||||
assertEquals(DECODED_STRING_AL_NUM, cipher.inputString);
|
||||
assertEquals(KEYWORD_AL_NUM_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_AL_NUM, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_AL_NUM, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_alNumClean(){
|
||||
cipher = new Substitution(false, false, false);
|
||||
|
||||
String output = cipher.encode(keywordAlNum, decodedStringAlNum);
|
||||
String output = cipher.encode(KEYWORD_AL_NUM, DECODED_STRING_AL_NUM);
|
||||
|
||||
assertEquals(decodedStringAlNumClean, cipher.inputString);
|
||||
assertEquals(keywordAlNumClean, cipher.keyword);
|
||||
assertEquals(encodedStringAlNumClean, cipher.outputString);
|
||||
assertEquals(encodedStringAlNumClean, output);
|
||||
assertEquals(DECODED_STRING_AL_NUM_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_AL_NUM_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_AL_NUM_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_AL_NUM_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrecticalEncoding_noAlNumKey(){
|
||||
cipher = new Substitution(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, decodedStringAlNum);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING_AL_NUM);
|
||||
|
||||
assertEquals(decodedStringAlNum, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedString + " 123", cipher.outputString);
|
||||
assertEquals(encodedString + " 123", output);
|
||||
assertEquals(DECODED_STRING_AL_NUM, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING + " 123", cipher.outputString);
|
||||
assertEquals(ENCODED_STRING + " 123", output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Substitution(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new Substitution(false, false, false);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_alNum(){
|
||||
cipher = new Substitution(true, true, true);
|
||||
|
||||
String output = cipher.decode(keywordAlNum, encodedStringAlNum);
|
||||
String output = cipher.decode(KEYWORD_AL_NUM, ENCODED_STRING_AL_NUM);
|
||||
|
||||
assertEquals(encodedStringAlNum, cipher.inputString);
|
||||
assertEquals(keywordAlNumClean, cipher.keyword);
|
||||
assertEquals(decodedStringAlNum, cipher.outputString);
|
||||
assertEquals(decodedStringAlNum, output);
|
||||
assertEquals(ENCODED_STRING_AL_NUM, cipher.inputString);
|
||||
assertEquals(KEYWORD_AL_NUM_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_AL_NUM, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_AL_NUM, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_alNumClean(){
|
||||
cipher = new Substitution(false, false, false);
|
||||
|
||||
String output = cipher.decode(keywordAlNum, encodedStringAlNum);
|
||||
String output = cipher.decode(KEYWORD_AL_NUM, ENCODED_STRING_AL_NUM);
|
||||
|
||||
assertEquals(encodedStringAlNumClean, cipher.inputString);
|
||||
assertEquals(keywordAlNumClean, cipher.keyword);
|
||||
assertEquals(decodedStringAlNumClean, cipher.outputString);
|
||||
assertEquals(decodedStringAlNumClean, output);
|
||||
assertEquals(ENCODED_STRING_AL_NUM_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_AL_NUM_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_AL_NUM_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_AL_NUM_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_noAlNumKey(){
|
||||
cipher = new Substitution(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString + " 123");
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING + " 123");
|
||||
|
||||
assertEquals(encodedString + " 123", cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedString + " 123", cipher.outputString);
|
||||
assertEquals(decodedString + " 123", output);
|
||||
assertEquals(ENCODED_STRING + " 123", cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING + " 123", cipher.outputString);
|
||||
assertEquals(DECODED_STRING + " 123", output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/monosubstitution/VigenereTest.java
|
||||
//Mattrixwv
|
||||
// Created: 07-25-21
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -12,6 +8,7 @@ import static org.mockito.Mockito.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
@@ -30,13 +27,19 @@ public class VigenereTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String inputString = "MeSsage to^encode";
|
||||
private static final String inputStringClean = "MESSAGETOENCODE";
|
||||
private static final String outputString = "WiQooxh ds^cjqfgo";
|
||||
private static final String outputStringClean = "WIQOOXHDSCJQFGO";
|
||||
private static final String keyword = "ke yw*ord";
|
||||
private static final String keywordClean = "KEYWORD";
|
||||
private ArrayList<Integer> offset = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3));
|
||||
private static final String INPUT_STRING = "MeSsage to^encode";
|
||||
private static final String INPUT_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String OUTPUT_STRING = "WiQooxh ds^cjqfgo";
|
||||
private static final String OUTPUT_STRING_CLEAN = "WIQOOXHDSCJQFGO";
|
||||
private static final String KEYWORD = "ke yw*ord";
|
||||
private static final String KEYWORD_CLEAN = "KEYWORD";
|
||||
private ArrayList<Integer> offset;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup(){
|
||||
offset = new ArrayList<>(List.of(10, 4, 24, 22, 14, 17, 3));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@@ -93,11 +96,11 @@ public class VigenereTest{
|
||||
|
||||
@Test
|
||||
public void testSetOffset(){
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
|
||||
cipher.setOffset();
|
||||
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
verify(logger, times(1)).debug("Setting offset array from keyword");
|
||||
verify(logger, times(1)).debug("Offset {}", offset);
|
||||
@@ -108,17 +111,17 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
cipher.setInputString(INPUT_STRING);
|
||||
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(INPUT_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -126,17 +129,17 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = false;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
cipher.setInputString(INPUT_STRING);
|
||||
|
||||
assertEquals(inputString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(INPUT_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -144,17 +147,17 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
cipher.setInputString(INPUT_STRING);
|
||||
|
||||
assertEquals(inputString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(INPUT_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -162,17 +165,17 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
cipher.setInputString(INPUT_STRING);
|
||||
|
||||
assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(INPUT_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -180,7 +183,7 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -200,7 +203,7 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -217,14 +220,14 @@ public class VigenereTest{
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", keyword);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", KEYWORD);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing all non-letter characters");
|
||||
verify(logger, times(1)).debug("Clean keyword '{}'", keywordClean);
|
||||
verify(logger, times(1)).debug("Clean keyword '{}'", KEYWORD_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -260,13 +263,13 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.inputString = inputString;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = INPUT_STRING;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(OUTPUT_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(2)).debug("Encoding uppercase");
|
||||
@@ -274,7 +277,7 @@ public class VigenereTest{
|
||||
verify(logger, times(13)).debug("Encoding lowercase");
|
||||
verify(logger, times(5)).debug("Wrapping around to a");
|
||||
verify(logger, times(17)).debug(eq("Encoded character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Encoded message '{}'", outputString);
|
||||
verify(logger, times(1)).debug("Encoded message '{}'", OUTPUT_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -282,13 +285,13 @@ public class VigenereTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.inputString = outputString;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = OUTPUT_STRING;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(inputString, cipher.outputString);
|
||||
assertEquals(INPUT_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(2)).debug("Decoding uppercase");
|
||||
@@ -296,27 +299,27 @@ public class VigenereTest{
|
||||
verify(logger, times(13)).debug("Decoding lowercase");
|
||||
verify(logger, times(5)).debug("Wrapping around to z");
|
||||
verify(logger, times(17)).debug(eq("Decoded character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Decoded message '{}'", inputString);
|
||||
verify(logger, times(1)).debug("Decoded message '{}'", INPUT_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.outputString = outputString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = INPUT_STRING;
|
||||
cipher.outputString = OUTPUT_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.offset = offset;
|
||||
|
||||
assertEquals(inputString, cipher.getInputString());
|
||||
assertEquals(outputString, cipher.getOutputString());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(INPUT_STRING, cipher.getInputString());
|
||||
assertEquals(OUTPUT_STRING, cipher.getOutputString());
|
||||
assertEquals(KEYWORD, cipher.getKeyword());
|
||||
assertEquals(offset, cipher.getOffsets());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.outputString = outputString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = INPUT_STRING;
|
||||
cipher.outputString = OUTPUT_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.offset = offset;
|
||||
|
||||
cipher.reset();
|
||||
@@ -332,51 +335,51 @@ public class VigenereTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Vigenere(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
String output = cipher.encode(KEYWORD, INPUT_STRING);
|
||||
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(INPUT_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(outputString, output);
|
||||
assertEquals(OUTPUT_STRING, cipher.outputString);
|
||||
assertEquals(OUTPUT_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Vigenere(false, false, false);
|
||||
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
String output = cipher.encode(KEYWORD, INPUT_STRING);
|
||||
|
||||
assertEquals(inputStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(INPUT_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(outputStringClean, cipher.outputString);
|
||||
assertEquals(outputStringClean, output);
|
||||
assertEquals(OUTPUT_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(OUTPUT_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Vigenere(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, outputString);
|
||||
String output = cipher.decode(KEYWORD, OUTPUT_STRING);
|
||||
|
||||
assertEquals(outputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(OUTPUT_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(inputString, cipher.outputString);
|
||||
assertEquals(inputString, output);
|
||||
assertEquals(INPUT_STRING, cipher.outputString);
|
||||
assertEquals(INPUT_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new Vigenere(false, false, false);
|
||||
|
||||
String output = cipher.decode(keyword, outputString);
|
||||
String output = cipher.decode(KEYWORD, OUTPUT_STRING);
|
||||
|
||||
assertEquals(outputStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(OUTPUT_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(offset, cipher.offset);
|
||||
assertEquals(inputStringClean, cipher.outputString);
|
||||
assertEquals(inputStringClean, output);
|
||||
assertEquals(INPUT_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(INPUT_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/polysubstitution/BifidTest.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -27,12 +23,12 @@ public class BifidTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Test
|
||||
private static final String inputString = "Message to^encode";
|
||||
private static final String inputStringClean = "MESSAGETOENCODE";
|
||||
private static final String outputString = "Mqaokne kc^vdodzd";
|
||||
private static final String outputStringClean = "MQAOKNEKCVDODZD";
|
||||
private static final String keyword = "keyword";
|
||||
private static final String keywordClean = "KEYWORDABCFGHILMNPQSTUVXZ";
|
||||
private static final String INPUT_STRING = "Message to^encode";
|
||||
private static final String INPUT_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String OUTPUT_STRING = "Mqaokne kc^vdodzd";
|
||||
private static final String OUTPUT_STRING_CLEAN = "MQAOKNEKCVDODZD";
|
||||
private static final String KEYWORD = "keyword";
|
||||
private static final String KEYWORD_CLEAN = "KEYWORDABCFGHILMNPQSTUVXZ";
|
||||
|
||||
|
||||
@Test
|
||||
@@ -97,10 +93,10 @@ public class BifidTest{
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keyword, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Setting keyword '{}'", keyword);
|
||||
assertEquals(KEYWORD, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Setting keyword '{}'", KEYWORD);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -118,14 +114,14 @@ public class BifidTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
cipher.setInputString(INPUT_STRING);
|
||||
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(INPUT_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -134,14 +130,14 @@ public class BifidTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
cipher.setInputString(INPUT_STRING);
|
||||
|
||||
assertEquals(inputString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(INPUT_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -150,14 +146,14 @@ public class BifidTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
cipher.setInputString(INPUT_STRING);
|
||||
|
||||
assertEquals(inputString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(INPUT_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -166,14 +162,14 @@ public class BifidTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputString(inputString);
|
||||
cipher.setInputString(INPUT_STRING);
|
||||
|
||||
assertEquals(inputString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", inputString);
|
||||
assertEquals(INPUT_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", INPUT_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -214,29 +210,29 @@ public class BifidTest{
|
||||
|
||||
@Test
|
||||
public void testFormatOutput(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.inputString = INPUT_STRING;
|
||||
|
||||
cipher.formatOutput(outputStringClean);
|
||||
cipher.formatOutput(OUTPUT_STRING_CLEAN);
|
||||
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(OUTPUT_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output");
|
||||
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Altering uppercase");
|
||||
verify(logger, times(14)).debug("Altering lowercase");
|
||||
verify(logger, times(2)).debug("Adding symbol");
|
||||
verify(logger, times(1)).debug("Formatted output string '{}'", outputString);
|
||||
verify(logger, times(1)).debug("Formatted output string '{}'", OUTPUT_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = INPUT_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(INPUT_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(OUTPUT_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(1)).debug("Encoding Polybius");
|
||||
verify(logger, times(1)).debug("Splitting Polybius Square message");
|
||||
@@ -246,14 +242,14 @@ public class BifidTest{
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.inputString = outputString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = OUTPUT_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(outputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(inputString, cipher.outputString);
|
||||
assertEquals(OUTPUT_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(INPUT_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(1)).debug("Encoding Polybius Square");
|
||||
verify(logger, times(1)).debug("Splitting Polybius Square message");
|
||||
@@ -263,20 +259,20 @@ public class BifidTest{
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.outputString = outputString;
|
||||
cipher.inputString = INPUT_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.outputString = OUTPUT_STRING;
|
||||
|
||||
assertEquals(inputString, cipher.getInputString());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(outputString, cipher.getOutputString());
|
||||
assertEquals(INPUT_STRING, cipher.getInputString());
|
||||
assertEquals(KEYWORD, cipher.getKeyword());
|
||||
assertEquals(OUTPUT_STRING, cipher.getOutputString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.outputString = outputString;
|
||||
cipher.inputString = INPUT_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.outputString = OUTPUT_STRING;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -290,11 +286,11 @@ public class BifidTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Bifid(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
String output = cipher.encode(KEYWORD, INPUT_STRING);
|
||||
|
||||
assertEquals(inputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(INPUT_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(OUTPUT_STRING, cipher.outputString);
|
||||
assertEquals(output, cipher.outputString);
|
||||
}
|
||||
|
||||
@@ -302,35 +298,35 @@ public class BifidTest{
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Bifid(false, false, false);
|
||||
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
String output = cipher.encode(KEYWORD, INPUT_STRING);
|
||||
|
||||
assertEquals(inputStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(outputStringClean, cipher.outputString);
|
||||
assertEquals(outputStringClean, output);
|
||||
assertEquals(INPUT_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(OUTPUT_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(OUTPUT_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Bifid(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, outputString);
|
||||
String output = cipher.decode(KEYWORD, OUTPUT_STRING);
|
||||
|
||||
assertEquals(outputString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(inputString, cipher.outputString);
|
||||
assertEquals(inputString, output);
|
||||
assertEquals(OUTPUT_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(INPUT_STRING, cipher.outputString);
|
||||
assertEquals(INPUT_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new Bifid(false, false, false);
|
||||
|
||||
String output = cipher.decode(keyword, outputString);
|
||||
String output = cipher.decode(KEYWORD, OUTPUT_STRING);
|
||||
|
||||
assertEquals(outputStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(inputStringClean, cipher.outputString);
|
||||
assertEquals(inputStringClean, output);
|
||||
assertEquals(OUTPUT_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(INPUT_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(INPUT_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//Mattrixwv/src/test/java/com/mattrixwv/cipherstream/polysubstitution/ColumnarTest.java
|
||||
//Mattrixwv
|
||||
// Created: 01-16-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -31,16 +27,16 @@ public class ColumnarTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "Message to*encode";
|
||||
private static final String decodedStringPadded = "Message to*encodexxxxxx";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODEXXXXXX";
|
||||
private static final String encodedString = "Edeomte ac*gosnse";
|
||||
private static final String DECODED_STRING = "Message to*encode";
|
||||
private static final String DECODED_STRING_PADDED = "Message to*encodexxxxxx";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODEXXXXXX";
|
||||
private static final String ENCODED_STRING = "Edeomte ac*gosnse";
|
||||
//TODO: This is a bug that needs fixed
|
||||
private static final String encodedStringPadingAdded = "Edxeoxmte ac*xgoxsnxsex"; //When padding is added to outputString for decoding
|
||||
private static final String encodedStringPadded = "Edxeoxm te*acxgoxsnxsex"; //When padding is left in outputString
|
||||
private static final String encodedStringClean = "EDXEOXMTEACXGOXSNXSEX";
|
||||
private static final String keyword = "ke yw*ord";
|
||||
private static final String keywordClean = "KEYWORD";
|
||||
private static final String ENCODED_STRING_PADDING_ADDED = "Edxeoxmte ac*xgoxsnxsex"; //When padding is added to outputString for decoding
|
||||
private static final String ENCODED_STRING_PADDED = "Edxeoxm te*acxgoxsnxsex"; //When padding is left in outputString
|
||||
private static final String ENCODED_STRING_CLEAN = "EDXEOXMTEACXGOXSNXSEX";
|
||||
private static final String KEYWORD = "ke yw*ord";
|
||||
private static final String KEYWORD_CLEAN = "KEYWORD";
|
||||
private static final ArrayList<ArrayList<Character>> encodeGrid = new ArrayList<>(
|
||||
List.of(
|
||||
new ArrayList<>(
|
||||
@@ -237,17 +233,17 @@ public class ColumnarTest{
|
||||
|
||||
@Test
|
||||
public void testGetCleanInputString(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
String output = cipher.getCleanInputString();
|
||||
|
||||
assertEquals(decodedString.toUpperCase().replaceAll("[^A-Z]", ""), output);
|
||||
assertEquals(DECODED_STRING.toUpperCase().replaceAll("[^A-Z]", ""), output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateGridEncode(){
|
||||
cipher.inputString = decodedStringPadded;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = DECODED_STRING_PADDED;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
|
||||
cipher.createGridEncode();
|
||||
|
||||
@@ -257,8 +253,8 @@ public class ColumnarTest{
|
||||
|
||||
@Test
|
||||
public void testCreateGridDecode(){
|
||||
cipher.inputString = encodedStringPadingAdded;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = ENCODED_STRING_PADDING_ADDED;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
|
||||
cipher.createGridDecode();
|
||||
|
||||
@@ -271,19 +267,19 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Appending {} characters", 6);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_PADDED);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -291,19 +287,19 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = false;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.characterToAdd = 'X';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded.toUpperCase(), cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Appending {} characters", 6);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_PADDED.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -311,19 +307,19 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded.replaceAll("\\s", ""), cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Appending {} characters", 6);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_PADDED.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -331,19 +327,19 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Appending {} characters", 6);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_PADDED.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -351,7 +347,7 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keyword;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -373,7 +369,7 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keyword;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -395,7 +391,7 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keyword;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -417,18 +413,18 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringPadingAdded, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING_PADDING_ADDED, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", ENCODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedStringPadingAdded);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING_PADDING_ADDED);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -436,7 +432,7 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.setInputStringDecode("Message to encod");
|
||||
@@ -455,18 +451,18 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = false;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.characterToAdd = 'X';
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringPadingAdded.toUpperCase(), cipher.inputString);
|
||||
assertEquals(ENCODED_STRING_PADDING_ADDED.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", ENCODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedStringPadingAdded.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING_PADDING_ADDED.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -474,18 +470,18 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringPadingAdded.replaceAll("\\s", ""), cipher.inputString);
|
||||
assertEquals(ENCODED_STRING_PADDING_ADDED.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", ENCODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedStringPadingAdded.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING_PADDING_ADDED.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -493,18 +489,18 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringPadingAdded.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
assertEquals(ENCODED_STRING_PADDING_ADDED.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", ENCODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedStringPadingAdded.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING_PADDING_ADDED.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -512,7 +508,7 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -533,7 +529,7 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -554,7 +550,7 @@ public class ColumnarTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -573,53 +569,53 @@ public class ColumnarTest{
|
||||
@Test
|
||||
public void testCreateOutputStringFromColumns(){
|
||||
cipher.removePadding = true;
|
||||
cipher.inputString = decodedStringPadded;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = DECODED_STRING_PADDED;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = decodeGrid;
|
||||
cipher.charsAdded = 6;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.createOutputStringFromColumns();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Creating output string for encoding");
|
||||
verify(logger, times(1)).debug("Getting added characters");
|
||||
verify(logger, times(1)).debug("Turning grid into string");
|
||||
verify(logger, times(1)).debug("Formatting output string");
|
||||
verify(logger, times(1)).debug("Output string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Output string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateOutputStringFromColumns_padding(){
|
||||
cipher.removePadding = false;
|
||||
cipher.inputString = decodedStringPadded;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = DECODED_STRING_PADDED;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = decodeGrid;
|
||||
cipher.charsAdded = 6;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.createOutputStringFromColumns();
|
||||
|
||||
assertEquals(encodedStringPadded, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_PADDED, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Creating output string for encoding");
|
||||
verify(logger, times(1)).debug("Getting added characters");
|
||||
verify(logger, times(1)).debug("Turning grid into string");
|
||||
verify(logger, times(1)).debug("Formatting output string");
|
||||
verify(logger, times(1)).debug("Output string '{}'", encodedStringPadded);
|
||||
verify(logger, times(1)).debug("Output string '{}'", ENCODED_STRING_PADDED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateOutputStringFromRows(){
|
||||
cipher.removePadding = false;
|
||||
cipher.inputString = encodedStringPadded;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = ENCODED_STRING_PADDED;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = encodeGrid;
|
||||
cipher.charsAdded = 6;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.createOutputStringFromRows();
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Creating output string for decoding");
|
||||
verify(logger, times(1)).debug("Transforming grid to a string");
|
||||
verify(logger, times(1)).debug("Removing padding");
|
||||
@@ -628,21 +624,21 @@ public class ColumnarTest{
|
||||
verify(logger, times(1)).debug("Adding upper case");
|
||||
verify(logger, times(20)).debug("Adding lower case");
|
||||
verify(logger, times(2)).debug("Adding symbol");
|
||||
verify(logger, times(1)).debug("Decoded output string '{}'", decodedStringPadded);
|
||||
verify(logger, times(1)).debug("Decoded output string '{}'", DECODED_STRING_PADDED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateOuputStringFromRows_removePadding(){
|
||||
cipher.removePadding = true;
|
||||
cipher.inputString = encodedStringPadingAdded;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = ENCODED_STRING_PADDING_ADDED;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = encodeGrid;
|
||||
cipher.charsAdded = 6;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.createOutputStringFromRows();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Creating output string for decoding");
|
||||
verify(logger, times(1)).debug("Transforming grid to a string");
|
||||
verify(logger, times(1)).debug("Removing padding");
|
||||
@@ -651,16 +647,16 @@ public class ColumnarTest{
|
||||
verify(logger, times(1)).debug("Adding upper case");
|
||||
verify(logger, times(14)).debug("Adding lower case");
|
||||
verify(logger, times(2)).debug("Adding symbol");
|
||||
verify(logger, times(1)).debug("Decoded output string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Decoded output string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original keyword {}", keyword);
|
||||
verify(logger, times(1)).debug("Cleaned keyword {}", keywordClean);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Original keyword {}", KEYWORD);
|
||||
verify(logger, times(1)).debug("Cleaned keyword {}", KEYWORD_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -722,7 +718,7 @@ public class ColumnarTest{
|
||||
public void testGetKeywordAlphaLocations(){
|
||||
ArrayList<Integer> alphaLocations = new ArrayList<>(List.of(6, 1, 0, 4, 5, 3, 2));
|
||||
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
|
||||
ArrayList<Integer> returnedLocations = cipher.getKeywordAlphaLocations();
|
||||
|
||||
@@ -735,7 +731,7 @@ public class ColumnarTest{
|
||||
public void testGetKeywordOriginalLocations(){
|
||||
ArrayList<Integer> orderedLocations = new ArrayList<>(List.of(2, 1, 6, 5, 3, 4, 0));
|
||||
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
|
||||
ArrayList<Integer> returnedLocations = cipher.getKeywordOriginalLocations();
|
||||
|
||||
@@ -774,14 +770,14 @@ public class ColumnarTest{
|
||||
public void testEncode(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.removePadding = true;
|
||||
cipher.inputString = decodedStringPadded;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = DECODED_STRING_PADDED;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.charsAdded = 6;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(1)).debug("Creating grid for encoding");
|
||||
verify(logger, times(2)).debug("Creating an array of keyword letter locations");
|
||||
@@ -793,14 +789,14 @@ public class ColumnarTest{
|
||||
public void testEncode_padding(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.removePadding = false;
|
||||
cipher.inputString = decodedStringPadded;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = DECODED_STRING_PADDED;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.charsAdded = 6;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedStringPadded, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_PADDED, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(1)).debug("Creating grid for encoding");
|
||||
verify(logger, times(1)).debug("Creating an array of keyword letter locations");
|
||||
@@ -812,14 +808,14 @@ public class ColumnarTest{
|
||||
public void testDecode(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.removePadding = true;
|
||||
cipher.inputString = encodedStringPadingAdded;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = ENCODED_STRING_PADDING_ADDED;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.charsAdded = 6;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(1)).debug("Creating grid for decoding");
|
||||
verify(logger, times(2)).debug("Creating array of original keyword locations");
|
||||
@@ -831,14 +827,14 @@ public class ColumnarTest{
|
||||
public void testDecode_padding(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.removePadding = false;
|
||||
cipher.inputString = encodedStringPadded;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = ENCODED_STRING_PADDED;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.charsAdded = 6;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(1)).debug("Creating grid for decoding");
|
||||
verify(logger, times(1)).debug("Creating array of original keyword locations");
|
||||
@@ -848,20 +844,20 @@ public class ColumnarTest{
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(KEYWORD, cipher.getKeyword());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.grid = encodeGrid;
|
||||
cipher.charsAdded = 5;
|
||||
|
||||
@@ -878,71 +874,71 @@ public class ColumnarTest{
|
||||
public void testPracticalEncode(){
|
||||
cipher = new Columnar(true, true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncode_noPadding(){
|
||||
cipher = new Columnar(true, true, true, false);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedStringPadded, cipher.outputString);
|
||||
assertEquals(encodedStringPadded, output);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_PADDED, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_PADDED, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncode_clean(){
|
||||
cipher = new Columnar(false, false, false, false);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecode(){
|
||||
cipher = new Columnar(true, true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringPadingAdded, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING_PADDING_ADDED, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecode_noPadding(){
|
||||
cipher = new Columnar(true, true, true, false);
|
||||
|
||||
String output = cipher.decode(keyword, encodedStringPadded);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING_PADDED);
|
||||
|
||||
assertEquals(encodedStringPadded, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedStringPadded, cipher.outputString);
|
||||
assertEquals(decodedStringPadded, output);
|
||||
assertEquals(ENCODED_STRING_PADDED, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_PADDED, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecode_clean(){
|
||||
cipher = new Columnar(false, false, false, false);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/polysubstitution/HillTest.java
|
||||
//Mattrixwv
|
||||
// Created: 01-31-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -31,13 +27,13 @@ public class HillTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Fields
|
||||
private static final String decodedString = "Message to^encoded";
|
||||
private static final String decodedStringPadded = "Message to^encodedxx";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODEDXX";
|
||||
private static final String encodedString = "Mgkeqge ul^ikhisplrd";
|
||||
private static final String encodedStringClean = "MGKEQGEULIKHISPLRD";
|
||||
private static final int[][] keyArray = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||
private static final ModMatrix key = new ModMatrix(keyArray, 26);
|
||||
private static final String DECODED_STRING = "Message to^encoded";
|
||||
private static final String DECODED_STRING_PADDED = "Message to^encodedxx";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODEDXX";
|
||||
private static final String ENCODED_STRING = "Mgkeqge ul^ikhisplrd";
|
||||
private static final String ENCODED_STRING_CLEAN = "MGKEQGEULIKHISPLRD";
|
||||
private static final int[][] KEY_ARRAY = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
|
||||
private static final ModMatrix KEY = new ModMatrix(KEY_ARRAY, 26);
|
||||
|
||||
|
||||
@Test
|
||||
@@ -133,19 +129,19 @@ public class HillTest{
|
||||
|
||||
@Test
|
||||
public void testSetKey(){
|
||||
cipher.setKey(key);
|
||||
cipher.setKey(KEY);
|
||||
|
||||
assertEquals(key, cipher.key);
|
||||
assertEquals(KEY, cipher.key);
|
||||
verify(logger, times(1)).debug("Setting key");
|
||||
verify(logger, times(1)).debug("Testing mod");
|
||||
verify(logger, times(1)).debug("Testing square");
|
||||
verify(logger, times(1)).debug("Testing invertable");
|
||||
verify(logger, times(1)).debug("key\n{}", key);
|
||||
verify(logger, times(1)).debug("key\n{}", KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKey_invalidMod(){
|
||||
ModMatrix matrix = new ModMatrix(keyArray, 30);
|
||||
ModMatrix matrix = new ModMatrix(KEY_ARRAY, 30);
|
||||
|
||||
assertThrows(InvalidKeyException.class, () -> {
|
||||
cipher.setKey(matrix);
|
||||
@@ -196,20 +192,20 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Checking length");
|
||||
verify(logger, times(1)).debug("Adding {} characters", 2);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_PADDED);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -217,20 +213,20 @@ public class HillTest{
|
||||
cipher.preserveCapitals = false;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
cipher.characterToAdd = 'X';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded.toUpperCase(), cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Checking length");
|
||||
verify(logger, times(1)).debug("Adding {} characters", 2);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_PADDED.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -238,20 +234,20 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded.replaceAll("\\s", ""), cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Checking length");
|
||||
verify(logger, times(1)).debug("Adding {} characters", 2);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_PADDED.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -259,20 +255,20 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Checking length");
|
||||
verify(logger, times(1)).debug("Adding {} characters", 2);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_PADDED.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -280,7 +276,7 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -303,7 +299,7 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -326,7 +322,7 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
cipher.characterToAdd = 'x';
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
@@ -349,17 +345,17 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", ENCODED_STRING);
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING);
|
||||
verify(logger, times(1)).debug("Checking length");
|
||||
}
|
||||
|
||||
@@ -368,17 +364,17 @@ public class HillTest{
|
||||
cipher.preserveCapitals = false;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString.toUpperCase(), cipher.inputString);
|
||||
assertEquals(ENCODED_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", ENCODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING.toUpperCase());
|
||||
verify(logger, times(1)).debug("Checking length");
|
||||
}
|
||||
|
||||
@@ -387,17 +383,17 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||
assertEquals(ENCODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", ENCODED_STRING);
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Checking length");
|
||||
}
|
||||
|
||||
@@ -406,17 +402,17 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
assertEquals(ENCODED_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", ENCODED_STRING);
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Checking length");
|
||||
}
|
||||
|
||||
@@ -425,19 +421,19 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputStringDecode(encodedString + "a");
|
||||
cipher.setInputStringDecode(ENCODED_STRING + "a");
|
||||
});
|
||||
|
||||
assertEquals(encodedString + "a", cipher.inputString);
|
||||
assertEquals(ENCODED_STRING + "a", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", encodedString + "a");
|
||||
verify(logger, times(1)).debug("Original input string '{}'", ENCODED_STRING + "a");
|
||||
verify(logger, never()).debug("Removing capitals");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString + "a");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING + "a");
|
||||
verify(logger, times(1)).debug("Checking length");
|
||||
}
|
||||
|
||||
@@ -446,7 +442,7 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputStringDecode("");
|
||||
@@ -467,7 +463,7 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputStringDecode("*^&");
|
||||
@@ -488,7 +484,7 @@ public class HillTest{
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.key = key;
|
||||
cipher.key = KEY;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputStringDecode(null);
|
||||
@@ -506,13 +502,13 @@ public class HillTest{
|
||||
|
||||
@Test
|
||||
public void testGetCleanInputString(){
|
||||
cipher.inputString = decodedStringPadded;
|
||||
cipher.inputString = DECODED_STRING_PADDED;
|
||||
|
||||
String output = cipher.getCleanInputString();
|
||||
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
verify(logger, times(1)).debug("Cleaning inputString");
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", decodedStringClean);
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", DECODED_STRING_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -555,18 +551,18 @@ public class HillTest{
|
||||
|
||||
@Test
|
||||
public void testPolishOutputString(){
|
||||
cipher.inputString = decodedStringPadded;
|
||||
cipher.outputString = encodedStringClean;
|
||||
cipher.inputString = DECODED_STRING_PADDED;
|
||||
cipher.outputString = ENCODED_STRING_CLEAN;
|
||||
|
||||
String output = cipher.polishOutputString();
|
||||
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
verify(logger, times(1)).debug("Polishing output string");
|
||||
verify(logger, times(20)).debug(eq("Current char {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Uppercase");
|
||||
verify(logger, times(17)).debug("Lowercase");
|
||||
verify(logger, times(2)).debug("Symbol");
|
||||
verify(logger, times(1)).debug("Polished string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Polished string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -579,8 +575,8 @@ public class HillTest{
|
||||
expectedVectors.add(new ModMatrix(new int[][]{{14}, { 3}, { 4}}, 26));
|
||||
expectedVectors.add(new ModMatrix(new int[][]{{ 3}, {23}, {23}}, 26));
|
||||
|
||||
cipher.inputString = decodedStringPadded;
|
||||
cipher.key = key;
|
||||
cipher.inputString = DECODED_STRING_PADDED;
|
||||
cipher.key = KEY;
|
||||
|
||||
ArrayList<ModMatrix> returnedVectors = cipher.getInputVectors();
|
||||
|
||||
@@ -593,29 +589,29 @@ public class HillTest{
|
||||
@Test
|
||||
public void testGetOutputFromVectors(){
|
||||
ArrayList<ModMatrix> outputVectors = new ArrayList<>();
|
||||
outputVectors.add(key.multiply(new ModMatrix(new int[][]{{12}, { 4}, {18}}, 26)));
|
||||
outputVectors.add(key.multiply(new ModMatrix(new int[][]{{18}, { 0}, { 6}}, 26)));
|
||||
outputVectors.add(key.multiply(new ModMatrix(new int[][]{{ 4}, {19}, {14}}, 26)));
|
||||
outputVectors.add(key.multiply(new ModMatrix(new int[][]{{ 4}, {13}, { 2}}, 26)));
|
||||
outputVectors.add(key.multiply(new ModMatrix(new int[][]{{14}, { 3}, { 4}}, 26)));
|
||||
outputVectors.add(key.multiply(new ModMatrix(new int[][]{{ 3}, {23}, {23}}, 26)));
|
||||
outputVectors.add(KEY.multiply(new ModMatrix(new int[][]{{12}, { 4}, {18}}, 26)));
|
||||
outputVectors.add(KEY.multiply(new ModMatrix(new int[][]{{18}, { 0}, { 6}}, 26)));
|
||||
outputVectors.add(KEY.multiply(new ModMatrix(new int[][]{{ 4}, {19}, {14}}, 26)));
|
||||
outputVectors.add(KEY.multiply(new ModMatrix(new int[][]{{ 4}, {13}, { 2}}, 26)));
|
||||
outputVectors.add(KEY.multiply(new ModMatrix(new int[][]{{14}, { 3}, { 4}}, 26)));
|
||||
outputVectors.add(KEY.multiply(new ModMatrix(new int[][]{{ 3}, {23}, {23}}, 26)));
|
||||
|
||||
String returnedString = cipher.getOutputFromVectors(outputVectors);
|
||||
|
||||
assertEquals(encodedStringClean, returnedString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, returnedString);
|
||||
verify(logger, times(1)).debug("Turning vectors into a string");
|
||||
verify(logger, times(6)).debug(eq("Current vector {}"), any(ModMatrix.class));
|
||||
verify(logger, times(1)).debug("Converted string '{}'", encodedStringClean);
|
||||
verify(logger, times(1)).debug("Converted string '{}'", ENCODED_STRING_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.inputString = decodedStringPadded;
|
||||
cipher.key = key;
|
||||
cipher.inputString = DECODED_STRING_PADDED;
|
||||
cipher.key = KEY;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(1)).debug("Multiplying vectors");
|
||||
verify(logger, times(6)).debug(eq("Current input vector {}"), any(ModMatrix.class));
|
||||
@@ -624,35 +620,35 @@ public class HillTest{
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.inputString = encodedString;
|
||||
cipher.key = key;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
cipher.key = KEY;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(1)).debug("Getting inverse of key");
|
||||
verify(logger, times(1)).debug("Inverse of key {}", key.inverse());
|
||||
verify(logger, times(1)).debug("Inverse of key {}", KEY.inverse());
|
||||
verify(logger, times(6)).debug(eq("Current input vector {}"), any(ModMatrix.class));
|
||||
verify(logger, times(6)).debug(eq("Multiplied vector {}"), any(ModMatrix.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.key = key;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.key = KEY;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(key, cipher.getKey());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
assertEquals(KEY, cipher.getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.key = key;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.key = KEY;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -666,46 +662,46 @@ public class HillTest{
|
||||
public void testPracticalEncode(){
|
||||
cipher = new Hill(true, true, true);
|
||||
|
||||
String output = cipher.encode(key, decodedString);
|
||||
String output = cipher.encode(KEY, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.inputString);
|
||||
assertEquals(key, cipher.key);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.inputString);
|
||||
assertEquals(KEY, cipher.key);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncode_clean(){
|
||||
cipher = new Hill(false, false, false);
|
||||
|
||||
String output = cipher.encode(key, decodedString);
|
||||
String output = cipher.encode(KEY, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(key, cipher.key);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEY, cipher.key);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncode_array(){
|
||||
cipher = new Hill(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyArray, decodedString);
|
||||
String output = cipher.encode(KEY_ARRAY, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.inputString);
|
||||
assertEquals(key, cipher.key);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.inputString);
|
||||
assertEquals(KEY, cipher.key);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncode_padding(){
|
||||
cipher = new Hill(true, true, true, 'j');
|
||||
|
||||
String output = cipher.encode(key, decodedString);
|
||||
String output = cipher.encode(KEY, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString + "jj", cipher.inputString);
|
||||
assertEquals(key, cipher.key);
|
||||
assertEquals(DECODED_STRING + "jj", cipher.inputString);
|
||||
assertEquals(KEY, cipher.key);
|
||||
assertEquals("Mgkeqge ul^ikhispfzn", cipher.outputString);
|
||||
assertEquals("Mgkeqge ul^ikhispfzn", output);
|
||||
}
|
||||
@@ -714,47 +710,47 @@ public class HillTest{
|
||||
public void testPracticalDecode(){
|
||||
cipher = new Hill(true, true, true);
|
||||
|
||||
String output = cipher.decode(key, encodedString);
|
||||
String output = cipher.decode(KEY, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(key, cipher.key);
|
||||
assertEquals(decodedStringPadded, cipher.outputString);
|
||||
assertEquals(decodedStringPadded, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEY, cipher.key);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_PADDED, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecode_clean(){
|
||||
cipher = new Hill(false, false, false);
|
||||
|
||||
String output = cipher.decode(key, encodedString);
|
||||
String output = cipher.decode(KEY, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(key, cipher.key);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEY, cipher.key);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecode_array(){
|
||||
cipher = new Hill(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyArray, encodedString);
|
||||
String output = cipher.decode(KEY_ARRAY, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(key, cipher.key);
|
||||
assertEquals(decodedStringPadded, cipher.outputString);
|
||||
assertEquals(decodedStringPadded, output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEY, cipher.key);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_PADDED, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecode_padding(){
|
||||
cipher = new Hill(true, true, true, 'j');
|
||||
|
||||
String output = cipher.decode(key, "Mgkeqge ul^ikhispfzn");
|
||||
String output = cipher.decode(KEY, "Mgkeqge ul^ikhispfzn");
|
||||
|
||||
assertEquals("Mgkeqge ul^ikhispfzn", cipher.inputString);
|
||||
assertEquals(key, cipher.key);
|
||||
assertEquals(decodedString + "jj", cipher.outputString);
|
||||
assertEquals(decodedString + "jj", output);
|
||||
assertEquals(KEY, cipher.key);
|
||||
assertEquals(DECODED_STRING + "jj", cipher.outputString);
|
||||
assertEquals(DECODED_STRING + "jj", output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/polysubstitution/LargePolybiusSquareTest.java
|
||||
//Mattrixwv
|
||||
// Created: 04-21-23
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -27,14 +23,14 @@ public class LargePolybiusSquareTest{
|
||||
@Mock(name = "com.mattrixwv.cipherstream.polysubstitution.LargePolybiusSquare")
|
||||
private Logger logger;
|
||||
//Variables
|
||||
private static final String decodedString = "Message to^encode";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODE";
|
||||
private static final String encodedString = "35124343222612 4415^123624152112";
|
||||
private static final String encodedStringClean = "31 15 41 41 11 21 15 42 33 15 32 13 33 14 15";
|
||||
private static final String keyword = "ke yw*ord";
|
||||
private static final String keywordClean = "KEYWORDABCFGHIJLMNPQSTUVXZ0123456789";
|
||||
private static final String keywordBlank = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
private static final char[][] grid = {
|
||||
private static final String DECODED_STRING = "Message to^encode";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String ENCODED_STRING = "35124343222612 4415^123624152112";
|
||||
private static final String ENCODED_STRING_CLEAN = "31 15 41 41 11 21 15 42 33 15 32 13 33 14 15";
|
||||
private static final String KEYWORD = "ke yw*ord";
|
||||
private static final String KEYWORD_CLEAN = "KEYWORDABCFGHIJLMNPQSTUVXZ0123456789";
|
||||
private static final String KEYWORD_BLANK = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
private static final char[][] GRID = {
|
||||
{'K', 'E', 'Y', 'W', 'O', 'R'},
|
||||
{'D', 'A', 'B', 'C', 'F', 'G'},
|
||||
{'H', 'I', 'J', 'L', 'M', 'N'},
|
||||
@@ -42,7 +38,7 @@ public class LargePolybiusSquareTest{
|
||||
{'X', 'Z', '0', '1', '2', '3'},
|
||||
{'4', '5', '6', '7', '8', '9'}
|
||||
};
|
||||
private static final char[][] gridClean = {
|
||||
private static final char[][] GRID_CLEAN = {
|
||||
{'A', 'B', 'C', 'D', 'E', 'F'},
|
||||
{'G', 'H', 'I', 'J', 'K', 'L'},
|
||||
{'M', 'N', 'O', 'P', 'Q', 'R'},
|
||||
@@ -90,11 +86,11 @@ public class LargePolybiusSquareTest{
|
||||
|
||||
@Test
|
||||
public void testCreateGrid(){
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
|
||||
cipher.createGrid();
|
||||
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
verify(logger, times(1)).debug("Creating grid");
|
||||
}
|
||||
|
||||
@@ -103,13 +99,13 @@ public class LargePolybiusSquareTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -117,13 +113,13 @@ public class LargePolybiusSquareTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase().replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase().replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -131,13 +127,13 @@ public class LargePolybiusSquareTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase().replaceAll("[^a-zA-Z0-9\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -145,10 +141,10 @@ public class LargePolybiusSquareTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals("M E S S A G E T O E N C O D E", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", "M E S S A G E T O E N C O D E");
|
||||
@@ -204,34 +200,34 @@ public class LargePolybiusSquareTest{
|
||||
|
||||
@Test
|
||||
public void testGetPreparedInputStringEncoding(){
|
||||
cipher.inputString = decodedString.toUpperCase();
|
||||
cipher.inputString = DECODED_STRING.toUpperCase();
|
||||
|
||||
String preparedInputString = cipher.getPreparedInputStringEncode();
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
assertEquals(decodedString.toUpperCase().replaceAll("[^A-Z0-9]", ""), preparedInputString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
assertEquals(DECODED_STRING.toUpperCase().replaceAll("[^A-Z0-9]", ""), preparedInputString);
|
||||
verify(logger, times(1)).debug("Preparing input string for encoding");
|
||||
verify(logger, times(1)).debug("Prepared input string '{}'", preparedInputString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", keyword);
|
||||
verify(logger, times(1)).debug("Cleaned keyword '{}'", keywordClean);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", KEYWORD);
|
||||
verify(logger, times(1)).debug("Cleaned keyword '{}'", KEYWORD_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKeyword_blank(){
|
||||
cipher.setKeyword("");
|
||||
|
||||
assertEquals(keywordBlank, cipher.keyword);
|
||||
assertArrayEquals(gridClean, cipher.grid);
|
||||
assertEquals(KEYWORD_BLANK, cipher.keyword);
|
||||
assertArrayEquals(GRID_CLEAN, cipher.grid);
|
||||
verify(logger, times(1)).debug("Original keyword '{}'", "");
|
||||
verify(logger, times(1)).debug("Cleaned keyword '{}'", keywordBlank);
|
||||
verify(logger, times(1)).debug("Cleaned keyword '{}'", KEYWORD_BLANK);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -250,43 +246,43 @@ public class LargePolybiusSquareTest{
|
||||
public void testAddCharactersToCleanStringEncode(){
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = decodedString.toUpperCase();
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = GRID;
|
||||
cipher.inputString = DECODED_STRING.toUpperCase();
|
||||
|
||||
cipher.addCharactersToCleanStringEncode(encodedString.replaceAll("\\s", "").replaceAll("[^0-9]", ""));
|
||||
cipher.addCharactersToCleanStringEncode(ENCODED_STRING.replaceAll("\\s", "").replaceAll("[^0-9]", ""));
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output string");
|
||||
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
|
||||
verify(logger, times(15)).debug("Appending character");
|
||||
verify(logger, times(2)).debug("Appending symbol");
|
||||
verify(logger, times(1)).debug("Saving output string {}", encodedString);
|
||||
verify(logger, times(1)).debug("Saving output string {}", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddCharactersToCleanStringDecode(){
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = encodedString;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = GRID;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
|
||||
cipher.addCharactersToCleanStringDecode(decodedStringClean);
|
||||
cipher.addCharactersToCleanStringDecode(DECODED_STRING_CLEAN);
|
||||
|
||||
verify(logger, times(1)).debug("Formatting output string");
|
||||
verify(logger, times(17)).debug(eq("Current character {}"), anyChar());
|
||||
verify(logger, times(15)).debug("Appending character");
|
||||
verify(logger, times(2)).debug("Appending symbol");
|
||||
verify(logger, times(1)).debug("Saving output string {}", decodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Saving output string {}", DECODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.grid = GRID;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -302,47 +298,47 @@ public class LargePolybiusSquareTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new LargePolybiusSquare(true, true);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new LargePolybiusSquare(false, false);
|
||||
|
||||
String output = cipher.encode(decodedString);
|
||||
String output = cipher.encode(DECODED_STRING);
|
||||
|
||||
assertEquals("M E S S A G E T O E N C O D E", cipher.inputString);
|
||||
assertEquals(keywordBlank, cipher.keyword);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertEquals(KEYWORD_BLANK, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new LargePolybiusSquare(true, true);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedString.toUpperCase(), cipher.outputString);
|
||||
assertEquals(decodedString.toUpperCase(), output);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.outputString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean(){
|
||||
cipher = new LargePolybiusSquare(false, false);
|
||||
|
||||
String output = cipher.decode(encodedStringClean);
|
||||
String output = cipher.decode(ENCODED_STRING_CLEAN);
|
||||
|
||||
assertEquals(encodedStringClean.replaceAll("\\s", ""), cipher.inputString);
|
||||
assertEquals(keywordBlank, cipher.keyword);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_CLEAN.replaceAll("\\s", ""), cipher.inputString);
|
||||
assertEquals(KEYWORD_BLANK, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/MoresTest.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-28-21
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -26,9 +22,9 @@ public class MorseTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Fields
|
||||
private static final String inputString = "Message to^encode123";
|
||||
private static final String inputStringClean = "MESSAGETOENCODE123";
|
||||
private static final String outputString = "-- . ... ... .- --. . - --- . -. -.-. --- -.. . .---- ..--- ...--";
|
||||
private static final String INPUT_STRING = "Message to^encode123";
|
||||
private static final String INPUT_STRING_CLEAN = "MESSAGETOENCODE123";
|
||||
private static final String OUTPUT_STRING = "-- . ... ... .- --. . - --- . -. -.-. --- -.. . .---- ..--- ...--";
|
||||
|
||||
|
||||
@Test
|
||||
@@ -41,13 +37,13 @@ public class MorseTest{
|
||||
|
||||
@Test
|
||||
public void testSetInputStringEncode(){
|
||||
cipher.setInputStringEncode(inputString);
|
||||
cipher.setInputStringEncode(INPUT_STRING);
|
||||
|
||||
assertEquals(inputStringClean, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", inputString);
|
||||
assertEquals(INPUT_STRING_CLEAN, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", INPUT_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace and symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", inputStringClean);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", INPUT_STRING_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -93,10 +89,10 @@ public class MorseTest{
|
||||
|
||||
@Test
|
||||
public void testSetInputStringDecode(){
|
||||
cipher.setInputStringDecode(outputString);
|
||||
cipher.setInputStringDecode(OUTPUT_STRING);
|
||||
|
||||
assertEquals(outputString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", outputString);
|
||||
assertEquals(OUTPUT_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", OUTPUT_STRING);
|
||||
verify(logger, times(1)).debug("Checking for invalid characters");
|
||||
verify(logger, times(1)).debug("Saving");
|
||||
}
|
||||
@@ -141,16 +137,16 @@ public class MorseTest{
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.inputString = inputStringClean;
|
||||
cipher.inputString = INPUT_STRING_CLEAN;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(OUTPUT_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(18)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(15)).debug("Appending letter");
|
||||
verify(logger, times(3)).debug("Appending number");
|
||||
verify(logger, times(1)).debug("Saving encoded string '{}'", outputString);
|
||||
verify(logger, times(1)).debug("Saving encoded string '{}'", OUTPUT_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -166,20 +162,20 @@ public class MorseTest{
|
||||
verify(logger, times(1)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, never()).debug("Appending letter");
|
||||
verify(logger, never()).debug("Appending number");
|
||||
verify(logger, never()).debug("Saving encoded string '{}'", outputString);
|
||||
verify(logger, never()).debug("Saving encoded string '{}'", OUTPUT_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.inputString = outputString;
|
||||
cipher.inputString = OUTPUT_STRING;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(inputStringClean, cipher.outputString);
|
||||
assertEquals(INPUT_STRING_CLEAN, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(18)).debug(eq("Working letter {}"), anyString());
|
||||
verify(logger, times(18)).debug(eq("Decoded letter {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Saving decoded string '{}'", inputStringClean);
|
||||
verify(logger, times(1)).debug("Saving decoded string '{}'", INPUT_STRING_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -199,17 +195,17 @@ public class MorseTest{
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.outputString = outputString;
|
||||
cipher.inputString = INPUT_STRING;
|
||||
cipher.outputString = OUTPUT_STRING;
|
||||
|
||||
assertEquals(inputString, cipher.getInputString());
|
||||
assertEquals(outputString, cipher.getOutputString());
|
||||
assertEquals(INPUT_STRING, cipher.getInputString());
|
||||
assertEquals(OUTPUT_STRING, cipher.getOutputString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = inputString;
|
||||
cipher.outputString = outputString;
|
||||
cipher.inputString = INPUT_STRING;
|
||||
cipher.outputString = OUTPUT_STRING;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -220,19 +216,19 @@ public class MorseTest{
|
||||
|
||||
@Test
|
||||
public void testPracticalEncode(){
|
||||
String output = cipher.encode(inputString);
|
||||
String output = cipher.encode(INPUT_STRING);
|
||||
|
||||
assertEquals(inputStringClean, cipher.inputString);
|
||||
assertEquals(outputString, cipher.outputString);
|
||||
assertEquals(outputString, output);
|
||||
assertEquals(INPUT_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(OUTPUT_STRING, cipher.outputString);
|
||||
assertEquals(OUTPUT_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecode(){
|
||||
String output = cipher.decode(outputString);
|
||||
String output = cipher.decode(OUTPUT_STRING);
|
||||
|
||||
assertEquals(outputString, cipher.inputString);
|
||||
assertEquals(inputStringClean, cipher.outputString);
|
||||
assertEquals(inputStringClean, output);
|
||||
assertEquals(OUTPUT_STRING, cipher.inputString);
|
||||
assertEquals(INPUT_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(INPUT_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/PlayfairTest.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-30-21
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -29,14 +25,14 @@ public class PlayfairTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Fields
|
||||
private static final String decodedString = "Hide the gold in - the@tree+stump";
|
||||
private static final String decodedStringPadded = "Hide the gold in - the@trexe+stump";
|
||||
private static final String decodedStringClean = "HIDETHEGOLDINTHETREXESTUMP";
|
||||
private static final String encodedString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
|
||||
private static final String encodedStringClean = "BMODZBXDNABEKUDMUIXMMOUVIF";
|
||||
private static final String keyword = "Play-fair@Exam ple";
|
||||
private static final String keywordClean = "PLAYFIREXMBCDGHKNOQSTUVWZ";
|
||||
private static final char[][] grid = new char[][]{
|
||||
private static final String DECODED_STRING = "Hide the gold in - the@tree+stump";
|
||||
private static final String DECODED_STRING_PADDED = "Hide the gold in - the@trexe+stump";
|
||||
private static final String DECODED_STRING_CLEAN = "HIDETHEGOLDINTHETREXESTUMP";
|
||||
private static final String ENCODED_STRING = "Bmod zbx dnab ek - udm@uixmm+ouvif";
|
||||
private static final String ENCODED_STRING_CLEAN = "BMODZBXDNABEKUDMUIXMMOUVIF";
|
||||
private static final String KEYWORD = "Play-fair@Exam ple";
|
||||
private static final String KEYWORD_CLEAN = "PLAYFIREXMBCDGHKNOQSTUVWZ";
|
||||
private static final char[][] GRID = new char[][]{
|
||||
{'P', 'L', 'A', 'Y', 'F'},
|
||||
{'I', 'R', 'E', 'X', 'M'},
|
||||
{'B', 'C', 'D', 'G', 'H'},
|
||||
@@ -394,11 +390,11 @@ public class PlayfairTest{
|
||||
@Test
|
||||
public void testCreateGrid(){
|
||||
String expectedGridString = "[P L A Y F]\n[I R E X M]\n[B C D G H]\n[K N O Q S]\n[T U V W Z]";
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
|
||||
cipher.createGrid();
|
||||
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
verify(logger, times(1)).debug("Creating grid from keyword");
|
||||
verify(logger, times(25)).debug(eq("Letter {} going to position [{}] [{}]"), anyChar(), anyInt(), anyInt());
|
||||
verify(logger, times(1)).debug("Grid\n{}", expectedGridString);
|
||||
@@ -411,11 +407,11 @@ public class PlayfairTest{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.doubled = 'x';
|
||||
|
||||
cipher.setInputString(decodedString, true);
|
||||
cipher.setInputString(DECODED_STRING, true);
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string");
|
||||
verify(logger, times(1)).debug("Original input string {}", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string {}", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
@@ -428,11 +424,11 @@ public class PlayfairTest{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.doubled = 'X';
|
||||
|
||||
cipher.setInputString(decodedString, true);
|
||||
cipher.setInputString(DECODED_STRING, true);
|
||||
|
||||
assertEquals(decodedStringPadded.toUpperCase(), cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string");
|
||||
verify(logger, times(1)).debug("Original input string {}", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string {}", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
@@ -445,11 +441,11 @@ public class PlayfairTest{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.doubled = 'x';
|
||||
|
||||
cipher.setInputString(decodedString, true);
|
||||
cipher.setInputString(DECODED_STRING, true);
|
||||
|
||||
assertEquals(decodedStringPadded.replaceAll("\\s", ""), cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string");
|
||||
verify(logger, times(1)).debug("Original input string {}", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string {}", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
@@ -462,11 +458,11 @@ public class PlayfairTest{
|
||||
cipher.preserveSymbols = false;
|
||||
cipher.doubled = 'x';
|
||||
|
||||
cipher.setInputString(decodedString, true);
|
||||
cipher.setInputString(DECODED_STRING, true);
|
||||
|
||||
assertEquals(decodedStringPadded.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string");
|
||||
verify(logger, times(1)).debug("Original input string {}", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string {}", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
@@ -498,15 +494,15 @@ public class PlayfairTest{
|
||||
cipher.preserveSymbols = true;
|
||||
cipher.doubled = 'x';
|
||||
|
||||
cipher.setInputString(encodedString, false);
|
||||
cipher.setInputString(ENCODED_STRING, false);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string");
|
||||
verify(logger, times(1)).debug("Original input string {}", encodedString);
|
||||
verify(logger, times(1)).debug("Original input string {}", ENCODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -517,16 +513,16 @@ public class PlayfairTest{
|
||||
cipher.doubled = 'x';
|
||||
|
||||
assertThrows(InvalidCharacterException.class, () -> {
|
||||
cipher.setInputString(encodedString + cipher.replaced, false);
|
||||
cipher.setInputString(ENCODED_STRING + cipher.replaced, false);
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string");
|
||||
verify(logger, times(1)).debug("Original input string {}", encodedString + Character.toString(cipher.replaced));
|
||||
verify(logger, times(1)).debug("Original input string {}", ENCODED_STRING + Character.toString(cipher.replaced));
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, never()).debug("Clean input string '{}'", encodedString);
|
||||
verify(logger, never()).debug("Clean input string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -537,16 +533,16 @@ public class PlayfairTest{
|
||||
cipher.doubled = 'x';
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputString(encodedString + "a", false);
|
||||
cipher.setInputString(ENCODED_STRING + "a", false);
|
||||
});
|
||||
|
||||
assertEquals(encodedString + "a", cipher.inputString);
|
||||
assertEquals(ENCODED_STRING + "a", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string");
|
||||
verify(logger, times(1)).debug("Original input string {}", encodedString + "a");
|
||||
verify(logger, times(1)).debug("Original input string {}", ENCODED_STRING + "a");
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", encodedString + "a");
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", ENCODED_STRING + "a");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -593,48 +589,48 @@ public class PlayfairTest{
|
||||
public void testSetInputStringEncode(){
|
||||
cipher.doubled = 'x';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Cleaning up input string for encoding");
|
||||
verify(logger, times(1)).debug("Replacing all {} with {}", cipher.replaced, cipher.replacer);
|
||||
verify(logger, times(13)).debug(eq("Starting at character {}"), anyInt());
|
||||
verify(logger, times(13)).debug(eq("Adding to clean input: {} {} {} {}"), any(StringBuilder.class), anyChar(), any(StringBuilder.class), anyChar());
|
||||
verify(logger, times(1)).debug("Checking odd characters");
|
||||
verify(logger, never()).debug("Adding final character to make even");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_PADDED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputStringEncode_odd(){
|
||||
cipher.doubled = 'x';
|
||||
|
||||
cipher.setInputStringEncode(decodedStringPadded + 'a');
|
||||
cipher.setInputStringEncode(DECODED_STRING_PADDED + 'a');
|
||||
|
||||
assertEquals(decodedStringPadded + "ax", cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED + "ax", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Cleaning up input string for encoding");
|
||||
verify(logger, times(1)).debug("Replacing all {} with {}", cipher.replaced, cipher.replacer);
|
||||
verify(logger, times(14)).debug(eq("Starting at character {}"), anyInt());
|
||||
verify(logger, times(14)).debug(eq("Adding to clean input: {} {} {} {}"), any(StringBuilder.class), anyChar(), any(StringBuilder.class), anyChar());
|
||||
verify(logger, times(1)).debug("Checking odd characters");
|
||||
verify(logger, times(1)).debug("Adding final character to make even");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded + "ax");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_PADDED + "ax");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInputString_oddEndSymbol(){
|
||||
cipher.doubled = 'x';
|
||||
|
||||
cipher.setInputStringEncode(decodedStringPadded + "a*");
|
||||
cipher.setInputStringEncode(DECODED_STRING_PADDED + "a*");
|
||||
|
||||
assertEquals(decodedStringPadded + "a*x", cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED + "a*x", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Cleaning up input string for encoding");
|
||||
verify(logger, times(1)).debug("Replacing all {} with {}", cipher.replaced, cipher.replacer);
|
||||
verify(logger, times(14)).debug(eq("Starting at character {}"), anyInt());
|
||||
verify(logger, times(14)).debug(eq("Adding to clean input: {} {} {} {}"), any(StringBuilder.class), anyChar(), any(StringBuilder.class), anyChar());
|
||||
verify(logger, times(1)).debug("Checking odd characters");
|
||||
verify(logger, times(1)).debug("Adding final character to make even");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded + "a*x");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_PADDED + "a*x");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -642,41 +638,41 @@ public class PlayfairTest{
|
||||
cipher.doubled = 'x';
|
||||
cipher.replacer = 'i';
|
||||
|
||||
cipher.setInputStringEncode(decodedStringPadded + 'x');
|
||||
cipher.setInputStringEncode(DECODED_STRING_PADDED + 'x');
|
||||
|
||||
assertEquals(decodedStringPadded + "xi", cipher.inputString);
|
||||
assertEquals(DECODED_STRING_PADDED + "xi", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Cleaning up input string for encoding");
|
||||
verify(logger, times(1)).debug("Replacing all {} with {}", cipher.replaced, cipher.replacer);
|
||||
verify(logger, times(14)).debug(eq("Starting at character {}"), anyInt());
|
||||
verify(logger, times(14)).debug(eq("Adding to clean input: {} {} {} {}"), any(StringBuilder.class), anyChar(), any(StringBuilder.class), anyChar());
|
||||
verify(logger, times(1)).debug("Checking odd characters");
|
||||
verify(logger, times(1)).debug("Adding final character to make even");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringPadded + "xi");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_PADDED + "xi");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPreparedInputString(){
|
||||
cipher.inputString = decodedStringPadded;
|
||||
cipher.inputString = DECODED_STRING_PADDED;
|
||||
|
||||
String output = cipher.getPreparedInputString();
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
verify(logger, times(1)).debug("Getting input string ready for encoding");
|
||||
verify(logger, times(1)).debug("Prepared string '{}'", decodedStringClean);
|
||||
verify(logger, times(1)).debug("Prepared string '{}'", DECODED_STRING_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
verify(logger, times(1)).debug("Setting keyword");
|
||||
verify(logger, times(1)).debug("Original keyword {}", keyword);
|
||||
verify(logger, times(1)).debug("Original keyword {}", KEYWORD);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing all non-letter characters");
|
||||
verify(logger, times(1)).debug("Appending the alphabet to the keyword");
|
||||
verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer);
|
||||
verify(logger, times(1)).debug("Removing duplicate characters");
|
||||
verify(logger, times(1)).debug("Cleaned keyword {}", keywordClean);
|
||||
verify(logger, times(1)).debug("Cleaned keyword {}", KEYWORD_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -698,7 +694,7 @@ public class PlayfairTest{
|
||||
|
||||
@Test
|
||||
public void testFindChar(){
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
|
||||
CharLocation returnedLocation = cipher.findChar('E');
|
||||
|
||||
@@ -710,7 +706,7 @@ public class PlayfairTest{
|
||||
|
||||
@Test
|
||||
public void testFindChar_invalid(){
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.findChar('J');
|
||||
@@ -722,61 +718,61 @@ public class PlayfairTest{
|
||||
|
||||
@Test
|
||||
public void testGetGridChar(){
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
|
||||
char output = cipher.getGridChar(1, 1);
|
||||
|
||||
assertEquals(grid[1][1], output);
|
||||
assertEquals(GRID[1][1], output);
|
||||
verify(logger, times(1)).debug("Getting character from grid[{}][{}]", 1, 1);
|
||||
verify(logger, times(1)).debug("Character {}", grid[1][1]);
|
||||
verify(logger, times(1)).debug("Character {}", GRID[1][1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetGridChar_large(){
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
|
||||
char output = cipher.getGridChar(-4, -4);
|
||||
|
||||
assertEquals(grid[1][1], output);
|
||||
assertEquals(GRID[1][1], output);
|
||||
verify(logger, times(1)).debug("Getting character from grid[{}][{}]", -4, -4);
|
||||
verify(logger, times(1)).debug("Character {}", grid[1][1]);
|
||||
verify(logger, times(1)).debug("Character {}", GRID[1][1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetGridChar_negative(){
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
|
||||
char output = cipher.getGridChar(6, 6);
|
||||
|
||||
assertEquals(grid[1][1], output);
|
||||
assertEquals(GRID[1][1], output);
|
||||
verify(logger, times(1)).debug("Getting character from grid[{}][{}]", 6, 6);
|
||||
verify(logger, times(1)).debug("Character {}", grid[1][1]);
|
||||
verify(logger, times(1)).debug("Character {}", GRID[1][1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddCharactersToCleanString(){
|
||||
cipher.inputString = decodedStringPadded;
|
||||
cipher.inputString = DECODED_STRING_PADDED;
|
||||
|
||||
cipher.addCharactersToCleanString(encodedStringClean);
|
||||
cipher.addCharactersToCleanString(ENCODED_STRING_CLEAN);
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output string");
|
||||
verify(logger, times(34)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Appending uppercase");
|
||||
verify(logger, times(25)).debug("Appending lowercase");
|
||||
verify(logger, times(8)).debug("Appending symbol");
|
||||
verify(logger, times(1)).debug("Formatted output '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Formatted output '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.inputString = decodedStringPadded;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = DECODED_STRING_PADDED;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = GRID;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(13)).debug(eq("Letters {} {}"), anyChar(), anyChar());
|
||||
verify(logger, times(2)).debug("Row encoding");
|
||||
@@ -787,13 +783,13 @@ public class PlayfairTest{
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.inputString = encodedString;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = GRID;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(13)).debug(eq("Letters {} {}"), anyChar(), anyChar());
|
||||
verify(logger, times(2)).debug("Row decoding");
|
||||
@@ -804,10 +800,10 @@ public class PlayfairTest{
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.grid = GRID;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -820,14 +816,14 @@ public class PlayfairTest{
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.grid = GRID;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(keyword, cipher.getKeyword());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
assertEquals(KEYWORD, cipher.getKeyword());
|
||||
assertEquals("[P L A Y F]\n[I R E X M]\n[B C D G H]\n[K N O Q S]\n[T U V W Z]", cipher.getGrid());
|
||||
assertEquals('J', cipher.getReplaced());
|
||||
assertEquals('I', cipher.getReplacer());
|
||||
@@ -839,51 +835,51 @@ public class PlayfairTest{
|
||||
public void testPracticalEncode(){
|
||||
cipher = new Playfair(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringPadded, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncode_clean(){
|
||||
cipher = new Playfair(false, false, false);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecode(){
|
||||
cipher = new Playfair(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedStringPadded, cipher.outputString);
|
||||
assertEquals(decodedStringPadded, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_PADDED, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_PADDED, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecode_clean(){
|
||||
cipher = new Playfair(false, false, false);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/PolybiusSquareTest.java
|
||||
//Mattrixwv
|
||||
// Created: 01-04-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -29,20 +25,20 @@ public class PolybiusSquareTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Fields
|
||||
private static final String decodedString = "Message to^encode";
|
||||
private static final String decodedStringClean = "M E S S A G E T O E N C O D E";
|
||||
private static final String encodedString = "41124545233212 5115^124225152212";
|
||||
private static final String encodedStringClean = "41 12 45 45 23 32 12 51 15 12 42 25 15 22 12";
|
||||
private static final String keyword = "ke yw*ord";
|
||||
private static final String keywordClean = "KEYWORDABCFGHILMNPQSTUVXZ";
|
||||
private static final char[][] grid = new char[][]{
|
||||
private static final String DECODED_STRING = "Message to^encode";
|
||||
private static final String DECODED_STRING_CLEAN = "M E S S A G E T O E N C O D E";
|
||||
private static final String ENCODED_STRING = "41124545233212 5115^124225152212";
|
||||
private static final String ENCODED_STRING_CLEAN = "41 12 45 45 23 32 12 51 15 12 42 25 15 22 12";
|
||||
private static final String KEYWORD = "ke yw*ord";
|
||||
private static final String KEYWORD_CLEAN = "KEYWORDABCFGHILMNPQSTUVXZ";
|
||||
private static final char[][] GRID = new char[][]{
|
||||
{'K', 'E', 'Y', 'W', 'O'},
|
||||
{'R', 'D', 'A', 'B', 'C'},
|
||||
{'F', 'G', 'H', 'I', 'L'},
|
||||
{'M', 'N', 'P', 'Q', 'S'},
|
||||
{'T', 'U', 'V', 'X', 'Z'}
|
||||
};
|
||||
private static final String gridString = "[K E Y W O]\n[R D A B C]\n[F G H I L]\n[M N P Q S]\n[T U V X Z]";
|
||||
private static final String GRID_STRING = "[K E Y W O]\n[R D A B C]\n[F G H I L]\n[M N P Q S]\n[T U V X Z]";
|
||||
|
||||
|
||||
@Test
|
||||
@@ -121,7 +117,7 @@ public class PolybiusSquareTest{
|
||||
|
||||
assertEquals('A', cipher.replaced);
|
||||
verify(logger, times(1)).debug("Setting replaced");
|
||||
verify(logger, times(1)).debug("Original character {}", 'a');
|
||||
verify(logger, times(1)).debug("Validating replaced character {}", 'a');
|
||||
verify(logger, times(1)).debug("Checking replacer");
|
||||
verify(logger, times(1)).debug("Cleaned character {}", 'A');
|
||||
}
|
||||
@@ -134,7 +130,7 @@ public class PolybiusSquareTest{
|
||||
|
||||
assertEquals('J', cipher.replaced);
|
||||
verify(logger, times(1)).debug("Setting replaced");
|
||||
verify(logger, times(1)).debug("Original character {}", '1');
|
||||
verify(logger, times(1)).debug("Validating replaced character {}", '1');
|
||||
verify(logger, never()).debug("Checking replacer");
|
||||
verify(logger, never()).debug(eq("Cleaned character {}"), anyChar());
|
||||
}
|
||||
@@ -147,7 +143,7 @@ public class PolybiusSquareTest{
|
||||
|
||||
assertEquals('J', cipher.replaced);
|
||||
verify(logger, times(1)).debug("Setting replaced");
|
||||
verify(logger, times(1)).debug("Original character {}", cipher.replacer);
|
||||
verify(logger, times(1)).debug("Validating replaced character {}", cipher.replacer);
|
||||
verify(logger, times(1)).debug("Checking replacer");
|
||||
verify(logger, never()).debug(eq("Cleaned character {}"), anyChar());
|
||||
}
|
||||
@@ -158,7 +154,7 @@ public class PolybiusSquareTest{
|
||||
|
||||
assertEquals('A', cipher.replacer);
|
||||
verify(logger, times(1)).debug("Setting replacer");
|
||||
verify(logger, times(1)).debug("Original character {}", 'a');
|
||||
verify(logger, times(1)).debug("Validating replacer character {}", 'a');
|
||||
verify(logger, times(1)).debug("Checking replaced");
|
||||
verify(logger, times(1)).debug("Cleaned character {}", 'A');
|
||||
}
|
||||
@@ -171,7 +167,7 @@ public class PolybiusSquareTest{
|
||||
|
||||
assertEquals('I', cipher.replacer);
|
||||
verify(logger, times(1)).debug("Setting replacer");
|
||||
verify(logger, times(1)).debug("Original character {}", '1');
|
||||
verify(logger, times(1)).debug("Validating replacer character {}", '1');
|
||||
verify(logger, never()).debug("Checking replaced");
|
||||
verify(logger, never()).debug(eq("Cleaned character {}"), anyChar());
|
||||
}
|
||||
@@ -184,20 +180,20 @@ public class PolybiusSquareTest{
|
||||
|
||||
assertEquals('I', cipher.replacer);
|
||||
verify(logger, times(1)).debug("Setting replacer");
|
||||
verify(logger, times(1)).debug("Original character {}", cipher.replaced);
|
||||
verify(logger, times(1)).debug("Validating replacer character {}", cipher.replaced);
|
||||
verify(logger, times(1)).debug("Checking replaced");
|
||||
verify(logger, never()).debug(eq("Cleaned character {}"), anyChar());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateGrid(){
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
|
||||
cipher.createGrid();
|
||||
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
verify(logger, times(1)).debug("Creating grid from keyword");
|
||||
verify(logger, times(1)).debug("Created grid\n{}", gridString);
|
||||
verify(logger, times(1)).debug("Created grid\n{}", GRID_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -207,16 +203,16 @@ public class PolybiusSquareTest{
|
||||
cipher.replaced = 'J';
|
||||
cipher.replacer = 'I';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Checking for digits");
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -226,16 +222,16 @@ public class PolybiusSquareTest{
|
||||
cipher.replaced = 'J';
|
||||
cipher.replacer = 'I';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase().replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Checking for digits");
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase().replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase().replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -245,16 +241,16 @@ public class PolybiusSquareTest{
|
||||
cipher.replaced = 'J';
|
||||
cipher.replacer = 'I';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase().replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase().replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Checking for digits");
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase().replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase().replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -264,16 +260,16 @@ public class PolybiusSquareTest{
|
||||
cipher.replaced = 'J';
|
||||
cipher.replacer = 'I';
|
||||
|
||||
cipher.setInputStringEncode(decodedString);
|
||||
cipher.setInputStringEncode(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for encoding '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Checking for digits");
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedStringClean);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -365,14 +361,14 @@ public class PolybiusSquareTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", ENCODED_STRING);
|
||||
verify(logger, times(1)).debug("Checking for letters");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -380,14 +376,14 @@ public class PolybiusSquareTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString);
|
||||
assertEquals(ENCODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", ENCODED_STRING);
|
||||
verify(logger, times(1)).debug("Checking for letters");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -395,14 +391,14 @@ public class PolybiusSquareTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputStringDecode(encodedString);
|
||||
cipher.setInputStringDecode(ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString.replaceAll("[^0-9\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString);
|
||||
assertEquals(ENCODED_STRING.replaceAll("[^0-9\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", ENCODED_STRING);
|
||||
verify(logger, times(1)).debug("Checking for letters");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", encodedString.replaceAll("[^0-9\\s]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", ENCODED_STRING.replaceAll("[^0-9\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -411,11 +407,11 @@ public class PolybiusSquareTest{
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputStringDecode(encodedString + "a");
|
||||
cipher.setInputStringDecode(ENCODED_STRING + "a");
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString + "a");
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", ENCODED_STRING + "a");
|
||||
verify(logger, times(1)).debug("Checking for letters");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
@@ -428,11 +424,11 @@ public class PolybiusSquareTest{
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputStringDecode(encodedString + "0");
|
||||
cipher.setInputStringDecode(ENCODED_STRING + "0");
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", encodedString + "0");
|
||||
verify(logger, times(1)).debug("Setting input string for decoding '{}'", ENCODED_STRING + "0");
|
||||
verify(logger, times(1)).debug("Checking for letters");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
@@ -492,38 +488,38 @@ public class PolybiusSquareTest{
|
||||
|
||||
@Test
|
||||
public void testGetPreparedInputStringEncode(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
String output = cipher.getPreparedInputStringEncode();
|
||||
|
||||
assertEquals(decodedStringClean.replaceAll("\\s", ""), output);
|
||||
assertEquals(DECODED_STRING_CLEAN.replaceAll("\\s", ""), output);
|
||||
verify(logger, times(1)).debug("Preparing input string for encoding");
|
||||
verify(logger, times(1)).debug("Prepared string '{}'", decodedStringClean.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Prepared string '{}'", DECODED_STRING_CLEAN.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPreparedInputStringDecode(){
|
||||
cipher.inputString = encodedString;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
|
||||
String output = cipher.getPreparedInputStringDecode();
|
||||
|
||||
assertEquals(encodedStringClean.replaceAll("\\s", ""), output);
|
||||
assertEquals(ENCODED_STRING_CLEAN.replaceAll("\\s", ""), output);
|
||||
verify(logger, times(1)).debug("Preparing input string for decoding");
|
||||
verify(logger, times(1)).debug("Prepared string '{}'", encodedStringClean.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Prepared string '{}'", ENCODED_STRING_CLEAN.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
verify(logger, times(1)).debug("Original keyword {}", keyword);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
verify(logger, times(1)).debug("Original keyword {}", KEYWORD);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing all non-letter characters");
|
||||
verify(logger, times(1)).debug("Appending entire alphabet");
|
||||
verify(logger, times(1)).debug("Replacing {} with {}", cipher.replaced, cipher.replacer);
|
||||
verify(logger, times(1)).debug("Cleaned keyword {}", keywordClean);
|
||||
verify(logger, times(1)).debug("Cleaned keyword {}", KEYWORD_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -566,7 +562,7 @@ public class PolybiusSquareTest{
|
||||
|
||||
@Test
|
||||
public void testFindChar(){
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
|
||||
CharLocation returnedLocation = cipher.findChar('A');
|
||||
|
||||
@@ -578,7 +574,7 @@ public class PolybiusSquareTest{
|
||||
|
||||
@Test
|
||||
public void testFindChar_invalid(){
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.findChar(cipher.replaced);
|
||||
@@ -590,41 +586,41 @@ public class PolybiusSquareTest{
|
||||
|
||||
@Test
|
||||
public void testAddCharactersToCleanStringEncode(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
cipher.addCharactersToCleanStringEncode(encodedStringClean.replaceAll("\\D", ""));
|
||||
cipher.addCharactersToCleanStringEncode(ENCODED_STRING_CLEAN.replaceAll("\\D", ""));
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output string for encoding");
|
||||
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(15)).debug("Adding encoded characters");
|
||||
verify(logger, times(2)).debug("Adding symbols");
|
||||
verify(logger, times(1)).debug("Formatted output '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Formatted output '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddCharactersToCleanStringDecode(){
|
||||
cipher.inputString = encodedString;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
|
||||
cipher.addCharactersToCleanStringDecode(decodedStringClean.replaceAll("\\s", ""));
|
||||
cipher.addCharactersToCleanStringDecode(DECODED_STRING_CLEAN.replaceAll("\\s", ""));
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.outputString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output string for decoding");
|
||||
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(15)).debug("Adding decoded characters");
|
||||
verify(logger, times(2)).debug("Adding symbols");
|
||||
verify(logger, times(1)).debug("Formatted output '{}'", decodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Formatted output '{}'", DECODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.inputString = decodedString.toUpperCase();
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = DECODED_STRING.toUpperCase();
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = GRID;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(15)).debug(eq("Current working character {}"), anyChar());
|
||||
verify(logger, times(15)).debug(eq("Location {}, {}"), anyInt(), anyInt());
|
||||
@@ -632,13 +628,13 @@ public class PolybiusSquareTest{
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.inputString = encodedString;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = GRID;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.outputString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(15)).debug(eq("Digits to decode {} {}"), anyChar(), anyChar());
|
||||
verify(logger, times(15)).debug(eq("Decoded letter {}"), anyChar());
|
||||
@@ -646,14 +642,14 @@ public class PolybiusSquareTest{
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.grid = GRID;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(keywordClean, cipher.getKeyword());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(KEYWORD_CLEAN, cipher.getKeyword());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
assertEquals("[K E Y W O]\n[R D A B C]\n[F G H I L]\n[M N P Q S]\n[T U V X Z]", cipher.getGrid());
|
||||
assertEquals('J', cipher.getReplaced());
|
||||
assertEquals('I', cipher.getReplacer());
|
||||
@@ -661,10 +657,10 @@ public class PolybiusSquareTest{
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.grid = GRID;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -680,51 +676,51 @@ public class PolybiusSquareTest{
|
||||
public void testPracticalEncode(){
|
||||
cipher = new PolybiusSquare(true, true);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncode_clean(){
|
||||
cipher = new PolybiusSquare(false, false);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecode(){
|
||||
cipher = new PolybiusSquare(true, true);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedString.toUpperCase(), cipher.outputString);
|
||||
assertEquals(decodedString.toUpperCase(), output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.outputString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecode_clean(){
|
||||
cipher = new PolybiusSquare(false, false);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString.replaceAll("\\s", "").replaceAll("[^0-9]", ""), cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedStringClean.replaceAll("\\s", ""), cipher.outputString);
|
||||
assertEquals(decodedStringClean.replaceAll("\\s", ""), output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(ENCODED_STRING.replaceAll("\\s", "").replaceAll("[^0-9]", ""), cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN.replaceAll("\\s", ""), cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN.replaceAll("\\s", ""), output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/polysubstitution/RailFenceTest.java
|
||||
//Mattrixwv
|
||||
// Created: 03-21-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -27,18 +23,18 @@ public class RailFenceTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Fields
|
||||
private static final String decodedString = "Message to^encode";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODE";
|
||||
private static final String encodedString3 = "Maooesg te^cdsene";
|
||||
private static final String encodedString3Clean = "MAOOESGTECDSENE";
|
||||
private static final String encodedString5 = "Moetese ne^sgcdao";
|
||||
private static final String encodedString5Clean = "MOETESENESGCDAO";
|
||||
private static final StringBuilder[] fence3 = new StringBuilder[]{
|
||||
private static final String DECODED_STRING = "Message to^encode";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODE";
|
||||
private static final String ENCODED_STRING_3 = "Maooesg te^cdsene";
|
||||
private static final String ENCODED_STRING_3_CLEAN = "MAOOESGTECDSENE";
|
||||
private static final String ENCODED_STRING_5 = "Moetese ne^sgcdao";
|
||||
private static final String ENCODED_STRING_5_CLEAN = "MOETESENESGCDAO";
|
||||
private static final StringBuilder[] FENCE_3 = new StringBuilder[]{
|
||||
new StringBuilder("Maoo"),
|
||||
new StringBuilder("esgtecd"),
|
||||
new StringBuilder("sene")
|
||||
};
|
||||
private static final StringBuilder[] fence5 = new StringBuilder[]{
|
||||
private static final StringBuilder[] FENCE_5 = new StringBuilder[]{
|
||||
new StringBuilder("Mo"),
|
||||
new StringBuilder("ete"),
|
||||
new StringBuilder("sene"),
|
||||
@@ -101,14 +97,14 @@ public class RailFenceTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -117,14 +113,14 @@ public class RailFenceTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", decodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", DECODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -133,14 +129,14 @@ public class RailFenceTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", decodedString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", DECODED_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -149,14 +145,14 @@ public class RailFenceTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
verify(logger, times(1)).debug("Clean input string '{}'", DECODED_STRING.replaceAll("[^a-zA-Z\\s]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -215,43 +211,43 @@ public class RailFenceTest{
|
||||
|
||||
@Test
|
||||
public void testGetCleanInputString(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
String output = cipher.getCleanInputString();
|
||||
|
||||
assertEquals(decodedString.replaceAll("[^a-zA-Z]", ""), output);
|
||||
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z]", ""), output);
|
||||
verify(logger, times(1)).debug("Getting input string for encoding");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatOutput(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
cipher.formatOutput(encodedString3Clean);
|
||||
cipher.formatOutput(ENCODED_STRING_3_CLEAN);
|
||||
|
||||
assertEquals(encodedString3, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_3, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output string");
|
||||
verify(logger, times(17)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Formatting uppercase");
|
||||
verify(logger, times(14)).debug("Formatting lowercase");
|
||||
verify(logger, times(2)).debug("Inserting symbol");
|
||||
verify(logger, times(1)).debug("Formatted output '{}'", encodedString3);
|
||||
verify(logger, times(1)).debug("Formatted output '{}'", ENCODED_STRING_3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDecodedStringFromFence(){
|
||||
cipher.fence = fence3;
|
||||
cipher.fence = FENCE_3;
|
||||
|
||||
String output = cipher.getDecodedStringFromFence();
|
||||
|
||||
assertEquals(decodedString.replaceAll("[^a-zA-Z]", ""), output);
|
||||
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z]", ""), output);
|
||||
verify(logger, times(1)).debug("Getting decoded string from the fence");
|
||||
verify(logger, times(1)).debug(eq("Fence output '{}'"), any(StringBuilder.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.fence = new StringBuilder[]{
|
||||
new StringBuilder(),
|
||||
new StringBuilder(),
|
||||
@@ -260,7 +256,7 @@ public class RailFenceTest{
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString3, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_3, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(15)).debug(eq("Working character '{}'"), anyChar());
|
||||
verify(logger, times(9)).debug("Moving up");
|
||||
@@ -272,7 +268,7 @@ public class RailFenceTest{
|
||||
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.inputString = encodedString3;
|
||||
cipher.inputString = ENCODED_STRING_3;
|
||||
cipher.fence = new StringBuilder[]{
|
||||
new StringBuilder(),
|
||||
new StringBuilder(),
|
||||
@@ -281,14 +277,14 @@ public class RailFenceTest{
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(1)).debug("Number of characters in the top rail {}", 4);
|
||||
verify(logger, times(1)).debug("Number of characters in the middle rails {}", 8);
|
||||
verify(logger, times(1)).debug("Number of characters in the bottom rail {}", 4);
|
||||
verify(logger, times(1)).debug("Adding characters to the rails");
|
||||
verify(logger, times(1)).debug("Appending the bottom rail");
|
||||
verify(logger, times(1)).debug("Fence output '{}'", decodedString.replaceAll("[^a-zA-Z]", ""));
|
||||
verify(logger, times(1)).debug("Fence output '{}'", DECODED_STRING.replaceAll("[^a-zA-Z]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -314,20 +310,20 @@ public class RailFenceTest{
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString3;
|
||||
cipher.fence = fence3;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING_3;
|
||||
cipher.fence = FENCE_3;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString3, cipher.getOutputString());
|
||||
assertEquals(fence3.length, cipher.getNumRails());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING_3, cipher.getOutputString());
|
||||
assertEquals(FENCE_3.length, cipher.getNumRails());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString3;
|
||||
cipher.fence = fence3;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING_3;
|
||||
cipher.fence = FENCE_3;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -340,127 +336,127 @@ public class RailFenceTest{
|
||||
public void testPracticalEncoding_3(){
|
||||
cipher = new RailFence(true, true, true);
|
||||
|
||||
String output = cipher.encode(3, decodedString);
|
||||
String output = cipher.encode(3, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(fence3.length, cipher.fence.length);
|
||||
assertEquals(fence3[0].toString(), cipher.fence[0].toString());
|
||||
assertEquals(fence3[1].toString(), cipher.fence[1].toString());
|
||||
assertEquals(fence3[2].toString(), cipher.fence[2].toString());
|
||||
assertEquals(encodedString3, cipher.outputString);
|
||||
assertEquals(encodedString3, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(FENCE_3.length, cipher.fence.length);
|
||||
assertEquals(FENCE_3[0].toString(), cipher.fence[0].toString());
|
||||
assertEquals(FENCE_3[1].toString(), cipher.fence[1].toString());
|
||||
assertEquals(FENCE_3[2].toString(), cipher.fence[2].toString());
|
||||
assertEquals(ENCODED_STRING_3, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_3, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_3Clean(){
|
||||
cipher = new RailFence(false, false, false);
|
||||
|
||||
String output = cipher.encode(3, decodedString);
|
||||
String output = cipher.encode(3, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(fence3.length, cipher.fence.length);
|
||||
assertEquals(fence3[0].toString().toUpperCase(), cipher.fence[0].toString());
|
||||
assertEquals(fence3[1].toString().toUpperCase(), cipher.fence[1].toString());
|
||||
assertEquals(fence3[2].toString().toUpperCase(), cipher.fence[2].toString());
|
||||
assertEquals(encodedString3Clean, cipher.outputString);
|
||||
assertEquals(encodedString3Clean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(FENCE_3.length, cipher.fence.length);
|
||||
assertEquals(FENCE_3[0].toString().toUpperCase(), cipher.fence[0].toString());
|
||||
assertEquals(FENCE_3[1].toString().toUpperCase(), cipher.fence[1].toString());
|
||||
assertEquals(FENCE_3[2].toString().toUpperCase(), cipher.fence[2].toString());
|
||||
assertEquals(ENCODED_STRING_3_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_3_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_5(){
|
||||
cipher = new RailFence(true, true, true);
|
||||
|
||||
String output = cipher.encode(5, decodedString);
|
||||
String output = cipher.encode(5, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(fence5.length, cipher.fence.length);
|
||||
assertEquals(fence5[0].toString(), cipher.fence[0].toString());
|
||||
assertEquals(fence5[1].toString(), cipher.fence[1].toString());
|
||||
assertEquals(fence5[2].toString(), cipher.fence[2].toString());
|
||||
assertEquals(fence5[3].toString(), cipher.fence[3].toString());
|
||||
assertEquals(fence5[4].toString(), cipher.fence[4].toString());
|
||||
assertEquals(encodedString5, cipher.outputString);
|
||||
assertEquals(encodedString5, output);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(FENCE_5.length, cipher.fence.length);
|
||||
assertEquals(FENCE_5[0].toString(), cipher.fence[0].toString());
|
||||
assertEquals(FENCE_5[1].toString(), cipher.fence[1].toString());
|
||||
assertEquals(FENCE_5[2].toString(), cipher.fence[2].toString());
|
||||
assertEquals(FENCE_5[3].toString(), cipher.fence[3].toString());
|
||||
assertEquals(FENCE_5[4].toString(), cipher.fence[4].toString());
|
||||
assertEquals(ENCODED_STRING_5, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_5, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_5Clean(){
|
||||
cipher = new RailFence(false, false, false);
|
||||
|
||||
String output = cipher.encode(5, decodedString);
|
||||
String output = cipher.encode(5, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(fence5.length, cipher.fence.length);
|
||||
assertEquals(fence5[0].toString().toUpperCase(), cipher.fence[0].toString());
|
||||
assertEquals(fence5[1].toString().toUpperCase(), cipher.fence[1].toString());
|
||||
assertEquals(fence5[2].toString().toUpperCase(), cipher.fence[2].toString());
|
||||
assertEquals(fence5[3].toString().toUpperCase(), cipher.fence[3].toString());
|
||||
assertEquals(fence5[4].toString().toUpperCase(), cipher.fence[4].toString());
|
||||
assertEquals(encodedString5Clean, cipher.outputString);
|
||||
assertEquals(encodedString5Clean, output);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(FENCE_5.length, cipher.fence.length);
|
||||
assertEquals(FENCE_5[0].toString().toUpperCase(), cipher.fence[0].toString());
|
||||
assertEquals(FENCE_5[1].toString().toUpperCase(), cipher.fence[1].toString());
|
||||
assertEquals(FENCE_5[2].toString().toUpperCase(), cipher.fence[2].toString());
|
||||
assertEquals(FENCE_5[3].toString().toUpperCase(), cipher.fence[3].toString());
|
||||
assertEquals(FENCE_5[4].toString().toUpperCase(), cipher.fence[4].toString());
|
||||
assertEquals(ENCODED_STRING_5_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_5_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_3(){
|
||||
cipher = new RailFence(true, true, true);
|
||||
|
||||
String output = cipher.decode(3, encodedString3);
|
||||
String output = cipher.decode(3, ENCODED_STRING_3);
|
||||
|
||||
assertEquals(encodedString3, cipher.inputString);
|
||||
assertEquals(fence3.length, cipher.fence.length);
|
||||
assertEquals(fence3[0].toString(), cipher.fence[0].toString());
|
||||
assertEquals(fence3[1].toString(), cipher.fence[1].toString());
|
||||
assertEquals(fence3[2].toString(), cipher.fence[2].toString());
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING_3, cipher.inputString);
|
||||
assertEquals(FENCE_3.length, cipher.fence.length);
|
||||
assertEquals(FENCE_3[0].toString(), cipher.fence[0].toString());
|
||||
assertEquals(FENCE_3[1].toString(), cipher.fence[1].toString());
|
||||
assertEquals(FENCE_3[2].toString(), cipher.fence[2].toString());
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_3Clean(){
|
||||
cipher = new RailFence(false, false, false);
|
||||
|
||||
String output = cipher.decode(3, encodedString3);
|
||||
String output = cipher.decode(3, ENCODED_STRING_3);
|
||||
|
||||
assertEquals(encodedString3Clean, cipher.inputString);
|
||||
assertEquals(fence3.length, cipher.fence.length);
|
||||
assertEquals(fence3[0].toString().toUpperCase(), cipher.fence[0].toString());
|
||||
assertEquals(fence3[1].toString().toUpperCase(), cipher.fence[1].toString());
|
||||
assertEquals(fence3[2].toString().toUpperCase(), cipher.fence[2].toString());
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_3_CLEAN, cipher.inputString);
|
||||
assertEquals(FENCE_3.length, cipher.fence.length);
|
||||
assertEquals(FENCE_3[0].toString().toUpperCase(), cipher.fence[0].toString());
|
||||
assertEquals(FENCE_3[1].toString().toUpperCase(), cipher.fence[1].toString());
|
||||
assertEquals(FENCE_3[2].toString().toUpperCase(), cipher.fence[2].toString());
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_5(){
|
||||
cipher = new RailFence(true, true, true);
|
||||
|
||||
String output = cipher.decode(5, encodedString5);
|
||||
String output = cipher.decode(5, ENCODED_STRING_5);
|
||||
|
||||
assertEquals(encodedString5, cipher.inputString);
|
||||
assertEquals(fence5.length, cipher.fence.length);
|
||||
assertEquals(fence5[0].toString(), cipher.fence[0].toString());
|
||||
assertEquals(fence5[1].toString(), cipher.fence[1].toString());
|
||||
assertEquals(fence5[2].toString(), cipher.fence[2].toString());
|
||||
assertEquals(fence5[3].toString(), cipher.fence[3].toString());
|
||||
assertEquals(fence5[4].toString(), cipher.fence[4].toString());
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertEquals(ENCODED_STRING_5, cipher.inputString);
|
||||
assertEquals(FENCE_5.length, cipher.fence.length);
|
||||
assertEquals(FENCE_5[0].toString(), cipher.fence[0].toString());
|
||||
assertEquals(FENCE_5[1].toString(), cipher.fence[1].toString());
|
||||
assertEquals(FENCE_5[2].toString(), cipher.fence[2].toString());
|
||||
assertEquals(FENCE_5[3].toString(), cipher.fence[3].toString());
|
||||
assertEquals(FENCE_5[4].toString(), cipher.fence[4].toString());
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_5Clean(){
|
||||
cipher = new RailFence(false, false, false);
|
||||
|
||||
String output = cipher.decode(5, encodedString5);
|
||||
String output = cipher.decode(5, ENCODED_STRING_5);
|
||||
|
||||
assertEquals(encodedString5Clean, cipher.inputString);
|
||||
assertEquals(fence5.length, cipher.fence.length);
|
||||
assertEquals(fence5[0].toString().toUpperCase(), cipher.fence[0].toString());
|
||||
assertEquals(fence5[1].toString().toUpperCase(), cipher.fence[1].toString());
|
||||
assertEquals(fence5[2].toString().toUpperCase(), cipher.fence[2].toString());
|
||||
assertEquals(fence5[3].toString().toUpperCase(), cipher.fence[3].toString());
|
||||
assertEquals(fence5[4].toString().toUpperCase(), cipher.fence[4].toString());
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertEquals(ENCODED_STRING_5_CLEAN, cipher.inputString);
|
||||
assertEquals(FENCE_5.length, cipher.fence.length);
|
||||
assertEquals(FENCE_5[0].toString().toUpperCase(), cipher.fence[0].toString());
|
||||
assertEquals(FENCE_5[1].toString().toUpperCase(), cipher.fence[1].toString());
|
||||
assertEquals(FENCE_5[2].toString().toUpperCase(), cipher.fence[2].toString());
|
||||
assertEquals(FENCE_5[3].toString().toUpperCase(), cipher.fence[3].toString());
|
||||
assertEquals(FENCE_5[4].toString().toUpperCase(), cipher.fence[4].toString());
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/cipherstream/polysubstitution/TrifidTest.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 04-19-24
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -32,19 +28,19 @@ public class TrifidTest{
|
||||
@Mock
|
||||
private Logger logger;
|
||||
//Fields
|
||||
private static final String decodedString = "Message to^encode+";
|
||||
private static final String decodedStringClean = "MESSAGETOENCODE+";
|
||||
private static final String decodedStringCleanAlt = "MESSAGETOENCODE";
|
||||
private static final String encodedString = "Gqdokxy eg^ranmoqr";
|
||||
private static final String encodedStringAlt = "Gqdokpd od^ljvflf+";
|
||||
private static final String encodedString3 = "Gpjqdvd of^odlklf+";
|
||||
private static final String encodedStringClean = "GQDOKXYEGRANMOQR";
|
||||
private static final String encodedStringCleanAlt = "GQDOKPDODLJVFLF";
|
||||
private static final String encodedStringClean3 = "GPJQDVDOFODLKLF+";
|
||||
private static final String keyword = "ke yw*ord";
|
||||
private static final String keywordClean = "KEYWORDABCFGHIJLMNPQSTUVXZ+";
|
||||
private static final String keywordCleanAlt = "KEYWORDABCFGHIJLMNPQSTUVXZ=";
|
||||
private static final char[][][] grid = new char[][][]{
|
||||
private static final String DECODED_STRING = "Message to^encode+";
|
||||
private static final String DECODED_STRING_CLEAN = "MESSAGETOENCODE+";
|
||||
private static final String DECODED_STRING_CLEAN_ALT = "MESSAGETOENCODE";
|
||||
private static final String ENCODED_STRING = "Gqdokxy eg^ranmoqr";
|
||||
private static final String ENCODED_STRING_ALT = "Gqdokpd od^ljvflf+";
|
||||
private static final String ENCODED_STRING_3 = "Gpjqdvd of^odlklf+";
|
||||
private static final String ENCODED_STRING_CLEAN = "GQDOKXYEGRANMOQR";
|
||||
private static final String ENCODED_STRING_CLEAN_ALT = "GQDOKPDODLJVFLF";
|
||||
private static final String ENCODED_STRING_CLEAN_3 = "GPJQDVDOFODLKLF+";
|
||||
private static final String KEYWORD = "ke yw*ord";
|
||||
private static final String KEYWORD_CLEAN = "KEYWORDABCFGHIJLMNPQSTUVXZ+";
|
||||
private static final String KEYWORD_CLEAN_ALT = "KEYWORDABCFGHIJLMNPQSTUVXZ=";
|
||||
private static final char[][][] GRID = new char[][][]{
|
||||
{
|
||||
{'K', 'E', 'Y'},
|
||||
{'W', 'O', 'R'},
|
||||
@@ -61,8 +57,8 @@ public class TrifidTest{
|
||||
{'X', 'Z', '+'}
|
||||
}
|
||||
};
|
||||
private static final String gridString = "[K E Y]\n[W O R]\n[D A B]\n\n[C F G]\n[H I J]\n[L M N]\n\n[P Q S]\n[T U V]\n[X Z +]";
|
||||
private static final char[][][] gridAlt = new char[][][]{
|
||||
private static final String GRID_STRING = "[K E Y]\n[W O R]\n[D A B]\n\n[C F G]\n[H I J]\n[L M N]\n\n[P Q S]\n[T U V]\n[X Z +]";
|
||||
private static final char[][][] GRID_ALT = new char[][][]{
|
||||
{
|
||||
{'K', 'E', 'Y'},
|
||||
{'W', 'O', 'R'},
|
||||
@@ -226,16 +222,16 @@ public class TrifidTest{
|
||||
|
||||
@Test
|
||||
public void testSetKeyword(){
|
||||
cipher.setKeyword(keyword);
|
||||
cipher.setKeyword(KEYWORD);
|
||||
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
verify(logger, times(1)).debug("Original keyword {}", keyword);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
verify(logger, times(1)).debug("Original keyword {}", KEYWORD);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing all invalid characters");
|
||||
verify(logger, times(1)).debug("Appending entire alphabet to keyword");
|
||||
verify(logger, times(1)).debug("Removing duplicate characters");
|
||||
verify(logger, times(1)).debug("Cleaned keyword {}", keywordClean);
|
||||
verify(logger, times(1)).debug("Cleaned keyword {}", KEYWORD_CLEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -287,13 +283,13 @@ public class TrifidTest{
|
||||
|
||||
@Test
|
||||
public void setCreateGrid(){
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
|
||||
cipher.createGrid();
|
||||
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
verify(logger, times(1)).debug("Creating grid from keyword");
|
||||
verify(logger, times(1)).debug("Completed grid\n{}", gridString);
|
||||
verify(logger, times(1)).debug("Completed grid\n{}", GRID_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -320,14 +316,14 @@ public class TrifidTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -336,14 +332,14 @@ public class TrifidTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.toUpperCase(), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, times(1)).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -352,14 +348,14 @@ public class TrifidTest{
|
||||
cipher.preserveWhitespace = false;
|
||||
cipher.preserveSymbols = true;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("\\s", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, times(1)).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("\\s", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("\\s", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -370,11 +366,11 @@ public class TrifidTest{
|
||||
cipher.fillIn = ' ';
|
||||
|
||||
assertThrows(InvalidInputException.class, () -> {
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
});
|
||||
|
||||
assertEquals("", cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, never()).debug("Removing symbols");
|
||||
@@ -387,14 +383,14 @@ public class TrifidTest{
|
||||
cipher.preserveWhitespace = true;
|
||||
cipher.preserveSymbols = false;
|
||||
|
||||
cipher.setInputString(decodedString);
|
||||
cipher.setInputString(DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString.replaceAll("[^a-zA-Z\\s+]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", decodedString);
|
||||
assertEquals(DECODED_STRING.replaceAll("[^a-zA-Z\\s+]", ""), cipher.inputString);
|
||||
verify(logger, times(1)).debug("Original input string '{}'", DECODED_STRING);
|
||||
verify(logger, never()).debug("Removing case");
|
||||
verify(logger, never()).debug("Removing whitespace");
|
||||
verify(logger, times(1)).debug("Removing symbols");
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", decodedString.replaceAll("[^a-zA-Z\\s+]", ""));
|
||||
verify(logger, times(1)).debug("Cleaned input string '{}'", DECODED_STRING.replaceAll("[^a-zA-Z\\s+]", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -435,17 +431,17 @@ public class TrifidTest{
|
||||
|
||||
@Test
|
||||
public void testGetCleanInputString(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
String returnedInput = cipher.getCleanInputString();
|
||||
|
||||
assertEquals(decodedStringClean, returnedInput);
|
||||
assertEquals(DECODED_STRING_CLEAN, returnedInput);
|
||||
verify(logger, times(1)).debug("Cleaning input string for encoding");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindChar(){
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
|
||||
CharLocation returnedLocation = cipher.findChar('G');
|
||||
|
||||
@@ -456,7 +452,7 @@ public class TrifidTest{
|
||||
|
||||
@Test
|
||||
public void testFindChar_invalid(){
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
|
||||
assertThrows(InvalidCharacterException.class, () -> {
|
||||
cipher.findChar('=');
|
||||
@@ -468,7 +464,7 @@ public class TrifidTest{
|
||||
|
||||
@Test
|
||||
public void testGetChar(){
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
|
||||
char returnedChar = cipher.getChar(cipher.new CharLocation(0, 1, 2));
|
||||
|
||||
@@ -483,7 +479,7 @@ public class TrifidTest{
|
||||
"0, 1, 3"
|
||||
})
|
||||
public void testGetChar_invalidLocation(int x, int y, int z){
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
CharLocation location = cipher.new CharLocation(x, y, z);
|
||||
|
||||
assertThrows(InvalidCharacterException.class, () -> {
|
||||
@@ -496,47 +492,47 @@ public class TrifidTest{
|
||||
@Test
|
||||
public void testFormatOutput(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.inputString = decodedString;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
|
||||
cipher.formatOutput(encodedStringClean);
|
||||
cipher.formatOutput(ENCODED_STRING_CLEAN);
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output");
|
||||
verify(logger, times(18)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(1)).debug("Formatting uppercase");
|
||||
verify(logger, times(14)).debug("Formatting lowercase");
|
||||
verify(logger, times(1)).debug("Adding fillIn");
|
||||
verify(logger, times(2)).debug("Appending symbol");
|
||||
verify(logger, times(1)).debug("Formatted output '{}'", encodedString);
|
||||
verify(logger, times(1)).debug("Formatted output '{}'", ENCODED_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatOutput_noCapitals(){
|
||||
cipher.preserveCapitals = false;
|
||||
cipher.inputString = decodedString.toUpperCase();
|
||||
cipher.inputString = DECODED_STRING.toUpperCase();
|
||||
|
||||
cipher.formatOutput(encodedStringClean);
|
||||
cipher.formatOutput(ENCODED_STRING_CLEAN);
|
||||
|
||||
assertEquals(encodedString.toUpperCase(), cipher.outputString);
|
||||
assertEquals(ENCODED_STRING.toUpperCase(), cipher.outputString);
|
||||
verify(logger, times(1)).debug("Formatting output");
|
||||
verify(logger, times(18)).debug(eq("Working character {}"), anyChar());
|
||||
verify(logger, times(15)).debug("Formatting uppercase");
|
||||
verify(logger, never()).debug("Formatting lowercase");
|
||||
verify(logger, times(1)).debug("Adding fillIn");
|
||||
verify(logger, times(2)).debug("Appending symbol");
|
||||
verify(logger, times(1)).debug("Formatted output '{}'", encodedString.toUpperCase());
|
||||
verify(logger, times(1)).debug("Formatted output '{}'", ENCODED_STRING.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncode(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.inputString = decodedString;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = GRID;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(1)).debug("Converting letters to coordinates");
|
||||
verify(logger, times(1)).debug("Splitting locations into groups");
|
||||
@@ -548,14 +544,14 @@ public class TrifidTest{
|
||||
@Test
|
||||
public void testEncode_3(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.inputString = decodedString;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = GRID;
|
||||
cipher.groupSize = 3;
|
||||
|
||||
cipher.encode();
|
||||
|
||||
assertEquals(encodedString3, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_3, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Encoding");
|
||||
verify(logger, times(1)).debug("Converting letters to coordinates");
|
||||
verify(logger, times(1)).debug("Splitting locations into groups");
|
||||
@@ -567,13 +563,13 @@ public class TrifidTest{
|
||||
@Test
|
||||
public void testDecode(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.inputString = encodedString;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = GRID;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(1)).debug("Converting letters to coordinates");
|
||||
verify(logger, times(1)).debug("Splitting locations into groups");
|
||||
@@ -585,14 +581,14 @@ public class TrifidTest{
|
||||
@Test
|
||||
public void testDecode_3(){
|
||||
cipher.preserveCapitals = true;
|
||||
cipher.inputString = encodedString3;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.grid = grid;
|
||||
cipher.inputString = ENCODED_STRING_3;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.grid = GRID;
|
||||
cipher.groupSize = 3;
|
||||
|
||||
cipher.decode();
|
||||
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
verify(logger, times(1)).debug("Decoding");
|
||||
verify(logger, times(1)).debug("Converting letters to coordinates");
|
||||
verify(logger, times(1)).debug("Splitting locations into groups");
|
||||
@@ -603,28 +599,28 @@ public class TrifidTest{
|
||||
|
||||
@Test
|
||||
public void testGetters(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keywordClean;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD_CLEAN;
|
||||
cipher.groupSize = 3;
|
||||
cipher.fillIn = '=';
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
|
||||
assertEquals(decodedString, cipher.getInputString());
|
||||
assertEquals(encodedString, cipher.getOutputString());
|
||||
assertEquals(keywordClean, cipher.getKeyword());
|
||||
assertEquals(DECODED_STRING, cipher.getInputString());
|
||||
assertEquals(ENCODED_STRING, cipher.getOutputString());
|
||||
assertEquals(KEYWORD_CLEAN, cipher.getKeyword());
|
||||
assertEquals(3, cipher.getGroupSize());
|
||||
assertEquals('=', cipher.getFillIn());
|
||||
assertEquals(gridString, cipher.getGrid());
|
||||
assertEquals(GRID_STRING, cipher.getGrid());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset(){
|
||||
cipher.inputString = decodedString;
|
||||
cipher.outputString = encodedString;
|
||||
cipher.keyword = keyword;
|
||||
cipher.inputString = DECODED_STRING;
|
||||
cipher.outputString = ENCODED_STRING;
|
||||
cipher.keyword = KEYWORD;
|
||||
cipher.groupSize = 3;
|
||||
cipher.grid = grid;
|
||||
cipher.grid = GRID;
|
||||
|
||||
cipher.reset();
|
||||
|
||||
@@ -639,155 +635,155 @@ public class TrifidTest{
|
||||
public void testPracticalEncoding(){
|
||||
cipher = new Trifid(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedString, cipher.outputString);
|
||||
assertEquals(encodedString, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_3(){
|
||||
cipher = new Trifid(true, true, true);
|
||||
|
||||
String output = cipher.encode(keyword, 3, decodedString);
|
||||
String output = cipher.encode(KEYWORD, 3, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedString3, cipher.outputString);
|
||||
assertEquals(encodedString3, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_3, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_3, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_fill(){
|
||||
cipher = new Trifid(true, true, true, '=');
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedString, cipher.inputString);
|
||||
assertEquals(keywordCleanAlt, cipher.keyword);
|
||||
assertEquals(encodedStringAlt, cipher.outputString);
|
||||
assertEquals(encodedStringAlt, output);
|
||||
assertArrayEquals(gridAlt, cipher.grid);
|
||||
assertEquals(DECODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN_ALT, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_ALT, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_ALT, output);
|
||||
assertArrayEquals(GRID_ALT, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean(){
|
||||
cipher = new Trifid(false, false, false);
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedStringClean, cipher.outputString);
|
||||
assertEquals(encodedStringClean, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_clean3(){
|
||||
cipher = new Trifid(false, false, false);
|
||||
|
||||
String output = cipher.encode(keyword, 3, decodedString);
|
||||
String output = cipher.encode(KEYWORD, 3, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(encodedStringClean3, cipher.outputString);
|
||||
assertEquals(encodedStringClean3, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN_3, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN_3, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalEncoding_cleanFill(){
|
||||
cipher = new Trifid(false, false, false, '=');
|
||||
|
||||
String output = cipher.encode(keyword, decodedString);
|
||||
String output = cipher.encode(KEYWORD, DECODED_STRING);
|
||||
|
||||
assertEquals(decodedStringCleanAlt, cipher.inputString);
|
||||
assertEquals(keywordCleanAlt, cipher.keyword);
|
||||
assertEquals(encodedStringCleanAlt, cipher.outputString);
|
||||
assertEquals(encodedStringCleanAlt, output);
|
||||
assertArrayEquals(gridAlt, cipher.grid);
|
||||
assertEquals(DECODED_STRING_CLEAN_ALT, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN_ALT, cipher.keyword);
|
||||
assertEquals(ENCODED_STRING_CLEAN_ALT, cipher.outputString);
|
||||
assertEquals(ENCODED_STRING_CLEAN_ALT, output);
|
||||
assertArrayEquals(GRID_ALT, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding(){
|
||||
cipher = new Trifid(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedString, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(ENCODED_STRING, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_3(){
|
||||
cipher = new Trifid(true, true, true);
|
||||
|
||||
String output = cipher.decode(keyword, 3, encodedString3);
|
||||
String output = cipher.decode(KEYWORD, 3, ENCODED_STRING_3);
|
||||
|
||||
assertEquals(encodedString3, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(ENCODED_STRING_3, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_fill(){
|
||||
cipher = new Trifid(true, true, true, '=');
|
||||
|
||||
String output = cipher.decode(keyword, encodedStringAlt);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING_ALT);
|
||||
|
||||
assertEquals(encodedStringAlt, cipher.inputString);
|
||||
assertEquals(keywordCleanAlt, cipher.keyword);
|
||||
assertEquals(decodedString, cipher.outputString);
|
||||
assertEquals(decodedString, output);
|
||||
assertArrayEquals(gridAlt, cipher.grid);
|
||||
assertEquals(ENCODED_STRING_ALT, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN_ALT, cipher.keyword);
|
||||
assertEquals(DECODED_STRING, cipher.outputString);
|
||||
assertEquals(DECODED_STRING, output);
|
||||
assertArrayEquals(GRID_ALT, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testpracticalDecoding_clean(){
|
||||
cipher = new Trifid(false, false, false);
|
||||
|
||||
String output = cipher.decode(keyword, encodedString);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING);
|
||||
|
||||
assertEquals(encodedStringClean, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(ENCODED_STRING_CLEAN, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_clean3(){
|
||||
cipher = new Trifid(false, false, false);
|
||||
|
||||
String output = cipher.decode(keyword, 3, encodedString3);
|
||||
String output = cipher.decode(KEYWORD, 3, ENCODED_STRING_3);
|
||||
|
||||
assertEquals(encodedStringClean3, cipher.inputString);
|
||||
assertEquals(keywordClean, cipher.keyword);
|
||||
assertEquals(decodedStringClean, cipher.outputString);
|
||||
assertEquals(decodedStringClean, output);
|
||||
assertArrayEquals(grid, cipher.grid);
|
||||
assertEquals(ENCODED_STRING_CLEAN_3, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN, output);
|
||||
assertArrayEquals(GRID, cipher.grid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPracticalDecoding_cleanFill(){
|
||||
cipher = new Trifid(false, false, false, '=');
|
||||
|
||||
String output = cipher.decode(keyword, encodedStringAlt);
|
||||
String output = cipher.decode(KEYWORD, ENCODED_STRING_ALT);
|
||||
|
||||
assertEquals(encodedStringCleanAlt, cipher.inputString);
|
||||
assertEquals(keywordCleanAlt, cipher.keyword);
|
||||
assertEquals(decodedStringCleanAlt, cipher.outputString);
|
||||
assertEquals(decodedStringCleanAlt, output);
|
||||
assertArrayEquals(gridAlt, cipher.grid);
|
||||
assertEquals(ENCODED_STRING_CLEAN_ALT, cipher.inputString);
|
||||
assertEquals(KEYWORD_CLEAN_ALT, cipher.keyword);
|
||||
assertEquals(DECODED_STRING_CLEAN_ALT, cipher.outputString);
|
||||
assertEquals(DECODED_STRING_CLEAN_ALT, output);
|
||||
assertArrayEquals(GRID_ALT, cipher.grid);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user