Decrypting stream cipher in a simple program.

Hello! I'm trying/testing some stream cipher where, the key changes every time when it goes to the end of it, and then uses the new key again for the next decryption. I compile it with g++ no errors or warnings, but when I run the file, it gives me "Segmentation fault" as I am trying to view the first letter of the now new and decrypted text. Any ideas why is that? Here is my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;

int main()
{
char text[] = "hwcbwafjw";// text to decrypt
char key[]  = "wiki";//the key
char * new_t;//here will store the new decrypted text
int k = 0;

for (int i = 0; i < strlen(text); )
{
  for(int j = 0; (j < strlen(key) || i < strlen(text)); i++,j++)
  {
    new_t[i] = (text[i] - key[i] + 26) % 26;
  }
  for(int j = i - strlen(key) - 1; j < strlen(key); j++, k++)
  {
    key[j] = new_t[j] + key[k] % 26;
  }
}
  cout<<new_t[0];
  return 0;
}
declare new_t like char new_t[10];
Also, maybe this (j < strlen(key) || i < strlen(text)) should be (j < strlen(key) && i < strlen(text)) and, um... are you sure that you don't get out of the bounds of any of your arrays the way you're doing things? :/
Last edited on
The char new_t[strlen(text)]; did the right thing, and there i want to be 'or' statement so I think || is right for what I am trying to accomplish, but I have some mistake in the algorithm where I get the new key, so I have to figure it out because only the first 5 letters of the decrypted word are the right ones. The next are not right, because of the key changing every time.
I should get lostpedia as the decrypted text, instead of that i get lostpbehc :)
Last edited on
Topic archived. No new replies allowed.