#encryption behaviour unexpected

84 messages · Page 1 of 1 (latest)

slate wagon
#

Hi there trying to make a text cipher for cs50 ( coding beginner) , i didn't want to cheat and watch a guide . i Thought i had figured out a solution but i am getting unexpected behaviour.

the program takes a command line argument i.e the key for the cipher
followed by the message to be encrypted

i am subtracting the key from the regular alphabet to get the distance between ascii characters so i can then re apply the distance to the the message in order to encrypt it as per the key.

i have double checked the math and the distance is calculated properly yet upon re applying it something is going wrong and i cant figure out what.

string message = get_string("Plaintext :");
int meslen = strlen(message);
for(int i=0;i<meslen;i++){
if(isupper(message[i])){
message[i]+=32;
for(int a=0; a<alphlen;a++){
if(message[i]==alphabet[a]){
message[i]= message[i]-alphdiff[a];

}
else{}
}
message[i]-=32;
}
else if(islower(message[i])){
for(int a=0; a<alphlen;a++){
if(message[i]==alphabet[a]){
message[i]=message[i]-alphdiff[a];

}
else{}
}

}
else{message[i]=message[i];}

}
printf("%s",message);
}

rare fogBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

novel cradle
#

string message = get_string("Plaintext :");
int meslen = strlen(message);
for(int i=0;i<meslen;i++){
if(isupper(message[i])){
message[i]+=32;
for(int a=0; a<alphlen;a++){
if(message[i]==alphabet[a]){
message[i]= message[i]-alphdiff[a];

}
else{}
}
message[i]-=32;
}
else if(islower(message[i])){
for(int a=0; a<alphlen;a++){
if(message[i]==alphabet[a]){
message[i]=message[i]-alphdiff[a];

}
else{}
}

}
else{message[i]=message[i];}

}
printf("%s",message);
}

novel cradle
#
string message = get_string("Plaintext :");
int meslen = strlen(message);
for (int i = 0; i < meslen; i++) {
  if (isupper(message[i])) {
    message[i] += 32;
    for (int a = 0; a < alphlen; a++) {
      if (message[i] == alphabet[a]) {
        message[i] = message[i] - alphdiff[a];

      } else {}
    }
    message[i] -= 32;
  } else if (islower(message[i])) {
    for (int a = 0; a < alphlen; a++) {
      if (message[i] == alphabet[a]) {
        message[i] = message[i] - alphdiff[a];

      } else {}
    }

  } else {
    message[i] = message[i];
  }

}
printf("%s", message);
}
#

i assume alphlen is 26 ?

slate wagon
#

yeah

#

uhh can ia sk u how to format like that im new here

novel cradle
#
int x 
#

and alphdiff is what ?

slate wagon
#

alphdiff is alphabet[i] - key[i]

#

running in a loop to get 26 variations

novel cradle
#

post the full code

slate wagon
#

sure

#

do i wrap it in the '''c'''

novel cradle
#

`

#

backticks

#

not '

slate wagon
#

cc c'''#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(int argc, string argv[])
{
int keylen=strlen(argv[1]);
string key = argv[1];
int alphdiff[26] ;
if(keylen!=26){
printf("key must contain 26 Letters");
return 1;
}
else if(argc!=2){printf("Usage: ./substitution key");
return 1;

}//all works till here
else{
for(int i=0; i<keylen;i++){
if(isupper(key[i])){
key[i]+=32;
}

}

}
string alphabet= "abcdefghijklmnopqrstuvwxyz";
int alphlen= strlen(alphabet);
for(int i=0;i<keylen;i++){
alphdiff[i]=alphabet[i]-key[i];// havent tested

}
for(int i=0;i<alphlen;i++){
    printf("%i  %c  %c", alphdiff[i], alphabet[i],key[i]);
    printf("\n");
}

string message = get_string("Plaintext :");
int meslen = strlen(message);
for(int i=0;i<meslen;i++){
if(isupper(message[i])){
message[i]+=32;
for(int a=0; a<alphlen;a++){
if(message[i]==alphabet[a]){
message[i]= message[i]-alphdiff[a];

}
else{}
}
message[i]-=32;
}
else if(islower(message[i])){
for(int a=0; a<alphlen;a++){
if(message[i]==alphabet[a]){
message[i]=message[i]-alphdiff[a];

}
else{}
}

}
else{message[i]=message[i];
#

uhm ...

novel cradle
#

3x ` followed by a c
your code
3x `

slate wagon
#
include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(int argc, string argv[])
{
int keylen=strlen(argv[1]);
string key = argv[1];
int alphdiff[26] ;
if(keylen!=26){
    printf("key must contain 26 Letters");
    return 1;
}
else if(argc!=2){printf("Usage: ./substitution key");
return 1;


}//all works till here
else{
    for(int i=0; i<keylen;i++){
        if(isupper(key[i])){
 key[i]+=32;
        }


    }

}
string  alphabet= "abcdefghijklmnopqrstuvwxyz";
int alphlen= strlen(alphabet);
    for(int i=0;i<keylen;i++){
        alphdiff[i]=alphabet[i]-key[i];// havent tested


    }
    for(int i=0;i<alphlen;i++){
        printf("%i  %c  %c", alphdiff[i], alphabet[i],key[i]);
        printf("\n");
    }
string message = get_string("Plaintext :");
int meslen = strlen(message);
for(int i=0;i<meslen;i++){
    if(isupper(message[i])){
        message[i]+=32;
        for(int a=0; a<alphlen;a++){
if(message[i]==alphabet[a]){
    message[i]= message[i]-alphdiff[a];


}
else{}
        }
        message[i]-=32;
    }
    else if(islower(message[i])){
        for(int a=0; a<alphlen;a++){
if(message[i]==alphabet[a]){
    message[i]=message[i]-alphdiff[a];

}
else{}
}

    }
    else{message[i]=message[i];}




}
printf("%s",message);
}```
novel cradle
#

is it supposed to subsitute each letter of message with key ?

slate wagon
#

exactly

novel cradle
#

encrypted[i] = key[tolower(message[i]) - 'a']

#

e.g. if message = "ABC" and key = "zyx"

i = 0

encrypted[i] = key[tolower(message[i]) - 'a'] 
encrypted[0] = key[tolower(message[0]) - 'a']
encrypted[0] = key[tolower('A') - 'a']
encrypted[0] = key['a' - 'a']
encrypted[0] = key[0]
encrypted[0] = 'z'

...
encrypted[1] = 'y'
encrypted[2] = 'x'
slate wagon
#

i dont follow what its doing man

novel cradle
#

what part dont you get

slate wagon
#

i dont see how this takes the users input of a key and transforms the second input i.e the message they type when prompted into a new encrypted message, because u have no reference to the inputted text

#

you are not using the distance between characters in intigers?

novel cradle
#

message is the user input
key is well yea the key

#

each letter of message is replaced with the letter in key

slate wagon
#

and placed into a new array encrypted

#

?

novel cradle
#

yea

#

you can also place it in message[i] instead of encrypted[i]

slate wagon
#

do you see where the error is in my code?

#

or is it a downright mess

#

lol

novel cradle
#

its a mess yea

slate wagon
#

haha ok

#

can i ask you what the - 'a' is

#

or is that just for me

novel cradle
#

its so we can index at 0 where the key string starts

#
'a' - 'a' = 0
'b' - 'a' = 1
'c' - 'a' = 2
...
'z' - 'a' = 25
slate wagon
#

can i try to explain my logic to you if you have time ? i dont wanna be a bother so feel free to say no im sure with more experience ill understand better but i rlly dont see why my program isnt running as expected

novel cradle
#

you can explain it yea

slate wagon
#

right

novel cradle
#

but i wont debug or read it troll

slate wagon
#

no problem

#

ill explain in english

#

i take the key and set it into a string and i also create a string of the regular alphabet to start

#

i run a for loop where every character of the alphabet is deducted by every character of the key and store it into a new array alphdiff

#

then i run through a nester for where for each character of the message input i check wether it is the same as the alphabet character and when it is i re add the diffirence stored in alph diff

#

/cann u see any problem with it?

novel cradle
slate wagon
#

but you had to manually set that right

#

?

#

i wanted to avoid that

#

took it as like a challenge to do it automatic

#

or am i missing something

novel cradle
#

i dont set it manually

#

the difference is calced on the fly

#

message[i] - 'a'

slate wagon
#

so conceptually were doing the same thing

novel cradle
#

ye

slate wagon
#

i have to assume then my syntax is fucking it up

#

so im gonna try to understand your syntax betetr

#

you take key at index msg as your and set it as encrypted at index 0

#

is that right?

novel cradle
#

at index alphadiff

#

message[i] would be 65 if message[i] = 'a'

slate wagon
#

oh i c you are bypassing that whole bit by using the -a- to set it straight into alphadiff?

novel cradle
#

our key is only 26 letters long id index out of bounds

#

if i subtract it by 'a' i get a 0

slate wagon
#

ahhhaaaaaaaaaaaaaaa

#

im so sry i didnt realise the - 'a' was a direct subtraction i thought it was some unknown to me way of setting parameters of an array

#

thank you so much man its clear to me now

#

thank you for your patience ill catch you later

rare fogBOT
#

@slate wagon Has your question been resolved? If so, run !solved :)

rare fogBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.