Example #1
0
    public static void Main()
    {
        ICipher      ciphRef;
        SimpleCipher sc  = new SimpleCipher(); // объект класса простого шифрования
        BitCipher    bit = new BitCipher(27);  // объект с ключом шифрования, 27 - ключ для шифрования
        string       plain;
        string       coded;

        // Сначала переменная ciphRef ссылается на объект
        // класса SimpleCipher (простое шифрование).
        ciphRef = sc;
        Console.WriteLine("Использование простого шифра.");
        plain = "testing";
        coded = ciphRef.encode(plain);
        Console.WriteLine("Зашифрованный текст: " + coded);
        plain = ciphRef.decode(coded);
        Console.WriteLine("Открытый текст: " + plain);
        // Теперь переменная ciphRef refer ссылается на
        // объект класса BitCipher (поразрядное шифрование).
        ciphRef = bit;
        Console.WriteLine(
            "\nИспользование поразрядного шифрования.");
        plain = "testing";
        coded = ciphRef.encode(plain);
        Console.WriteLine("Зашифрованный текст: " + coded);
        plain = ciphRef.decode(coded);
        Console.WriteLine("Открытый текст: " + plain);
    }
Example #2
0
        public void WriteEntryInfo(Stream outStream)
        {
            var entryBytes = GetFileEntryByes();

            SimpleCipher.EncryptBytes(entryBytes);
            outStream.Write(entryBytes, 0, entryBytes.Length); // 文件项内容

            FileEntryLength = entryBytes.Length;
        }
Example #3
0
        public void WriteLengthInfo(Stream outStream)
        {
            var bytes1 = BitConverter.GetBytes(OriginalLength);
            var bytes2 = BitConverter.GetBytes(GZipFileLength);
            var bytes3 = BitConverter.GetBytes(FileEntryLength);


            SimpleCipher.EncryptBytes(bytes1);
            SimpleCipher.EncryptBytes(bytes2);
            SimpleCipher.EncryptBytes(bytes3);


            outStream.Write(bytes1, 0, bytes1.Length);
            outStream.Write(bytes2, 0, bytes2.Length);
            outStream.Write(bytes3, 0, bytes3.Length);
        }
Example #4
0
        public void ReadLengthInfo(Stream srcStream)
        {
            var bytes1 = new byte[4];
            var bytes2 = new byte[4];
            var bytes3 = new byte[4];

            srcStream.Read(bytes1, 0, bytes1.Length);
            srcStream.Read(bytes2, 0, bytes2.Length);
            srcStream.Read(bytes3, 0, bytes3.Length);

            SimpleCipher.EncryptBytes(bytes1);
            SimpleCipher.EncryptBytes(bytes2);
            SimpleCipher.EncryptBytes(bytes3);

            OriginalLength  = BitConverter.ToInt32(bytes1, 0);
            GZipFileLength  = BitConverter.ToInt32(bytes2, 0);
            FileEntryLength = BitConverter.ToInt32(bytes3, 0);
        }
Example #5
0
        public void ReadEntryInfo(Stream srcStream)
        {
            var entryBytes = new byte[FileEntryLength];

            srcStream.Read(entryBytes, 0, entryBytes.Length); // FileEntry 字节
            SimpleCipher.EncryptBytes(entryBytes);

            var entryStr = Encoding.Default.GetString(entryBytes); // 不能用 ASCII, 要处理汉字
            var strArray = entryStr.Split('|');

            var lastWriteTimeticks  = long.Parse(strArray[3]);
            var lastAccessTimeticks = long.Parse(strArray[4]);
            var lastCreateTimeticks = long.Parse(strArray[5]);

            _lastWriteTime  = new DateTime(lastWriteTimeticks);
            _lastAccessTime = new DateTime(lastAccessTimeticks);
            _creationTime   = new DateTime(lastCreateTimeticks);

            FileFullName = strArray[6];
        }
Example #6
0
    public void Substitution_cipher_can_handle_messages_longer_than_the_key()
    {
        var sut = new SimpleCipher("abc");

        Assert.Equal("iboaqcnecbfcr", sut.Encode("iamapandabear"));
    }
Example #7
0
    public void Substitution_cipher_can_wrap_on_decode()
    {
        var sut = new SimpleCipher("abcdefghij");

        Assert.Equal("zzzzzzzzzz", sut.Decode("zabcdefghi"));
    }
Example #8
0
    public void Random_key_cipher_can_encode()
    {
        var sut = new SimpleCipher();

        Assert.Equal(sut.Key.Substring(0, 10), sut.Encode("aaaaaaaaaa"));
    }
Example #9
0
    public void Substitution_cipher_can_double_shift_encode()
    {
        var sut = new SimpleCipher("iamapandabear");

        Assert.Equal("qayaeaagaciai", sut.Encode("iamapandabear"));
    }
Example #10
0
    public void Substitution_cipher_is_reversible_i_e_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
    {
        var sut = new SimpleCipher("abcdefghij");

        Assert.Equal("abcdefghij", sut.Decode(sut.Encode("abcdefghij")));
    }
Example #11
0
    public void Substitution_cipher_can_encode()
    {
        var sut = new SimpleCipher("abcdefghij");

        Assert.Equal("abcdefghij", sut.Encode("aaaaaaaaaa"));
    }
Example #12
0
    public void Random_key_cipher_key_is_made_only_of_lowercase_letters()
    {
        var sut = new SimpleCipher();

        Assert.Matches("^[a-z]+$", sut.Key);
    }
Example #13
0
    public void Random_key_cipher_can_decode()
    {
        var sut = new SimpleCipher();

        Assert.Equal("aaaaaaaaaa", sut.Decode(sut.Key.Substring(0, 10)));
    }