Advertisement
StormWingDelta

Encryption/Decryption

Jan 22nd, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. //Strange issue with this is I for some reason can't use any characters in an Encryption code that are going to get //Encrypted be the next Encryption...?!
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. /**
  7.  * Write a description of class CryptoTest here.
  8.  *
  9.  * @author (your name)
  10.  * @version (a version number or a date)
  11.  */
  12. public class CryptoTest
  13. {
  14.     public static void main(String args[])
  15.     {
  16.         //Scanner kbReader = new Scanner(System.in);
  17.         System.out.print("Enter a sentence that is to be encrypted: ");
  18.         String sntnc = "This is a very big morning.";//kbReader.nextLine( );
  19.         System.out.println("Original sentence = " + sntnc);
  20.         MyCrypto myCryptObj = new MyCrypto( );
  21.         String encryptdSntnc = myCryptObj.encrypt2(sntnc);
  22.         System.out.println("Encrypted sentence = " + encryptdSntnc);
  23.         String decryptdSntnc = myCryptObj.decrypt2(encryptdSntnc);
  24.         System.out.println("Decrypted sentence = " + decryptdSntnc);
  25.     }
  26.  
  27.     /*
  28.      * Enter a sentence that is to be encrypted: This is a very big morning.
  29.      * Original sentence = This is a very big morning.
  30.      * Encrypted sentence = This is a ag',rery dug>?/ijeb..w ssadorninjeb..w.
  31.      * V = ag',r
  32.      * B = dug>?/
  33.      * G = jeb..w
  34.      * M = ssad
  35.      *
  36.      * Decrypted sentence = This is a very big morning.
  37.      *
  38.      *
  39.      */
  40.  
  41. }
  42.  
  43.  
  44.  
  45. import java.io.*;
  46. import java.util.*;
  47. /**
  48.  * Write a description of class MyCrypto here.
  49.  *
  50.  * @author (your name)
  51.  * @version (a version number or a date)
  52.  */
  53. public class MyCrypto
  54. {
  55.     public String MN;
  56.     public String Hold1;
  57.     public String Hold2;
  58.     private String Ch1;
  59.     private String Ch2;
  60.     private String Ch3;
  61.     private String[] Tar = new String[4];
  62.     private String[] Rep = new String[4];
  63.     /**
  64.      * Constructor for objects of class MyCrypto
  65.      */
  66.     public MyCrypto()
  67.     {
  68.         //
  69.         Ch1 = "agfgr";
  70.         Ch2 = "jeb..w";
  71.         Ch3 = "ssad";
  72.        
  73.         Tar[0] = "v";
  74.         Tar[1] = "g";
  75.         Tar[2] = "m";
  76.         Tar[3] = "b";
  77.        
  78.         Rep[0] = "g";
  79.         Rep[1] = "v";
  80.         Rep[2] = "b";
  81.         Rep[3] = "m";
  82.     }
  83.  
  84.     public String encrypt(String E)
  85.     {
  86.         MN = E.replace("v", Ch1);
  87.         Hold1 = MN.replace("g", Ch2);
  88.         MN = Hold1;
  89.         Hold2 = MN.replace("m", Ch3);
  90.         MN = Hold2;
  91.        
  92.         return MN;
  93.     }
  94.     public String decrypt(String D)
  95.     {
  96.         MN = D;
  97.         Hold1 = MN.replace(Ch2, "g");
  98.         MN = Hold1;
  99.         Hold2 = MN.replace(Ch3, "m");
  100.         MN = Hold2;
  101.         String Test = MN.replace(Ch1, "v");
  102.         MN = Test;
  103.         return MN;
  104.     }
  105.    
  106.     public String encrypt2(String E)
  107.     {
  108.         MN = E;
  109.         int Run = 0;
  110.         while(Run < 4)
  111.         {
  112.             Hold1 = MN.replace(Tar[Run], Rep[Run]);
  113.             MN = Hold1;
  114.             Run++;
  115.         }
  116.        
  117.         return MN;
  118.     }
  119.     public String decrypt2(String E)
  120.     {
  121.         MN = E;
  122.         int Run = 0;
  123.         while(Run < 4)
  124.         {
  125.             Hold1 = MN.replace(Rep[Run], Tar[Run]);
  126.             MN = Hold1;
  127.             Run++;
  128.         }
  129.        
  130.         return MN;
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement