Advertisement
Mizuhara_Chizuru

encry

Apr 23rd, 2023
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | Software | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.lang.*;
  4. public class Main
  5. {
  6.     static int pass = 0;
  7.     static String ans, backup;
  8.     static Scanner sc = new Scanner(System.in);
  9.     public static String decrypt(String backup)
  10.     {
  11.         int temp, len = backup.length(), i = 0;
  12.         char ans2[] = new char[len];
  13.         char x;
  14.         while (i < len)
  15.         {
  16.             if (pass > 50)
  17.                 pass = 0;
  18.             temp = backup.charAt(i);
  19.             temp -= pass;
  20.             x = (char)temp;
  21.             ans2[i] = x;
  22.             i++;
  23.             pass++;
  24.         }
  25.         ans2[len] = '\0';
  26.         return String.valueOf(ans2);
  27.     }
  28.  
  29.     public static String decryptv2()
  30.     {
  31.         String f;
  32.         System.out.println("Enter the name(with extension) of file you want to open.\n\n\t");
  33.         f = sc.next();
  34.         try
  35.         {
  36.             File file = new File(f);
  37.             BufferedReader br = new BufferedReader(new FileReader(file));
  38.             String str = "", s;
  39.             while ((s = br.readLine()) != null)
  40.                 str += s;
  41.             br.close();
  42.             return decrypt(str);
  43.         }
  44.         catch (Exception e)
  45.         {
  46.             System.out.println("File not found");
  47.             return "";
  48.         }
  49.     }
  50.  
  51.     public static void save(StringBuilder backup)
  52.     {
  53.         String f;
  54.         System.out.println("Enter the name of the file(with extension)\n\n\t");
  55.         f = sc.next();
  56.         try
  57.         {
  58.             FileWriter file = new FileWriter(f);
  59.             file.write(backup.toString());
  60.             System.out.println("The text is succesfully saved in a file named \"" + f + "\"" + (char)24 + "\n\n\t");
  61.             file.close();
  62.             System.in.read();
  63.         }
  64.         catch (Exception e)
  65.         {
  66.             System.out.println("Error occurred while saving the file.");
  67.         }
  68.     }
  69.  
  70.     public static void save2(StringBuilder backup)
  71.     {
  72.         String f;
  73.         System.out.println("Enter the name of the file(with extension)\n\n\t");
  74.         f = sc.next();
  75.         try
  76.         {
  77.             FileWriter file = new FileWriter(f);
  78.             file.write(backup.toString());
  79.             System.out.println("The text is succesfully saved in a file named \"" + f + "\"" + (char)24 + "\n\n\t");
  80.             file.close();
  81.             System.in.read();
  82.         }
  83.         catch (Exception e)
  84.         {
  85.             System.out.println("Error occurred while saving the file.");
  86.         }
  87.     }
  88.  
  89.     public static String encrypt(String str)
  90.     {
  91.         int len, i = 0, temp;
  92.         len = str.length();
  93.         char enc[] = new char[len];
  94.         char x;
  95.         while (i < len)
  96.         {
  97.             if (pass > 50)
  98.                 pass = 0;
  99.             temp = str.charAt(i);
  100.             temp += pass;
  101.             x = (char)temp;
  102.             enc[i] = x;
  103.             i++;
  104.             pass++;
  105.         }
  106.         enc[len] = '\0';
  107.         pass = 0;
  108.         backup = String.valueOf(enc);
  109.         return String.valueOf(enc);
  110.     }
  111.  
  112.     public static void encryptv2()
  113.     {
  114.         System.out.println("Enter the Filename(with extension).\n\n\t");
  115.         String str = "", f;
  116.         f = sc.next();
  117.         try
  118.         {
  119.             File file = new File(f);
  120.             BufferedReader br = new BufferedReader(new FileReader(file));
  121.             while (br.ready())
  122.                 str += br.readLine();
  123.             br.close();
  124.             backup = encrypt(str);
  125.         }
  126.         catch (Exception e)
  127.         {
  128.             System
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement