Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
Shift Cipher without knowing the key in c

I am writing a program to solve a random code.(the code will not have capital letters,any form of signs like full stop) The code is using shift cipher without knowing the key,it can also encrypt code but i will work on that one later.For now i am trying to output all 25 result first.

However if i enter things like "back to back",it will output a question mark at the end.

Here's my code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
  char code[400];
  char after[400];  // variable initialization
  int i, x, choice, z, temp, y;
  printf("                     Welcome\n");
  printf("Enter your code here:\n");
  scanf("%[^'\n']s", code);
  printf("Press 1 to Decrypt\nPress 2 to encrypt\nPress 3 to Exit\n");
  scanf("%d", &choice);

  // case 1 is decryption
  switch (choice) {
    case 1:
      for (x = 1; x <= 25; x++) {
        for (i = 0; (i <= 400 && code[i] != 0); i++)  // for loop
        {
          if (code[i] == 32) {  // 32 means space in ascii code
            after[i] = 32;
            continue;  // if there is a space it will continue
          }
          if (code[i] + x > 122) {  // 122 is z in ascii code
            after[i] = code[i] + x;  // if it is higher than 'z'
            after[i] = (after[i] - 122) + 96;  // it start counting form 'a'
            continue;
          } else {
            after[i] = code[i] + x;
            // if nothing is wrong,it starting adding the key
          }
        }
        printf("After decrypting:%s\n", after);
        printf("%s\n", code);
      }
      break;

    // case 2 is ecnrypt so its not important
    case 2:
      x = 5;
      for (i = 0; (i <= 400 && code[i] != 0); i++)  // for loop
      {
        if (code[i] == ' ') {
          continue;
        }
        if (code[i] + x > 122) {
          code[i] = code[i] + x;
          code[i] = (code[i] - 122) + 96;
          continue;
        } else {
          code[i] = code[i] + x;
        }
      }
      printf("After decrypting:%s", code);
      break;

    // not important as well
    case 3:
      printf("GOODBYE");
      break;

    // not important as well
    default:
      printf("Errors");
      return 0;
  }
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

To solve a shift cipher all you need is a dictionary and the first ten words or so of your ciphertext. Try all possibilities and test each “decoded” plaintext word against your dictionary. Choose the cipher with the highest match rate.

over 4 years ago · Santiago Trujillo Relatório

0

You forgot to null terminate after.

Perhaps you should do:

    for (i = 0; (i <= 400 && code[i] != 0); i++) {
      if (code[i] == 32) {
        after[i] = ...
      } else if (code[i] + x > 122) {
        after[i] = ...
      } else {
        after[i] = ...
      }
    }
    after[i] = '\0'; // <-- This line is missing.
    printf("After decrypting:%s\n", after);
    printf("%s\n", code);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda