C#中的加密:Enigma算法 [英] Encryption in C#: Enigma Algorithm

查看:93
本文介绍了C#中的加密:Enigma算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

几周前我找到了这个页面,你们互相帮助的感觉很酷.我来自瑞士,有一个很大的问题/要求:)

我想对一个ENIGMA进行编程,但只能使用三个转子.我知道转子如何工作,但我不知道如何编程.我在此页面上的C#中找到了此ENIGMA,并尝试查看他如何编程,但这太困难了.有没有更简单的方法来编程算法?在接下来的几分钟或几小时内,我将编写到目前为止编写的代码.

我的目标是使用自己的算法在C#中创建一个加密器.是否没有示例向您显示如何开始?

感谢您的关注!

Hey Guys!

I found this page some weeks ago and It''s just cool how you help each other. I''m from Switzerland and I have a big question/request :)

I wanted to programm an ENIGMA, but only with its three rotors. I know how the rotors work but I don''t have any idea how to programm that. I found this ENIGMA in C# on this page and tried to see how he programmed it, but it was way too difficult. Isn''t there an easier way to programm the algorithm? In the following minutes or hours I''ll put the code I programmed until now.

My goal is to make an Encryptor in C# with my own algorithms. Isn''t there a sample which shows you, how you have to start?

Thanks for your attention!

推荐答案

检查此链接:

有关C#中的Enigma仿真器的Codeproject文章 [
check this link:

Codeproject-article about Enigma emulator in C#[^]


您好,
这是我对JoeilG的解决方案,
首先在
下的CodeProject中浏览 学习区,
解决方案中心,
通用编程,
密码学安全性
您有一整章专门讨论密码学.

http://www.codeproject.com/KB/security/ [
Hello ,
this is my solution for JoeilG ,
first browse in the CodeProject under
Learning Zones ,
Solutions Center ,
General Programming,
Cryptograpfy & Security
you have a whole chapter devoted to cryptography.

http://www.codeproject.com/KB/security/[^]

Here is sample of code for Encryption of small text messages :

Program.cs

/*
 * Created by Perić Željko
 * periczeljkosmederevo@yahoo.com
 * IDE Sharp Develop C#
 * Date: 28.11.2011
 * Time: 11:20
 * 
 * This is a simple console application for encryption of short
 * text messages (up to 255 caracters).It is based on
 * increasing of generic ASCII code of letters in text messages
 * by value of Rotation_key enterd trough keyboard.
 * 
 * example : Rotation_key = 1
 * 			 Message = ABC (ASCII : 65 , 66 , 67)
 * 			 Encrypted = BCD (ASCII : 66 ,67 , 68)
 * 
 * Since the value of the ASCII code is limited ,
 * so the value of the Rotation_key should be limited
 * it is up to you to find min and max values.
 * When you do that , given key name will be justified.
 * 
 * !!! The program is not final because it has no part to check
 *  	what exactly user had entered on the keyboard
 * 		there should be try...parse...catch part.
 * 		it is up to you.
 * 		It allso can be modifed to load .txt document,
 * 		encrypt text from it and then send it trough mail,
 * 		for example.
 * 		There is only part of program for encryption,
 * 		the other part for decryption you shuld write 
 * 		yourself.Look at this code it should be easy.
 *               You should change only one key charcter in program
 *               for Encryption to become program for Decryption :))
 * !!!
 * 
 * Good luck and happy learning of C#
 * 
 */
 
using System;

namespace Encryption
{
	class Program
	{
		public static void Main(string[] args)
		{
			
			//
			// variables declaration
			//
			string Original_Message = "";
			string Encrypted_Message = "";
			string Original_Message_Letter_String = "";
			string Encrypted_Message_Letter_String = "";
			string Rotation_key_string = "";
			
			char Original_Message_Letter_Char = ' ';
			char Encrypted_Message_Letter_Char = ' ';
			
			int Letter_code = 0;
			int Rotation_key_number = 0;
			int Message_Lenght = 0;
			int Counter = 0;
			
			//
			// Hello to user
			//
			Console.Title = "Encryption program";
			Console.SetWindowSize(80,25);
			Console.Clear();
			Console.ForegroundColor = ConsoleColor.Green;
			Console.WriteLine("-------------------------------------------------");
			Console.WriteLine("    Program for encryption of text messages");
			Console.WriteLine("-------------------------------------------------");
			Console.WriteLine("");
			
			//
			// Get text message from console
			//
			Console.WriteLine("Type your message here :");
			Console.WriteLine("------------------------");
			Original_Message = Console.ReadLine();
			Console.WriteLine("");
			
			//
			// Get value of rotation key from console
			// and transfer it to number
			//
			Console.WriteLine("Value of rotation key :");
			Console.WriteLine("------------------------");
			Rotation_key_string = Console.ReadLine();
			Rotation_key_number = int.Parse(Rotation_key_string);
			Console.WriteLine("------------------------");
			Console.WriteLine("");
			
			//
			// Encrypt message
			//
			Message_Lenght = Original_Message.Length;
			Counter = 0;
			while(Counter < Message_Lenght)
			{
				Original_Message_Letter_String = Original_Message.Substring(Counter,1);
				Original_Message_Letter_Char = char.Parse(Original_Message_Letter_String);
				
				Letter_code = Original_Message_Letter_Char;
				Letter_code = Letter_code + Rotation_key_number;
				
				Encrypted_Message_Letter_Char = (char) Letter_code;
				Encrypted_Message_Letter_String =  Encrypted_Message_Letter_Char.ToString();
				
				Encrypted_Message = Encrypted_Message + Encrypted_Message_Letter_String;
				Counter = Counter + 1;
			}
			
			//
			// Write encrypted message on console
			//
			Console.WriteLine("Encrypted message");
			Console.WriteLine("-------------------------------------------------");
			Console.WriteLine(Encrypted_Message);
			Console.WriteLine("-------------------------------------------------");
			Console.WriteLine("");
			
			//
			// End of program
			//
			Console.Write("Press any key to continue . . . ");
			Console.ReadKey(true);
		}
	}
}



