Question
c++
LUILIIR Exercise #3: The xOr Cipher Write the fol lowing functions: void integerToBinary(int, int [0): // Convers an integer
OOO111i010101 Because the key is shorter than the message then we repeat the key digits as needed. 10001001001111100111010011
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;

void print_binary(int array[])
{
   int i;
   cout<<"\n\n";
   for(i=0;array[i]!=-1&&i<250;i++)
    {
cout<<array[i];
    }
cout<<"\n\n";
}
void rev_binary(int array[])
{
int count=-1,i,j;
for(;array[++count]!=-1;);
int array_temp[count];
for(i=0;array[i]!=-1&&i<250;i++)
{
array_temp[i]=array[i];
}
j=count-1;
for(i=0;i<count;i++)
{
array[i]=array_temp[j];
j--;
}
  
}
void IntegerToBinary(int num,int array[])
{
int r,d,i,count=-1;
d=num;
for(;count<250;)
{
r=d%2;
d=d/2;
  
count++;
array[count]=r;
if(d==1||d==0)
{
count++;
array[count]=d;
break;
}
}
array[count+1]=-1;
rev_binary(array);
}

void StringToBinary(string str,int array[])
{
   int i,j,count=0,temp;
   int array_temp[250];
  
   for(i=0;i<str.length();i++)
   {
       j=0;
       IntegerToBinary(str[i],array_temp);
       for(temp=count;array_temp[j]!=-1&&temp<250;temp++)
       {
           array[temp]=array_temp[j];
           j++;
           count++;
       }
   }
   array[count]=-1;
}

void XOr(int n1, int binary1[],int n2,int binary2[],int result[])
{
   int diff,i,temp;
   if(n1>n2)
   {
       temp=n2;
       diff=n1-n2;
       for(i=0;i<diff&&temp<250;i++)
       {
           binary2[temp]=binary2[i];
           temp++;
       }
       binary2[temp]=-1;
       n2=n1;
   }
   else if(n1<n2)
   {
       temp=n1;
       diff=n2-n1;
       for(i=0;i<diff&&temp<250;i++)
       {
           binary1[temp]=binary1[i];
           temp++;
       }
       binary1[temp]=-1;
       n1=n2;
   }
  
   for(i=0;i<n1;i++)
   {
       if(binary1[i]==binary2[i])
       {
           result[i]=0;
       }
       else
       {
           result[i]=1;
       }
   }
   result[i]=-1;
}
int GetLengthBinary(int array[])
{
   int i=-1;
   for(;array[++i]!=-1&&i<250;);
   return i;
}

int main()
{
   int array[250],i;

    IntegerToBinary(10,array);
    print_binary(array);

    StringToBinary("aaaaa",array);
    print_binary(array);
//    cout<<GetLengthBinary(array);

    int binary1[250],binary2[250],result[250],n1=-1,n2=-1;
    string st1,st2;
   
    cout<<"\nEnter String:";cin>>st1;
    cout<<"\nEnter Key:";cin>>st2;
   
    StringToBinary(st1,binary1);
    StringToBinary(st2,binary2);

   print_binary(binary1);
   print_binary(binary2);
  
   XOr(GetLengthBinary(binary1),binary1,GetLengthBinary(binary2),binary2,result);
  
   print_binary(result);
      
}

Add a comment
Know the answer?
Add Answer to:
c++ LUILIIR Exercise #3: The xOr Cipher Write the fol lowing functions: void integerToBinary(int, int [0):...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This...

    4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This function computes the number of appearances of each letter in the string word and stores them irn array freq of size 26. The letters are the 26 letters of the Latin alphabet whose ASCII values are in the range 97-122 for the lower case letters, and in the range 65-90 for the uppercase letters. You must account for uppercase and lowercase letter variants, which...

  • I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...

    I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key scanf("%s", encryptKey);                                                                   //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end { letter = message[iteration];                                                      ...

  • Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt...

    Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt a message is to use a date’s 6 digits to shift the letters. For example, if a date is picked as December 18, 1946, then the 6 digits are 121846. Assume the dates are in the 20th century. To encrypt a message, you will shift each letter of the message by the number of spaces indicated by the corresponding digit. For example, to encrypt...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n,...

    write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n, int key, int **loc); a is an array to be searched, n is the number of elements in the array, key is the search key, and loc is a pointer to the first location of the search key in array a (if found) or NULL otherwise. The function returns 1 if key is found in a, and returns 0 otherwise. Write a main() and...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher...

    Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher is an encryption algorithm that encrypts 1 byte of plain text at a time. This one uses a given 4-bit bit pattern as the key. The size of the encrypted message that we want to be able to send has a maximum length of 200 characters. You must: 1. prompt the user to input the clear text to be encrypted. You must use printf()...

  • u also need to store the letters' ASCII number in the freq array 4. [8] Write...

    u also need to store the letters' ASCII number in the freq array 4. [8] Write a C function with prototype · void letter_freq(const char word[], int freq []); This function computes the number of appearances of each letter in the string word and stores them in array freq of size 26. The letters are the 26 letters of the Latin alphabet whose ASCII values are in the range 97-122 for the lower case letters, and in the range 65-90...

  • In java write a command-line program that helps to decrypt a message that has been encrypted...

    In java write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1. Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case) are encrypted – a constant number, the shift, is added to the ASCII value of each letter and when letters are shifted beyond ‘z’ or ‘Z’ they are wrapped around (e.g. “Crazy?” becomes “Etcba?” when shifted by 2). When your...

  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT