Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished lab #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Crypto/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {

public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream("sonnet18.txt");
String body = IOUtils.toString(in, StandardCharsets.UTF_8.name());
System.out.println(body);
PrintStream out = new PrintStream(new FileOutputStream("sonnet18.enc"));
ROT13 rot13 = new ROT13();
String output = rot13.encrypt(body);
out.append(output);
out.flush();
}
}
55 changes: 51 additions & 4 deletions Crypto/src/ROT13.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,77 @@
import static java.lang.Character.toLowerCase;

public class ROT13 {
private Character cs;
private Character cf;

ROT13(Character cs, Character cf) {
this.cs = cs;
this.cf = cf;
}

ROT13() {
this.cs = 'a';
this.cf = 'a' + 13;
}


public String crypt(String text) throws UnsupportedOperationException {
String crypted = "";
for(int i = 0; i<text.length(); i++){
char textChar = text.charAt(i);
int shiftDifferences = 13;
int differenceFromLastChar = 0;
if(textChar >= 'a' && textChar <= 'z'){
differenceFromLastChar = 'z'-textChar;
if(differenceFromLastChar > shiftDifferences){
textChar += shiftDifferences;
}
else
{
textChar = 'a';
textChar +=(shiftDifferences - differenceFromLastChar-1);
}
}
else if(textChar >= 'A'&& textChar <= 'Z'){
differenceFromLastChar = 'Z'-textChar;
if(differenceFromLastChar > shiftDifferences){
textChar += shiftDifferences;
}else {
textChar = 'A';
textChar +=(shiftDifferences-differenceFromLastChar-1);
}
}
crypted += textChar;
}

return "";
return crypted;
}

public String encrypt(String text) {
return text;
return crypt(text);
}

public String decrypt(String text) {
return text;
return crypt(text);
}

public static String rotate(String s, Character c) {
char lastChar = s.charAt(s.length()-1);
String rotated = "";
for(int i = 0; i< s.length();i++){
char textChar = s.charAt(i);
int shiftDifferences = c - 'A';
int differenceFromLastChar = lastChar - textChar;
if(differenceFromLastChar >= shiftDifferences){

return "";
textChar += shiftDifferences;
}else {
textChar = 'A';
textChar += (shiftDifferences -differenceFromLastChar-1);
}
rotated +=textChar;
}
return rotated;
}

}
16 changes: 16 additions & 0 deletions SimpleCrypt.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/Crypto/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.5" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.13.1" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>SimpleCrypt</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

</project>
14 changes: 14 additions & 0 deletions sonnet18.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Funyy V pb`cner gurr gb n fh``re’f qnl?
Gubh neg `ber ybiryl naq `ber gr`crengr:
Ebhtu jvaqf qb funxr gur qneyvat ohqf bs @nl,
Naq fh``re’f yrnfr ungu nyy gbb fubeg n qngr;
Fb`rgv`r gbb ubg gur rlr bs urnira fuvarf,
Naq bsgra vf uvf tbyq pb`cyrkvba qv``'q;
Naq rirel snve seb` snve fb`rgv`r qrpyvarf,
Ol punapr be angher’f punatvat pbhefr hagev``'q;
Ohg gul rgreany fh``re funyy abg snqr,
Abe ybfr cbffrffvba bs gung snve gubh bj’fg;
Abe funyy qrngu oent gubh jnaqre’fg va uvf funqr,
Jura va rgreany yvarf gb gv`r gubh tebj’fg:
Fb ybat nf `ra pna oerngur be rlrf pna frr,
Fb ybat yvirf guvf, naq guvf tvirf yvsr gb gurr.