Created
March 22, 2020 11:23
-
-
Save dondish/70d7e5a97f1b241ba0d9aed7c662d461 to your computer and use it in GitHub Desktop.
JetBrains Academy Encrypted-Decrypted Final
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package encryptdecrypt; | |
import java.io.*; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.Arrays; | |
import java.util.Scanner; | |
public class Configration { | |
private boolean dec; | |
private boolean unicode; | |
private int key; | |
private String data = ""; | |
private OutputStream os = System.out; | |
public static Configration fromArgs(String[] args) { | |
Configration c = new Configration(); | |
boolean value = false; | |
String keyz = ""; | |
for (String k : args) { | |
if (k.startsWith("-")) { | |
keyz = k.substring(1); | |
value = true; | |
} else if (value) { | |
switch (keyz) { | |
case "key": | |
c.key = Integer.parseInt(k); | |
break; | |
case "mode": | |
c.dec = k.equals("dec"); | |
break; | |
case "in": | |
if (c.data.equals("")) { | |
try { | |
c.data = Files.readString(Paths.get(k)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
break; | |
case "data": | |
c.data = k; | |
break; | |
case "out": | |
try { | |
c.os = new FileOutputStream(k); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
break; | |
case "alg": | |
c.unicode = k.equals("unicode"); | |
break; | |
default: | |
System.out.println(keyz); | |
} | |
value = false; | |
} | |
} | |
return c; | |
} | |
public boolean isDec() { | |
return dec; | |
} | |
public int getKey() { | |
return key; | |
} | |
public String getData() { | |
return data; | |
} | |
@Override | |
public String toString() { | |
return "Configration{" + | |
"dec=" + dec + | |
", key=" + key + | |
", data='" + data + '\'' + | |
'}'; | |
} | |
public OutputStream getOs() { | |
return os; | |
} | |
public boolean isUnicode() { | |
return unicode; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package encryptdecrypt; | |
import java.io.IOException; | |
import java.io.OutputStreamWriter; | |
import java.util.Scanner; | |
public class Main { | |
private static Scanner s = new Scanner(System.in); | |
private static String shifter(String s, int key) { | |
StringBuffer sb = new StringBuffer(); | |
for (char cha : s.toCharArray()) { | |
if (Character.isAlphabetic(cha) && Character.isLowerCase(cha)) { | |
int r = (cha-'a'+key)%26; | |
if (r < 0) r += 26; | |
sb.append((char)('a'+r)); | |
} else if (Character.isAlphabetic(cha)) { | |
int r = (cha-'A'+key)%26; | |
if (r < 0) r += 26; | |
sb.append((char) ('A' +r )); | |
} else | |
sb.append(cha); | |
} | |
return sb.toString(); | |
} | |
private static String unicoder(String s, int key) { | |
StringBuffer sb = new StringBuffer(); | |
for (char cha : s.toCharArray()) { | |
sb.append((char)(cha+key)); | |
} | |
return sb.toString(); | |
} | |
public static void main(String[] args) { | |
Configration c = Configration.fromArgs(args); | |
int key = c.getKey(); | |
if (c.isDec()) key *= -1; | |
OutputStreamWriter osw = new OutputStreamWriter(c.getOs()); | |
String s; | |
if (c.isUnicode()) { | |
s = unicoder(c.getData(), key); | |
} else { | |
s = shifter(c.getData(), key); | |
} | |
try { | |
osw.write(s); | |
osw.flush(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment