Monday, October 13, 2014

Encrypt and Decrypt



Encrypt and Decrypt




Assign Value method
Encrypt button Event
String plainText = txtDec.getText();
     
        CaesarCrypt cc = new CaesarCrypt();
        String cipherText = cc.encrypt(plainText);
        lblEnc.setText(cipherText);

Decrypt button Event
String cipherText = txtEnc.getText();
       
        CaesarCrypt cc = new CaesarCrypt();
        String plainText = cc.decrypt(cipherText);
        lblDec.setText(plainText);

CaesarCrypt  Class code

public class CaesarCrypt {
   private final String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-";
  
  
   public String encrypt(String plainText)
   {
       
         String cipherText="";
               
         for(int i=0; i < plainText.length(); i++)
         {
          
            if (plainText.charAt(i) == 'a')
            {
               cipherText += "1";
            }
            if (plainText.charAt(i) == 'b')
            {
               cipherText += "2";
            }
            if (plainText.charAt(i) == 'c')
            {
               cipherText += "3";
            }
            if (plainText.charAt(i) == 'd')
            {
               cipherText += "4";
            }
         }
         return cipherText;
   }
  
   public String decrypt(String cipherText)
   {
      
         String plainText="";    
        
         for(int i=0;i<cipherText.length();i++)
         {
         
            if (cipherText.charAt(i) == '1')
            {
               plainText += "a";
            }
            if (cipherText.charAt(i) == '2')
            {
               plainText += "b";
            }
            if (cipherText.charAt(i) == '3')
            {
               plainText += "c";
            }
            if (cipherText.charAt(i) == '4')
            {
               plainText += "d";
            }
         }
        
         return plainText;
   }
}


Shift Key method





Encrypt button Event
String plainText = txtDec.getText();
        int shiftKey = Integer.parseInt(txtShift.getText());

        caseaeCryptShift cc = new caseaeCryptShift();
        String cipherText = cc.encrypt(plainText, shiftKey);
        lblEnc.setText(cipherText);
Decrypt button Event
  String cipherText = txtEnc.getText();
        int shiftKey = Integer.parseInt(txtShift.getText());

        caseaeCryptShift cc = new caseaeCryptShift();
        String plainText = cc.decrypt(cipherText, shiftKey);
        lblDec.setText(plainText);

caseaeCryptShift Class code

public class caseaeCryptShift {

    private final String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-";

    public String encrypt(String plainText, int shiftKey) {
      
        String cipherText = "";
        int charPosition;
        int keyVal;
        char replaceVal;

        for (int i = 0; i < plainText.length(); i++) {
            charPosition = chars.indexOf(plainText.charAt(i));
            keyVal = (charPosition + shiftKey) % 64;

            replaceVal = this.chars.charAt(keyVal);
            cipherText += replaceVal;
        }
        return cipherText;
    }

    public String decrypt(String cipherText, int shiftKey) {
      
        String plainText = "";
        int charPosition;
        int keyVal;
        char replaceVal;

        for (int i = 0; i < cipherText.length(); i++) {
            charPosition = this.chars.indexOf(cipherText.charAt(i));
            keyVal = charPosition - shiftKey;

            if (keyVal < 0) {
                keyVal = this.chars.length() + keyVal;
            }

            replaceVal = this.chars.charAt(keyVal);
            plainText += replaceVal;
        }

        return plainText;
    }
}




C# decryptuin with constant


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            //Int64 id = Encrypt("abacus");
            string word = Dencrypt(334160560256);
            System.Console.WriteLine(word);
            System.Console.ReadLine();
        }

        private static Int64 Encrypt(string p)
        {
            Int64 c = 31;
            for (int i = 0; i < p.Length; i++)
            {
                c = c * 47 + (byte)p[i] % 97;
            }

            return c;
        }

        private static string Dencrypt(Int64 id)
        {
            string word = "";
            while (id > 31)
            {          
                byte c= (byte) (id % 47 + 97) ; // remove the multiplied value and extract the character
                char ch= Convert.ToChar(c); // get the charactor
                word = ch + word; // create the word
                id = id / 47;
               
            }

            return word;
        }

    }
}

/*
The following Python function encrypts plain text to
cipher text. For example given the text 'abacus' the function returns a
the cypher text 334160560256. Your task is to reverse engineer the
algorithm and find the source text which results in the cypher text
15731535468486 in a language of your choosing. Remember to comment the
code with your reasoning.

def encrypt (p):
    h = 31
    for i in range(0, len(p)):
        h = h * 47 + ord(p[i]) % 97
    return h
*/




 
 

No comments:

Post a Comment