fork download
  1. using static System.Console;
  2.  
  3. public class Program {
  4. public static void Main() {
  5. int escolha = 1;
  6. while (escolha != 0) {
  7. WriteLine("ESCOLHA UMA OPÇÃO");
  8. WriteLine("-----------------");
  9. WriteLine("1-Encriptar");
  10. WriteLine("2-Decriptar");
  11. WriteLine("0-Encerrar");
  12. WriteLine("-----------------");
  13. if (!int.TryParse(ReadLine(), out escolha) || escolha < 0 || escolha > 2) {
  14. WriteLine("Opção inválida");
  15. continue;
  16. }
  17. if (escolha != 0) {
  18. WriteLine("Digite a mensagem: ");
  19. var mensagem = ReadLine();
  20. WriteLine("Digite a chave: ");
  21. var chave = ReadLine();
  22. WriteLine(mensagem.ToUpper());
  23. WriteLine(chave.ToUpper());
  24. WriteLine(CifraVigenere(mensagem, chave, escolha == 1));
  25. }
  26. }
  27. }
  28. private static string CifraVigenere(string mensagem, string chave, bool flag) { //normalmente não deveria escolher um método por um bool
  29. if (flag) {
  30. var codigo = "";
  31. for (int i = 0, j = 0; i < mensagem.Length; i++, j++) {
  32. char c = char.ToUpper(mensagem[i]);
  33. if (c < 'A' || c > 'Z') continue;
  34. codigo += (char)((c + char.ToUpper(chave[j % chave.Length]) - 2 * 'A') % 26 + 'A');
  35. }
  36. return codigo;
  37. }
  38. return ""; //até criar o decript
  39. }
  40. }
  41.  
  42. //https://pt.stackoverflow.com/q/185158/101
Success #stdin #stdout 0.02s 16116KB
stdin
Standard input is empty
stdout
ESCOLHA UMA OPÇÃO
-----------------
1-Encriptar
2-Decriptar
0-Encerrar
-----------------
Opção inválida