Rate my code - LetterHeron (#Java)
Ευχάριστο πολύ τον (TrevorC) για αυτόν τον κώδικα!
Code:
import java.util.Scanner;
import java.util.Base64;
public class ltr
{
public static void main(String[] args)
{
System.out.println("Choose encryption method");
System.out.println("");
System.out.println("Type 1 for Caesar Cipher");
System.out.println("");
System.out.println("Type 2 for Base64");
Scanner s = new Scanner(System.in);
int encMethod = s.nextInt();
switch(encMethod)
{
case 1:
ltr l = new ltr();
ltr.Caesar();
break;
case 2:
ltr lt = new ltr();
ltr.Base64();
break;
}
s.close();
}
public static void Caesar()
{
Scanner sc = new Scanner(System.in);
String msg;
String key;
int keyLength;
System.out.println("Enter message:");
msg = sc.nextLine();
System.out.println("Enter encryption key:");
key = sc.next();
keyLength= key.length();
System.out.println("1. Encrypt, 2. Decrypt, 3.Exit");
int option = sc.nextInt();
switch(option)
{
case 1:
System.out.println("Encrypted message..." +encrypt(msg,keyLength));
break;
case 2:
System.out.println("Decrypted message..." +decrypt(msg,keyLength));
break;
case 3:
System.exit(0);
break;
default:
System.out.println("Invalid input! Type 4 for help.");
break;
}
sc.close();
}
public static String encrypt(String msg,int keyLength)
{
String encrypted = "";
for(int i=0;i
{
int c = msg.charAt(i);
if(Character.isUpperCase(c))
{
c = c + (keyLength%26);
if(c > 'Z')
c = c - 26;
}
else if(Character.isLowerCase(c))
{
c = c + (keyLength%26);
if(c > 'z')
c = c - 26;
}
encrypted= encrypted + (char) c;
}
return encrypted;
}
public static String decrypt(String msg,int keyLength)
{
String decrypted = "";
for(int i=0;i
{
int c = msg.charAt(i);
if(Character.isUpperCase(c))
{
c = c -(keyLength%26);
if( c < 'A')
c = c + 26;
}
else if(Character.isLowerCase(c))
{
c = c - (keyLength%26);
if(c < 'a')
c = c + 26;
}
decrypted = decrypted + (char) c;
}
return decrypted;
}
public static void Base64()
{
int pick;
System.out.println("Enter your message, with no spaces.");
Scanner as = new Scanner(System.in);
String msg = as.next();
System.out.println("1. Encrypt, 2. Decrypt, 3.Exit");
pick = as.nextInt();
switch(pick)
{
case 1:
byte[] encode = Base64.getEncoder().encode(msg.getBytes());
System.out.println("The encoded message is: " + new String(encode));
break;
case 2:
System.out.println("Please enter your message again");
Scanner o = new Scanner(System.in);
String ms = o.nextLine();
byte[] s = ms.getBytes();
byte[] decode = Base64.getDecoder().decode(s);
System.out.println("Your message decoded is: " + new String(decode));
case 3:
System.exit(0);
break;
}
as.close();
}
}
Κατηγορίες:
Σχόλια