Advertisement
Guest User

C#_Caesar_Cipher

a guest
Jul 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9.  
  10. using System.Windows.Forms;
  11. using System.Runtime.InteropServices;
  12.  
  13. namespace WorldsWorstCypherOhAndBTW2PacisAliveMark2
  14. {
  15.  
  16.  
  17.     class Program
  18.     {
  19.  
  20.         [DllImport("User32", CharSet = CharSet.Auto)] // <--- needed for the wallpaper changing
  21.         public static extern int SystemParametersInfo(int uiAction, int uiParam,
  22.                string pvParam, uint fWinIni);
  23.  
  24.         static Random rnd = new Random();
  25.         //static string[] alphabet = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
  26.        
  27.         static int movement = 0;
  28.  
  29.         static string word = "";
  30.  
  31.         static string[] newalpha = new string[alphabetget().Length];
  32.  
  33.         static int[] arr = Enumerable.Range(0, alphabetget().Length).OrderBy(c => rnd.Next()).ToArray();
  34.         static int count = 0;
  35.  
  36.         static void Main(string[] args)
  37.         {
  38.             create_alphabet_file();
  39.  
  40.             take_inputs();
  41.  
  42.             create_shift_alphabet();
  43.             shuffle();
  44.             justEndMeAlready();
  45.            
  46.  
  47.             Console.ReadLine();
  48.         }
  49.  
  50.         static void create_alphabet_file()
  51.         {
  52.             File.WriteAllText("alphabet.txt","a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z");
  53.         }
  54.  
  55.         static string[] alphabetget()
  56.         {
  57.             return File.ReadAllText("alphabet.txt").Split(',');
  58.            
  59.         }
  60.  
  61.         //inputs
  62.  
  63.         static void take_inputs()
  64.         {
  65.             Console.WriteLine("Ceaser cypher program");
  66.             Console.Write("Enter Text : ");
  67.             word = Console.ReadLine();
  68.             Console.Write("Enter shift : ");
  69.             movement = Convert.ToInt16(Console.ReadLine());
  70.  
  71.             Console.WriteLine("Sit back and relax...");
  72.             Console.WriteLine("This may take a while");
  73.         }
  74.  
  75.         // outputs
  76.  
  77.         static void justEndMeAlready()
  78.         {
  79.             File.WriteAllText("output.txt", Cypher());
  80.             System.Diagnostics.Process.Start("GUIinVisUalBasIc.exe"); //dont forget to put the compiled vb in the same folder as the exicuteing code here
  81.  
  82.             string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/victory.jpg";
  83.             System.Threading.Thread.Sleep(4000);
  84.             Screenshot();
  85.             SystemParametersInfo(0x0014, 0, path, 0x0001);
  86.         }
  87.  
  88.         // the big shuffle
  89.  
  90.  
  91.         static void shuffle()
  92.         {
  93.             arr = Enumerable.Range(0, alphabetget().Length).OrderBy(c => rnd.Next()).ToArray();
  94.             for(int i = 0; i < alphabetget().Length; i ++)
  95.             {
  96.                
  97.                 newalpha[i] = alphabetget()[arr[i]];
  98.                 count += 1;
  99.             }
  100.         }
  101.  
  102.         static void create_shift_alphabet()
  103.         {
  104.             bool done = false;
  105.             int count = 0;
  106.  
  107.             while (done == false)
  108.             {
  109.                 count += 1;
  110.                 shuffle();
  111.                 done = Check_array_shift();
  112.             }
  113.             Console.WriteLine(count.ToString()+ " " + showarrr());
  114.         }
  115.  
  116.         // display arr for testing
  117.  
  118.         static string showarrr() // <----- Nnot needed for program exicution (left in the code anyway)
  119.         {
  120.             string output = "";
  121.             for (int i = 0; i < alphabetget().Length; i++)
  122.             {
  123.                 output += newalpha[i];
  124.             }
  125.             return output;
  126.         }
  127.  
  128.  
  129.  
  130.         // forget about this for now
  131.         static int Loop_Movement(int i) // helps the shift not fuck up when it gets to the end of an array
  132.         {
  133.             if ((movement + i) >= alphabetget().Length)
  134.             {
  135.                 return (movement + i) - alphabetget().Length;
  136.             }
  137.             else return movement + i;
  138.         }
  139.  
  140.         static bool Check_array_shift()// final utility check keeps some arbitery buffers updateing
  141.         {
  142.             bool check = true;
  143.             for (int i = 0; i < alphabetget().Length; i++)
  144.             {
  145.                 if (alphabetget()[Loop_Movement(i)] != newalpha[i])
  146.                 {
  147.                     check = false;
  148.                 }
  149.             }
  150.            
  151.             return check;
  152.         }
  153.  
  154.  
  155.         // actual cyphering
  156.  
  157.         static string Cypher()
  158.         {
  159.             string output = "";
  160.             for (int i = 0; i < word.Length; i++)
  161.             {
  162.                 if (word[i].ToString() != " ")
  163.                 {
  164.  
  165.                     for (int e = 0; e < alphabetget().Length; e++)
  166.                     {
  167.                         if (word[i].ToString() == alphabetget()[e]) output += newalpha[e];
  168.                     }
  169.                 }
  170.                 else
  171.                 {
  172.                     output += " ";
  173.                 }
  174.             }
  175.  
  176.             return output;
  177.         }
  178.  
  179.         // screenshotting utilitys
  180.  
  181.  
  182.  
  183.  
  184.         static void Screenshot()
  185.         {
  186.             Rectangle bounds = Screen.GetBounds(Point.Empty);
  187.             using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
  188.             {
  189.                 using (Graphics g = Graphics.FromImage(bitmap))
  190.                 {
  191.                     g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
  192.                 }
  193.  
  194.                 bitmap.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/victory.jpg", ImageFormat.Jpeg);
  195.             }
  196.         }
  197.  
  198.  
  199.  
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement