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

Complete #2

Open
wants to merge 3 commits 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
57 changes: 49 additions & 8 deletions Crypto/src/ROT13.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,73 @@
package src
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import static java.lang.Character.isLowerCase;
import static java.lang.Character.isUpperCase;
import static java.lang.Character.toLowerCase;

public class ROT13 {

private int shift;
ROT13(Character cs, Character cf) {
this.shift = cs - cf;
}

ROT13() {
shift = 13;
}


public String crypt(String text) throws UnsupportedOperationException {

return "";
public static String crypt(String text) throws UnsupportedOperationException {
StringBuilder sb = new StringBuilder();
char [] chars = text.toCharArray();
for (int i = 0; i <chars.length ; i++) {
char c = text.charAt(i);
if(c >= 'a' && c <= 'm') {
c += 13;
}else if (c >= 'A' && c <= 'M') {
c += 13;
} else if (c >= 'n' && c <= 'z') {
c -= 13;
} else if (c >= 'N' && c <= 'Z') {
c -= 13;
}
sb.append(c);
}
return sb.toString();
}

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

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

public static String rotate(String s, Character c) {
//this method will rotate the given String
//by the character placement
String sub = s.substring(0,s.indexOf(c));
String sub1 = s.substring(s.indexOf(c));
//then concat the two substrings.
return sub1.concat(sub);
}
public static String textFile(String filename){
StringBuilder sb = new StringBuilder();

return "";
File file = new File(filename);
try(Scanner sc = new Scanner(file)) {
while(sc.hasNextLine()){
String line = sc.nextLine();
String xored = encrypt(line);
sb.append(xored+"\n");
}
sb.deleteCharAt(sb.length()-1);
} catch (FileNotFoundException e){
e.printStackTrace();
}
return sb.toString();
}

}
35 changes: 33 additions & 2 deletions Crypto/src/ROT13Test.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package src;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;

import static org.junit.Assert.*;

public class ROT13Test {
Expand Down Expand Up @@ -61,15 +65,19 @@ public void cryptTest1() {

// When
String actual = cipher.encrypt(Q1);
System.out.println(actual);

System.out.println(Q1);
System.out.println(A1);
// System.out.println(actual);
// Then
assertTrue(actual.equals(A1));

// When
String actual2 = cipher.decrypt(Q2);
System.out.println(Q2);
System.out.println(A2);
System.out.println(actual2);
// Then
assertTrue(actual2.equals(A2));
}
Expand All @@ -87,5 +95,28 @@ public void cryptTest2() {
// Then
assertTrue(actual.equals(Q1));
}

}
@Test
public void textEncryptFile(){
//Given
File file = new File("sonnet18.txt");
//When
String expected = ROT13.encrypt("Shall I compare thee to a summer’s day?\n" +
"Thou art more lovely and more temperate:\n" +
"Rough winds do shake the darling buds of May,\n" +
"And summer’s lease hath all too short a date;\n" +
"Sometime too hot the eye of heaven shines,\n" +
"And often is his gold complexion dimm'd;\n" +
"And every fair from fair sometime declines,\n" +
"By chance or nature’s changing course untrimm'd;\n" +
"But thy eternal summer shall not fade,\n" +
"Nor lose possession of that fair thou ow’st;\n" +
"Nor shall death brag thou wander’st in his shade,\n" +
"When in eternal lines to time thou grow’st:\n" +
"So long as men can breathe or eyes can see,\n" +
"So long lives this, and this gives life to thee.");

String actual = ROT13.textFile("sonnet18.txt");
//Then
Assert.assertEquals(expected,actual);
}
}
12 changes: 12 additions & 0 deletions SimpleCrypt.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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_5">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>