这是用于解密小文本消息的代码示例:

Program.cs



Here is sample of code for Decryption of small text messages :

Program.cs

/*
 * Created by Perić Željko
 * periczeljkosmederevo@yahoo.com
 * IDE Sharp Develop C#
 * Date: 28.11.2011
 * Time: 11:20

 * 
 * This is a simple console application for decryption of short
 * text messages (up to 255 caracters) encrypted by program Encryption.
 * Program Encryption is based on
 * increasing of generic ASCII code of letters in text messages
 * by value of Rotation_key enterd trough keyboard.
 * 
 * example : Rotation_key = 1
 *              Original message = ABC (ASCII : 65 , 66 , 67)
 *              Encrypted message = BCD (ASCII : 66 ,67 , 68)
 * 
 * so program decryption works in oposite direction
 * 
 * example : Rotation_key = 1
 *              Encrypted message = BCD (ASCII : 65 , 66 , 67)
 *              Decrypted or Original message= ABC (ASCII : 66 ,67 , 68)
 * 
 * Since the value of the ASCII code is limited ,
 * so the value of the Rotation_key should be limited
 * it is up to you to find min and max values.
 * When you do that , given key name will be justified.
 * 
 * !!! The program is not final because it has no part to check
 *      what exactly user had entered on the keyboard
 *         there should be try...parse...catch part.
 *         it is up to you.
 *         It allso can be modifed to load .txt document,
 *         decrypt text from it and then send it trough mail,
 *         for example.
 *
 * !!!
 * 
 * Good luck and happy learning of C#
 * 
 */

using System;

namespace Decryption
{
	class Program
	{
		public static void Main(string[] args)
		{
		//
            // variables declaration
            //
            string Encrypted_Message = "";
            string Decrypted_Message = "";
            string Encrypted_Message_Letter_String = "";
            string Decrypted_Message_Letter_String = "";
            string Rotation_key_string = "";
            
            char Encrypted_Message_Letter_Char = ' ';
            char Decrypted_Message_Letter_Char = ' ';
            
            int Letter_code = 0;
            int Rotation_key_number = 0;
            int Message_Lenght = 0;
            int Counter = 0;
            
            //
            // Hello to user
            //
            Console.Title = "Decryption program";
            Console.SetWindowSize(80,25);
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("-------------------------------------------------");
            Console.WriteLine("    Program for decryption of text messages");
            Console.WriteLine("-------------------------------------------------");
            Console.WriteLine("");
            
            //
            // Get text message from console
            //
            Console.WriteLine("Type your Encrypted message here :");
            Console.WriteLine("-----------------------------------");
            Encrypted_Message = Console.ReadLine();
            Console.WriteLine("");
            
            //
            // Get value of rotation key from console
            // and transfer it to number
            //
            Console.WriteLine("Value of rotation key :");
            Console.WriteLine("------------------------");
            Rotation_key_string = Console.ReadLine();
            Rotation_key_number = int.Parse(Rotation_key_string);
            Console.WriteLine("------------------------");
            Console.WriteLine("");
            
            //
            // Encrypt message
            //
            Message_Lenght = Encrypted_Message.Length;
            Counter = 0;
            while(Counter < Message_Lenght)
            {
                Encrypted_Message_Letter_String = Encrypted_Message.Substring(Counter,1);
                Encrypted_Message_Letter_Char = char.Parse(Encrypted_Message_Letter_String);
                
                Letter_code = Encrypted_Message_Letter_Char;
                Letter_code = Letter_code - Rotation_key_number;
                
                Decrypted_Message_Letter_Char = (char) Letter_code;
                Decrypted_Message_Letter_String =  Decrypted_Message_Letter_Char.ToString();
                
                Decrypted_Message = Decrypted_Message + Decrypted_Message_Letter_String;
                Counter = Counter + 1;
            }
            
            //
            // Write Decrypted message on console
            //
            Console.WriteLine("Decrypted message - Original text");
            Console.WriteLine("-------------------------------------------------");
            Console.WriteLine(Decrypted_Message);
            Console.WriteLine("-------------------------------------------------");
            Console.WriteLine("");
            
            //
            // End of program
            //
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}



如果您使用的是IDE SharpDevelop,则可以在以下地址找到这些程序的完整解决方案:

加密 [



If you are using IDE SharpDevelop you can find complete solutions for those programs on this adress :

Encryption[^]

All the best,
Perić Željko


这篇关于C#中的加密:Enigma算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