LoginSignup
1
2

More than 3 years have passed since last update.

C#で簡易的な暗号化ソフトを作る

Posted at

C#で簡易的な暗号化ソフトを作ってみました。

今回は、暗号化したい元の文章を、Keyを使って暗号化、復号化する簡易的なソフトです。
Xorを使っています。

Cを暗号化されたデータ、Oを元のデータ、Kをキーだとすると

C = O xor K

で、暗号化し

O = C xor K

で復号化できます。
このXorの性質を利用した暗号化です。

プログラム

クラスにまとめたので、ライブラリにしたり、埋め込んだりできます。

code.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ango
{
    class code
    {
        public string encrypt(string original,string key)
        {
            string str = original;
            byte[] arr = Encoding.GetEncoding("Shift_JIS").GetBytes(str);
            string str2 = key;
            byte[] arr2 = Encoding.GetEncoding("Shift_JIS").GetBytes(str2);

            string out_string = "";
            int str2_index = 0;
            int str2_next_index;

            for (int i = 0; i < arr.Length; i++)
            {
                str2_next_index = str2_index + 1;
                if (str2_next_index >= arr2.Length) str2_next_index = 0;

                arr2[str2_index] = (byte)((arr2[str2_index] + arr2[str2_next_index]) ^ arr2[str2_next_index]);
                arr2[str2_next_index] = (byte)(arr2[str2_index] + arr2[str2_next_index]);

                out_string = out_string + (arr[i] ^ arr2[str2_index]).ToString() + ",";

                str2_index++;
                if (str2_index >= arr2.Length) str2_index = 0;
            }

            return out_string;
        }

        public string decrypt(string cryptogram,string key)
        {
            string[] arr = cryptogram.Split(',');
            byte[] byte_arr = new byte[arr.Length];

            string str2 = key;
            byte[] arr2 = Encoding.GetEncoding("Shift_JIS").GetBytes(str2);

            int str2_index = 0;
            int str2_next_index;

            for (int i = 0; i < arr.Length - 1; i++)
            {
                str2_next_index = str2_index + 1;
                if (str2_next_index >= arr2.Length) str2_next_index = 0;

                arr2[str2_index] = (byte)((arr2[str2_index] + arr2[str2_next_index]) ^ arr2[str2_next_index]);
                arr2[str2_next_index] = (byte)(arr2[str2_index] + arr2[str2_next_index]);

                byte_arr[i] = Convert.ToByte(arr[i]);
                byte_arr[i] = (byte)(byte_arr[i] ^ arr2[str2_index]);

                str2_index++;
                if (str2_index >= arr2.Length) str2_index = 0;
            }


            string out_str = Encoding.GetEncoding("Shift_JIS").GetString(byte_arr);

            return out_str;
        }
    }
}

解説

関数encryptは暗号化します。
引数に、元の文章、キーを渡します。
暗号化されたものがテキスト形式で返されます。

まず、元の文章と、キーを、StringからByte[]にします。
2つのByte[]を1バイトずつずらしながらXorします。

これだけだと、キーが推測されやすいらしいので、キーを常に変更しています。

                arr2[str2_index] = (byte)((arr2[str2_index] + arr2[str2_next_index]) ^ arr2[str2_next_index]);
                arr2[str2_next_index] = (byte)(arr2[str2_index] + arr2[str2_next_index]);

関数decryptは復号化する関数です。
引数に、暗号化されたテキスト、キーを渡します。
復号化された文章が返されます。

後は、暗号化の時と、ほぼ同じです。

使用例

zzzzzzzzz.png

dddddddd.png

読者に挑戦?

今回、暗号化のアルゴリズムを知られても、キーを推測して復号化できないと思います。
どこからそんな自信が来るのか分かりませんが、キーを常に変更しているため、非常に困難だと思います。(ド素人感覚)
もし、できそうだと思う方がいらしたら、以下の暗号化されたものを解読してください。

ちなみにキーは日本語も使えます。

135,168,181,167,173,90,27,45,183,0,220,38,143,140,145,252,201,93,82,34,227,101,95,234,150,153,76,118,94,33,1,93,20,74,41,192,184,27,143,147,163,76,95,170,199,9,222,177,50,45,233,232,244,49,173,221,229,158,220,65,132,217,75,38,221,150,20,152,149,2,64,172,236,151,155,98,78,22,237,202,107,47,40,205,107,160,67,237,166,173,235,46,249,188,154,9,241,140,222,211,209,101,136,149,175,224,113,128,100,233,145,140,153,104,201,230,10,165,140,115,161,124,85,76,206,185,67,158,7,201,153,124,44,29,200,170,132,29,145,4,228,15,230,139,19,78,71,91,193,93,225,154,
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2