在Microsoft Visual Studio中使用C#编程实现凯撒密码的加密和解密,可以使用以下代码:

using System;

class Program
{
    static void Main()
    {
        string plaintext = "HELLO"; // 明文
        int key = 3; // 密钥

        string encryptedText = Encrypt(plaintext, key);
        Console.WriteLine("加密后的密文: " + encryptedText);

        string decryptedText = Decrypt(encryptedText, key);
        Console.WriteLine("解密后的明文: " + decryptedText);

        Console.ReadKey();
    }

    static string Encrypt(string text, int key)
    {
        string encryptedText = "";

        foreach (char c in text)
        {
            if (char.IsLetter(c))
            {
                char encryptedChar = (char)(((int)c + key - 65) % 26 + 65); // 根据ASCII码进行加密
                encryptedText += encryptedChar;
            }
            else
            {
                encryptedText += c;
            }
        }

        return encryptedText;
    }

    static string Decrypt(string text, int key)
    {
        string decryptedText = "";

        foreach (char c in text)
        {
            if (char.IsLetter(c))
            {
                char decryptedChar = (char)(((int)c - key - 65 + 26) % 26 + 65); // 根据ASCII码进行解密
                decryptedText += decryptedChar;
            }
            else
            {
                decryptedText += c;
            }
        }

        return decryptedText;
    }
}

这段代码通过Encrypt函数实现了凯撒密码的加密,Decrypt函数实现了凯撒密码的解密。在Main函数中,分别对明文进行加密和解密操作,并打印结果。

凯撒密码加密解密用Microsoft visual studio中c#怎么编程用for循环

原文地址: https://www.cveoy.top/t/topic/i7Ro 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录