Hello All,
I am very new to C programming but i was working on cryptography for ipsec and am trying to write a code for implementing a simple code for a stream cipher which will take a 20 charecter long string and a 8 bit seed file and encrypt the text by performing a bit by bit xor operation on the string by generating a random number with same length as text using the seed string.
I know i made some big mistakes but i dont see any compilation errors but i get a runtime error and it just prints the charecters in printf but never accepts any value.
Could anyone kindly help me on this or correct me.
Any help would be much appreciated.Below is the code i have come up with.Again i want to stress i am very new to C

#include<stdio.h>
#include<string.h>
int main()
{
  char string[20];
  char key[8];
  char cout;
  char random[20];
  int i;
  int j;
  j=strlen(string);
  printf("enter the cipher text");
scanf("%c",&string); 
printf("enter the seed value");
scanf("%c",&key);
random[20]=srand(key);   
  for(i=0; i<=j; i++)
  {
    string[i]=string[i]^random[i];
    cout=string;
  }
  printf("ciphertext is &s",cout);

}

Recommended Answers

All 2 Replies

You will get a much better response if you improve your questions. Here is how you can improve your questions:

1) Show us the code. You already do this. Good.
2) Tell us exactly what input you type in.
3) Tell us what you saw happen, and what you think should have happened.

I note that you are trying to get the length of string before the user has typed anything in. That doesn't seem sensible.

I note also that you are trying to set an array to the returned value from srand;
random[20]=srand(key);

Tell me, what kind of object does srand return? Also, what do you think srand actually does? I will tell you that it does NOT generate random numbers.

cout=string; Given that cout is a single char and string is an array of char, what do you expect this to do?

Line 11: Undefined behaviour: you call strlen on an unititialised string, this could easily lead to memory accesses outside the boundaries of the array string. May be you meant to do this after string is requested from the user.

Line 13: Format string error: %c tells scanf to get a character from stdin not a string, perhaps you meant %s or you could have used fgets to prevent buffer overflow.

Line 15: Format string error: %c tells scanf to get a character from stdin not a string, perhaps you meant %s or you could have used fgets to prevent buffer overflow.

Line 16: Undefined behaviour: random is an array with size 20, that means valid index values are 0 <= index < 20. Using and index of 20 accesses memory outside the boundaries of the object.

Line 16: Language Error: srand returns void, i.e. nothing, you can not assign it to anything.

Line 17: Logic Error: i has the range 0 <= i <= j. Even assuming j had been correctly calculated at line 11 this should be range should be 0 <= i < j, if the string is length 3 then valid indexes for characters in it are in the range 0 - 2.

Line 19: Logic Error: xoring with random[i] with i in the range 0 <= i <= j but at no time are any of the entries in random initialised to anything.

Line 20: Language Error: Attempt to assign an array of char to a single char.

Line 22: Format String Error: that should % not & and %s is used to output a string but cout is a char the correct format specifier for a char is %c

For a program this simple you should be able to compile without errors or warnings, if you are getting errors or warnings during complation understand and fix them first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.