Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Simple Ciphers & String Transformations | Text Analysis & Simple Ciphers
C# Strings & Text Processing

bookSimple Ciphers & String Transformations

When you want to keep a message secret, you can use a cipher to transform the text so that only someone who knows the method can understand it. The process of changing the original message into something unreadable is called encoding, and turning it back into its original form is called decoding. Simple ciphers, like the Caesar cipher, work by shifting each letter in the message by a certain number of positions in the alphabet. These techniques are a great way to learn about both text processing and the basics of cryptography.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031323334
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { string original = "Secret Message: Meet at 8!"; int shift = 3; string encoded = CaesarEncode(original, shift); Console.WriteLine("Original: " + original); Console.WriteLine("Encoded: " + encoded); } public static string CaesarEncode(string input, int shift) { char[] buffer = input.ToCharArray(); for (int i = 0; i < buffer.Length; i++) { char letter = buffer[i]; if (char.IsLetter(letter)) { char offset = char.IsUpper(letter) ? 'A' : 'a'; letter = (char)(((letter - offset + shift) % 26) + offset); buffer[i] = letter; } } return new string(buffer); } } }

The shifting logic in a Caesar cipher works by moving each letter forward in the alphabet by a set number, called the shift. If a letter goes past 'Z' or 'z', it wraps around to the beginning of the alphabet. For example, shifting 'Z' by 1 gives 'A'. It's important to only shift alphabetic characters, so numbers, spaces, and punctuation stay the same and the message structure is preserved. This makes the encoded message easier to decode and keeps non-letter content intact.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { string encoded = "Vhfuhw Phvvdjh: Phhw dw 8!"; int shift = 3; string decoded = CaesarDecode(encoded, shift); Console.WriteLine("Encoded: " + encoded); Console.WriteLine("Decoded: " + decoded); } public static string CaesarDecode(string input, int shift) { // Decoding just shifts in the opposite direction return CaesarEncode(input, 26 - (shift % 26)); } public static string CaesarEncode(string input, int shift) { char[] buffer = input.ToCharArray(); for (int i = 0; i < buffer.Length; i++) { char letter = buffer[i]; if (char.IsLetter(letter)) { char offset = char.IsUpper(letter) ? 'A' : 'a'; letter = (char)(((letter - offset + shift) % 26) + offset); buffer[i] = letter; } } return new string(buffer); } } }

Simple ciphers like the Caesar cipher are easy to implement and can hide a message from someone who is not looking closely. However, they have significant weaknesses. If someone knows or guesses that a Caesar cipher was used, it is very easy to break by trying all possible shifts or analyzing letter frequencies. While simple ciphers are useful for learning and for puzzles, they do not provide strong security for real-world communication.

Note
Definition

Definition: A cipher is a method for transforming text to keep its meaning hidden. The Caesar cipher is named after Julius Caesar, who reportedly used it to send secret messages to his generals. Ciphers have been used throughout history for military, diplomatic, and personal communication.

1234567891011121314151617
// General Caesar cipher algorithm (encoding) public static string CaesarCipher(string input, int shift) { char[] buffer = input.ToCharArray(); for (int i = 0; i < buffer.Length; i++) { char letter = buffer[i]; if (char.IsLetter(letter)) { char offset = char.IsUpper(letter) ? 'A' : 'a'; letter = (char)(((letter - offset + shift) % 26) + offset); buffer[i] = letter; } } return new string(buffer); }
copy

1. What does a Caesar cipher do?

2. Why must non-letter characters be preserved in a cipher?

3. What is the main weakness of a simple Caesar cipher?

question mark

What does a Caesar cipher do?

Select the correct answer

question mark

Why must non-letter characters be preserved in a cipher?

Select the correct answer

question mark

What is the main weakness of a simple Caesar cipher?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 5

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookSimple Ciphers & String Transformations

Deslize para mostrar o menu

When you want to keep a message secret, you can use a cipher to transform the text so that only someone who knows the method can understand it. The process of changing the original message into something unreadable is called encoding, and turning it back into its original form is called decoding. Simple ciphers, like the Caesar cipher, work by shifting each letter in the message by a certain number of positions in the alphabet. These techniques are a great way to learn about both text processing and the basics of cryptography.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031323334
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { string original = "Secret Message: Meet at 8!"; int shift = 3; string encoded = CaesarEncode(original, shift); Console.WriteLine("Original: " + original); Console.WriteLine("Encoded: " + encoded); } public static string CaesarEncode(string input, int shift) { char[] buffer = input.ToCharArray(); for (int i = 0; i < buffer.Length; i++) { char letter = buffer[i]; if (char.IsLetter(letter)) { char offset = char.IsUpper(letter) ? 'A' : 'a'; letter = (char)(((letter - offset + shift) % 26) + offset); buffer[i] = letter; } } return new string(buffer); } } }

The shifting logic in a Caesar cipher works by moving each letter forward in the alphabet by a set number, called the shift. If a letter goes past 'Z' or 'z', it wraps around to the beginning of the alphabet. For example, shifting 'Z' by 1 gives 'A'. It's important to only shift alphabetic characters, so numbers, spaces, and punctuation stay the same and the message structure is preserved. This makes the encoded message easier to decode and keeps non-letter content intact.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { string encoded = "Vhfuhw Phvvdjh: Phhw dw 8!"; int shift = 3; string decoded = CaesarDecode(encoded, shift); Console.WriteLine("Encoded: " + encoded); Console.WriteLine("Decoded: " + decoded); } public static string CaesarDecode(string input, int shift) { // Decoding just shifts in the opposite direction return CaesarEncode(input, 26 - (shift % 26)); } public static string CaesarEncode(string input, int shift) { char[] buffer = input.ToCharArray(); for (int i = 0; i < buffer.Length; i++) { char letter = buffer[i]; if (char.IsLetter(letter)) { char offset = char.IsUpper(letter) ? 'A' : 'a'; letter = (char)(((letter - offset + shift) % 26) + offset); buffer[i] = letter; } } return new string(buffer); } } }

Simple ciphers like the Caesar cipher are easy to implement and can hide a message from someone who is not looking closely. However, they have significant weaknesses. If someone knows or guesses that a Caesar cipher was used, it is very easy to break by trying all possible shifts or analyzing letter frequencies. While simple ciphers are useful for learning and for puzzles, they do not provide strong security for real-world communication.

Note
Definition

Definition: A cipher is a method for transforming text to keep its meaning hidden. The Caesar cipher is named after Julius Caesar, who reportedly used it to send secret messages to his generals. Ciphers have been used throughout history for military, diplomatic, and personal communication.

1234567891011121314151617
// General Caesar cipher algorithm (encoding) public static string CaesarCipher(string input, int shift) { char[] buffer = input.ToCharArray(); for (int i = 0; i < buffer.Length; i++) { char letter = buffer[i]; if (char.IsLetter(letter)) { char offset = char.IsUpper(letter) ? 'A' : 'a'; letter = (char)(((letter - offset + shift) % 26) + offset); buffer[i] = letter; } } return new string(buffer); }
copy

1. What does a Caesar cipher do?

2. Why must non-letter characters be preserved in a cipher?

3. What is the main weakness of a simple Caesar cipher?

question mark

What does a Caesar cipher do?

Select the correct answer

question mark

Why must non-letter characters be preserved in a cipher?

Select the correct answer

question mark

What is the main weakness of a simple Caesar cipher?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 5
some-alt