(Solved Homework): Implement the Tiny Encryption Algorithm (TEA). a. Use your TEA algorithm to encrypt the 64-bit plaintext blo…

ONLY PART B PLEASE!!!

Don't use plagiarized sources. Get Your Custom Essay on
(Solved Homework): Implement the Tiny Encryption Algorithm (TEA). a. Use your TEA algorithm to encrypt the 64-bit plaintext blo…
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
Order Essay

Implement the Tiny Encryption Algorithm (TEA). a. Use your TEA algorithm to encrypt the 64-bit plaintext block 0x0123456789ABCDEF using the 128-bit key OxA56BABCD00000000FFFFFFFFABCDEF01. Decrypt the resulting ciphertext and verify that you obtain the original plaintext. b. Using the key in part a, encrypt and decrypt the following message using each of the three block cipher modes discussed in the text (ECB mode, CBC mode, and CTR mode). Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

Expert Answer

 

import java.math.BigInteger;


public class TEA {

    BigInteger [] K ; //128 bits key
    private String plainText;
    public static final BigInteger delta = new BigInteger("9e3779b9",16);


    //constructor receives a string of plaintext and 128 bit key in hexadecimal
    public TEA(String plainText, String key)
    {
        parseKey(key);


    }

    //constructor receives a hexadecimal 
    public TEA(String key)
    {

        parseKey(key);

    }

    //parses a 128 bit key, given in hexadecimal form, and store its value in 4 integers (total of 128 bits), 
    private void parseKey(String key)
    {
        if(key.substring(0,2).equals("0x"))
            key= key.substring(2);

        //validating input
        if(key.length() != 32)
        {
            System.out.println("Invalid key size!");
            return;
        }


        //dividing the key into 4 strings
        String[] kStr = new String[4];
        int index=-1;
        for(int i=0; i<key.length(); i++)
        {
            if(i%8 == 0)
            {
                index++;
                kStr[index]="";

            }
            kStr[index] = kStr[index] + key.charAt(i);
        }


        //converting the 4 hex strings into 4 integers
        K= new BigInteger[4];
        for(int i=0; i<4; i++)
            K[i] = new BigInteger(kStr[i], 16); 

    }

    //receives a plaintext block of 64 bits in hexadecimal to be encrypted
    //returns the cipher block
    String encryptBlock(String plainTextBlock)
    {
        if(plainTextBlock.substring(0,2).equals("0x"))
            plainTextBlock= plainTextBlock.substring(2);

        //validating input
        if(plainTextBlock.length()!=16)
        {
            System.out.println("Invalid block size!");
            return null;

        }

        //separating the string block into left and right blocks
        String LStr = plainTextBlock.substring(0, 8); //left block (32 bit)
        String RStr = plainTextBlock.substring(8); //right block (32 bit)

        //converting left and right blocks to integers
        BigInteger L = new BigInteger(LStr, 16);
        BigInteger R = new BigInteger(RStr, 16);


        BigInteger sum= new BigInteger("0");
        //32 rounds
        for(int i=0; i<32; i++)
        {
            sum = sum.add(delta);
            L= sum(L,  (sum(shiftLeft(R,4),K[0]))   .xor(sum(R,sum))    .xor(sum(shiftRight(R,5),K[1]))) ;
            R= sum(R,  (sum(shiftLeft(L,4),K[2]))   .xor(sum(L,sum))    .xor(sum(shiftRight(L,5),K[3]))) ;

            //R= R.add(  (shiftLeft(R,4).add(K[2])).xor(L.add(sum)).xor(shiftRight(L,5).add(K[3])) );


        }

        //joining back the blocks as hex
        String cipherBlock = "0x"+L.toString(16)+R.toString(16)+"";


        return cipherBlock;
    }


    //receives a ciphertext block of 64 bits in hexadecimal to be decrypted
    //returns the plaintext block
    String decryptBlock(String cipherBlock)
    {
        if(cipherBlock.substring(0,2).equals("0x"))
            cipherBlock= cipherBlock.substring(2);

        //validating input
        if(cipherBlock.length()!=16)
        {
            System.out.println("Invalid block size!");
            return null;

        }

        //separating the string block into left and right blocks
        String LStr = cipherBlock.substring(0, 8); //left block (32 bit)
        String RStr = cipherBlock.substring(8); //right block (32 bit)

        //converting left and right blocks to integers
        BigInteger L = new BigInteger(LStr, 16);
        BigInteger R = new BigInteger(RStr, 16);

        BigInteger sum= shiftLeft(delta,5);
        //32 rounds
        for(int i=0; i<32; i++)
        {

            R= subtract(R,  (sum(shiftLeft(L,4),K[2]))   .xor(sum(L,sum))    .xor(sum(shiftRight(L,5),K[3]))) ;
            L= subtract(L,  (sum(shiftLeft(R,4),K[0]))   .xor(sum(R,sum))    .xor(sum(shiftRight(R,5),K[1]))) ;


            //R= R.subtract(  (L.shiftLeft(4).add(K[2])).xor(L.add(sum)).xor(L.shiftRight(5).add(K[3])) );
            //L= L.subtract(  (R.shiftLeft(4).add(K[0])).xor(R.add(sum)).xor(R.shiftRight(5).add(K[1])) );
            sum = sum.subtract(delta);  
        }

        //joining back the blocks as hex
        String plainTextBlock = "0x"+L.toString(16)+R.toString(16)+"";


        return plainTextBlock;
    }


    private BigInteger shiftLeft(BigInteger x, int steps)
    {

       BigInteger shifted=null;
       boolean negative =false;

       String xStr = x.toString(2);

       //removing negative sign while shifting (currently)
       if(xStr.charAt(0)=='-')
       {
           negative= true;
           xStr = xStr.substring(1);
       }


       int additionalSize = 32- xStr.length();

       for(int i=0; i<additionalSize; i++)
           xStr= "0"+xStr;



       for(int i=0; i<steps; i++)
       {
           xStr = xStr.substring(1);
           xStr = xStr+"0";
       }

       //one last addition of negative sign if the number is negative
       if(negative==true)
           xStr= "-"+xStr;

       //System.out.println(xStr);
      shifted = new BigInteger(xStr,2);

        return shifted;
    }


    private BigInteger shiftRight(BigInteger x, int steps)
    {
       BigInteger shifted=null;
       boolean negative = false;

       String xStr = x.toString(2);

       //removing negative sign while shifting (currently)
       if(xStr.charAt(0)=='-')
       {
           negative= true;
           xStr = xStr.substring(1);
       }

       int additionalSize = 32- xStr.length();

       for(int i=0; i<additionalSize; i++)
           xStr= "0"+xStr;


       for(int i=0; i<steps; i++)
       {
           xStr = xStr.substring(0,xStr.length()-1);
           xStr = "0"+xStr;
       }

       //one last addition of negative sign if the number is negative
       if(negative==true)
           xStr= "-"+xStr;

      shifted = new BigInteger(xStr,2);

        return shifted;
    }

    private BigInteger sum(BigInteger a, BigInteger b)
    {

        BigInteger sum = a.add(b);
        String sumStr = sum.toString(2);
        if(sumStr.length()>32)
        {
            int diff = sumStr.length()- 32;
            sumStr = sumStr.substring(diff);
        }

        BigInteger newSum = new BigInteger(sumStr,2);

        return newSum;
    }

    private BigInteger subtract(BigInteger a, BigInteger b)
    {

        BigInteger sub = a.subtract(b);

        String subStr = sub.toString(2);
        if(subStr.length()>32)
        {
            int diff = subStr.length()- 32;
            subStr = subStr.substring(diff);
        }

        BigInteger newSub = new BigInteger(subStr,2);

        return newSub;
    }



    public static void main(String[] args)
    {

        String plainText="0x0123456789ABCDEF";
        String key= "0xA56BABCD00000000FFFFFFFFABCDEF01";
        TEA tea = new TEA(key);
        String cipherText = tea.encryptBlock(plainText);
        System.out.println("Original Plain Text:"+plainText);
        System.out.println("CipherText:"+cipherText);
        System.out.println("Decrypted CipherText is:"+tea.decryptBlock(cipherText));




    }


}

Homework Ocean
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents