分类:InfoSec

SYSTEM CLEANNER

SYSTEM CLEANNER

        private void initScanFolder()
        {
            //SYSTEM
            //PREFETCH
            scanFolder.Add("Prefetch Files", Env.GetFolderPath(Spi.Windows) + @"\Prefetch");
            //ICON CACHE
            scanFolder.Add("Icon caches", Env.GetFolderPath(Spi.UserProfile) + @"\AppData\Local\IconCache.db");
            //SYSTEM AUTOUPDATE
            scanFolder.Add("System autoupdate files", Env.GetFolderPath(Spi.Windows) + @"\SoftwareDistribution\Download");
            //SYSTEM LOG FILES
            //AUTOUPDATE HOTFIX LOG
            scanFolder.Add("Autoupdate hotfix log", Env.GetFolderPath(Spi.Windows) + @"\SoftwareDistribution\DataStore\Logs");
            //ERROR REPORT
            scanFolder.Add("Error report", @"C:\ProgramData\Microsoft\Windows\WER\ReportArchive");
            //LOGS
            scanFolder.Add("Windows LOG", Env.GetFolderPath(Spi.Windows) + @"\Logs");

            //GOOGLE CHROME
            //BROSWE HISTORY
            scanFolder.Add("Chrome history", Env.GetFolderPath(Spi.UserProfile) + @"\AppData\Local\Google\Chrome\User Data\Default\JumpListIconsOld");
            //CACHE
            scanFolder.Add("Chrome cache", Env.GetFolderPath(Spi.UserProfile) + @"\AppData\Local\Google\Chrome\User Data\Default\Cache");
            //COOKIES
            scanFolder.Add("Chrome cookies", Env.GetFolderPath(Spi.UserProfile) + @"\AppData\Local\Google\Chrome\User Data\Default\Local Storage");

            //INTERNET EXPLORER
            //CACHE
            scanFolder.Add("IE cache", Env.GetFolderPath(Spi.UserProfile) + @"\AppData\Local\Microsoft\Windows\INetCache\IE");
            scanFolder.Add("IE_low cache ", Env.GetFolderPath(Spi.UserProfile) + @"\AppData\Local\Microsoft\Windows\INetCache\Low\IE");
            //COOKIES
            scanFolder.Add("IE cookies", Env.GetFolderPath(Spi.UserProfile) + @"\AppData\Local\Microsoft\Windows\INetCookies");
            scanFolder.Add("IE DOM files", Env.GetFolderPath(Spi.UserProfile) + @"\AppData\Local\Microsoft\Internet Explorer\DOMStore");
        }
[IS]OTP

[IS]OTP

One Time Password
基于AES+TIMESTAMP

using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

public class Test
{
    public static void Main()
    {
        const string SharedKey = "12341234123412341234123412341234";
        byte[] PrevInput = Encoding.UTF8.GetBytes(SharedKey);
        while (true)
        {
            var TimeStamp = GetTimeStamp();
            PrevInput = GetNewInput(PrevInput, Encoding.UTF8.GetBytes(TimeStamp + TimeStamp + TimeStamp + TimeStamp.Substring(8, 2)));
            PrevInput = AESEncrypt(PrevInput, SharedKey);
            Console.WriteLine("Pass: " + Convert.ToBase64String(PrevInput, 0, 32));
            Console.WriteLine("Time: " + TimeStamp);
            Console.ReadKey();
        }
    }

    public static byte[] AESEncrypt(byte[] Data, String Key)
    {
        MemoryStream mStream = new MemoryStream();
        RijndaelManaged aes = new RijndaelManaged();
        byte[] plainBytes = Data;
        Byte[] bKey = new Byte[32];
        Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
        aes.Mode = CipherMode.ECB;
        aes.Padding = PaddingMode.PKCS7;
        aes.KeySize = 128;
        aes.Key = bKey;
        CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
        try
        {
            cryptoStream.Write(plainBytes, 0, plainBytes.Length);
            cryptoStream.FlushFinalBlock();
            return mStream.ToArray();
        }
        finally
        {
            cryptoStream.Close();
            mStream.Close();
            aes.Clear();
        }
    }

    public static byte[] GetNewInput(byte[] PrevInput,byte[] TimeStamp)
    {
        byte[] NewInput = new byte[32];
        for (int i = 0; i < 32; i++)
        {
            NewInput[i] = (byte)(PrevInput[i] | TimeStamp[i]);
        }
        return NewInput;
    }

    public static string GetTimeStamp()
    {
        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
        return Convert.ToInt64(ts.TotalSeconds).ToString();
    }
}
SBOX_TESTTOOL

SBOX_TESTTOOL

最近给一个比赛写的,不过有不少地方没处理好感觉

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

namespace TestProject
{
    class TestTool
    {
        private UInt32[] SBOX;
        public TestTool(UInt32[] Sbox)
        {
            SBOX = new UInt32[256];
            try
            {
                Sbox.CopyTo(SBOX, 0);
            }
            catch
            {
                throw new Exception("Data Copy Error");
            }
        }
        public class NonlinearDegree
        {
            UInt32[] Table = new UInt32[256];
            void SetTable()
            {
                UInt32 flag;
                for (UInt32 i = 0; i < 256; i++)
                {
                    flag = i;
                    for (int j = 0; j < 8; j++)
                    {
                        if (((flag >> j) & 0x01) != 0)
                        {
                            Table[i]++;
                        }
                    }
                }
            }
            public UInt32 Test(UInt32[] SBOX)
            {
                UInt32 U, A, x, b, temp, nonlin = 256;
                SetTable();
                for (U = 1; U < 256; U++)
                {
                    for (A = 1; A < 256; A++)
                    {
                        for (b = 0; b < 2; b++)
                        {
                            temp = 0;
                            for (x = 0; x < 256; x++)
                            {
                                if (((Table[x & A] + b) % 2) != (Table[SBOX[x] & U] % 2))
                                {
                                    temp++;
                                }
                                
                            }
                            if (nonlin > temp)
                            {
                                nonlin = temp;
                            }
                        }

                    }
                }
                return nonlin;
            }
        }
        public class FixedPoint
        {
            public UInt32 Test(UInt32[] SBOX)
            {
                UInt32 count = 0;
                for (int i = 0; i < 256; i++)
                {
                    if (SBOX[i] == i)
                    {
                        count++;
                    }
                }
                return count;
            }
        }
        public class DifferentialUniform
        {
            UInt32[,] DTable = new UInt32[256, 256];
            public UInt32 Test(UInt32[] SBOX)
            {
                UInt32 differ = 0;
                DTable[0, 0] = 256;
                for (int alpha = 1; alpha < 256; alpha++)
                {
                    for (int x = 0; x < 256; x++)
                        DTable[alpha, SBOX[x] ^ SBOX[x ^ alpha]]++;
                    for (int x = 0; x < 256; x++)
                        if (DTable[alpha, x] > differ)
                            differ = DTable[alpha, x];
                }
                return differ;
            }
        }
        public class AlgebraDegree
        {
            UInt32[] Table = new UInt32[256];
            void SetTable()
            {
                int flag;
                for (int i = 0; i < 256; i++)
                {
                    flag = i;
                    for (int j = 0; j < 8; j++)
                    {
                        if (((flag >> j) & 0x01) != 0)
                        {
                            Table[i]++;
                        }
                    }
                }
            }
            public UInt32 Test(UInt32[] SBOX)
            {
                UInt32 Degree = 0;
                int[,] function = new int[8, 256];
                SetTable();
                for (int flag = 0; flag < 8; flag++)
                {
                    int[] term = new int[256];
                    UInt32 degree = 0;
                    for (int X = 0; X < 256; X++)
                    {
                        for (int a = 0; a < 256; a++)
                        {
                            if (((SBOX[a] >> flag) & 0x01) != 0)
                            {
                                int temp = 0x01;
                                for (int x = 0; x < 8; x++)
                                {
                                    if (((~X >> x) & 0x01) != 0)
                                    {
                                        if (((a >> x) & 0x01) != 0)
                                        {
                                            temp = 0x00;
                                            break;
                                        }
                                    }
                                }
                                term[X] ^= temp;
                            }
                        }
                        function[flag, X] = term[X];
                    }
                    for (int i = 0; i < 256; i++)
                    {
                        if ((function[flag, i] == 0x01) && (Table[i] > degree))
                            degree = Table[i];
                        if (Degree < degree)
                            Degree = degree;
                    }
                }
                return Degree;
            }

        }
        public class IterativePeriod
        {
            public UInt32[] Test(UInt32[] SBOX)
            {
                UInt32 temp;
                int[] js = new int[8];
                UInt32[] ot = new UInt32[256];
                for (int i = 0; i < 256; i++)
                {
                    temp = SBOX[i];
                    do
                    {
                        temp = SBOX[temp];
                        ot[i]++;
                    } while (temp != SBOX[i]);
                }
                return ot;
            }
        }
        public class Robust
        {
            UInt32[,] DTable = new UInt32[256, 256];
            public double Test(UInt32[] SBOX)
            {
                int num = 0;
                UInt32 max = 0;
                double robustdegree = 0;
                DTable[0, 0] = 256;
                for (int alpha = 1; alpha < 256; alpha++)
                {
                    for (int x = 0; x < 256; x++)
                        DTable[alpha, SBOX[x] ^ SBOX[x ^ alpha]]++;
                    for (int x = 0; x < 256; x++)
                        if (DTable[alpha, x] > max)
                            max = DTable[alpha, x];
                }
                for (int i = 0; i < 256; i++)
                    if (DTable[i, 0] != 0) num++;
                robustdegree = ((1 - (num + 0.0) / 256) * (1 - (max + 0.0) / 256));
                return robustdegree;
            }           
        }
        public class Balance
        {
            UInt32[] Table = new UInt32[256];
            void SetTable()
            {
                int flag;
                for (int i = 0; i < 256; i++)
                {
                    flag = i;
                    for (int j = 0; j < 8; j++)
                    {
                        if (((flag >> j) & 0x01) != 0)
                        {
                            Table[i]++;
                        }
                    }
                }
            }
            public bool Test(UInt32[] SBOX)
            {
                SetTable();
                int testnum = 0xFF;
                UInt32 count = 0;
                for (int i = 0; i < 256; i++)
                    count += Table[testnum & SBOX[i]];
                if (count - 1024 != 0) return false;
                else return true;
            }
        }
        public class StrictAvalancheCriterion
        {
            public double[,] sac = new double[8, 8];
            public void Test(UInt32[] SBOX)
            {
                int n = 8;
                int i, j, k, bt, nN, sx, sy;
                //double[,] sac = new double[8, 8];
                int[] js = new int[8];
                bt = 1;
                nN = (int)Math.Pow(2, n);
                for (i = 0; i < n; i++)
                {
                    for (k = 0; k < n; k++) js[k] = 0;
                    for (j = 0; j < nN; j++)
                    {
                        sx = (int)SBOX[j];
                        sy = (int)SBOX[j ^ bt];
                        for (k = 0; k < n; k++) if ((sx >> k & 1) != (sy >> k & 1)) js[k]++;
                    }
                    bt <<= 1;
                    for (k = 0; k < n; k++) sac[i, k] = (js[k] * 1.0) / 256;
                }
            } 
        }
    }
}

维吉尼亚密码分析 (密码学/c++)

维吉尼亚密码分析 (密码学/c++)


英文字母使用频率统计\(p\):
从A到Z:0.082,0.015,0.028,0.043,0.127,0.022,0.020,0.061,0.070,0.002,0.008,0.040,0.024,0.067,0.079,0.019,0.001,0.060,0.063,0.091,0.028,0.010,0.023,0.001,0.020,0.001

密钥长度推测:重合指数法
设定\(X = {x_1}{x_2}..{x_n}\)是一个含有\(n\)个字符的字符串,\(X\)的重合指数记为\({I_c}(x)\),定义为\(X\)中两个随机元素相同的概率。
假设\({f_0},{f_1},..,{f_{25}}\)分别表示字母\(A,B,..,Z\)在串X中出现的频率,则共有\(\left( {\begin{array}{*{20}{c}}n\\2\end{array}} \right)\)种方法来选取\(X\)中任两个元素。对于每一个\(i \in [0,25]\),有\[{I_c}(x) =\frac{{\sum\limits_{i = 0}^{25} {\left( {\begin{array}{*{20}{c}}{{f_i}}\\2\end{array}} \right)} }}{{\left( {\begin{array}{*{20}{c}}n\\2\end{array}} \right)}} = \frac{{\sum\limits_{i = 0}^{25} {{f_i}({f_i} – 1)} }}{{n(n – 1)}}\]
对于文本中字母\(A,B,..,Z\)出现的期望概率\({p_0},{p_1},..,{p_{25}}\),我们对于\({I_c}(x)\)的期望为\[{I_c}(x) \approx \sum\limits_{i = 0}^{25} {p_i^2} = 0.065\]
假设一个维吉尼亚密码加密的密文串为\({Y_0},{Y_1},..,{Y_{25}}\),那么我们假设密钥长度为\(m\),则有
\[\begin{array}{l}{Y_1} = {y_1}{y_{m + 1}}{y_{2m + 1}}…\\{Y_2} = {y_2}{y_{m + 2}}{y_{2m + 2}}…\\{Y_3} = {y_3}{y_{m + 3}}{y_{2m + 3}}…\\…\end{array}\]
如果密钥长度为\(m\),那么每一个\({I_c}(x)\)都应大约为0.065,如果\(m\)不是密钥长度,则这个串的\({I_c}(x)\)则应接近随机串。
对于一个完全随机串\[{I_c}(x) \approx 26{(1/26)^2} = 0.038\]

密钥推测:
知道密钥长度之后,我们还是将密文分为\(m\)列,定义数值\[{M_g} = \sum\limits_{i = 0}^{25} {{p_i}{f_{_i + g}}} \]
对于\(g \in [0,25]\),最接近0.065的\({M_g}\)可以被认定为当前列数的密钥。

注意:
必须在一定数据规模下才可以,小规模数据极易出错。
以上分析证明了维吉尼亚密码在惟密文攻击下不是安全的。

#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;

double cp[] = { 0.082,0.015,0.028,0.043,0.127,0.022,0.020,0.061,0.070,0.002,0.008,0.040,0.024,0.067,0.079,0.019,0.001,0.060,0.063,0.091,0.028,0.010,0.023,0.001,0.020,0.001 };

char encode(char, char);
char decode(char, char);
int findlength(string);
int *crack(int, string);

int main()
{
	string key;
	string cipherText = "";
	cin >> key;
	int l = key.length();
	char inChar;
	int p = 0;
	while (cin >> inChar)
	{
		if (isalpha(inChar))
		{
			cipherText += encode(inChar, key[p % l]);
			p++;		
		}
	}
	cout << cipherText;
	l = findlength(cipherText);
	int *k = crack(l, cipherText);
	cout << "Key length: " << l << endl;
	cout << "Key: ";
	for (int i = 0; i < l; i++)
	{
		cout << (char)(k[i] + 'A');
	}
	cout << endl;
	for (int i = 0; i < cipherText.length(); i++)
	{
		cout << decode(cipherText[i], (k[i % l] + 'A'));
	}
	cout << endl;
	system("pause");
	return 0;
}

char encode(char c, char k)
{
	char upperC = toupper(c) - 65;
	char upperK = toupper(k) - 65;
	char code = (upperC + upperK) % 26 + 65;
	return code;
}

char decode(char c, char k)
{
	char code;
	char upperC = toupper(c) - 65;
	char upperK = toupper(k) - 65;
	if (upperC - upperK > 0)
		code = ((upperC - upperK) % 26) + 65;
	else code = (26 - abs(upperC - upperK))%26+65;
	return code;
}

int findlength(string c)
{
	int count = c.length();
	bool find = false;
	int m = 1;
	while (!find)
	{
		int ln = count / m + 1;
		char *k = new char[ln];
		double f = 0;
		for (int i = 0; i < m; i++)
		{
			int co = 0;
			for (int j = 0; j < ln; j++)
			{
				if ((j * m + i) < (c.length() - 1))
				{
					k[j] = c[j * m + i]; 
					co++;
				}
			}
			int cco = 0;
			int eq = 0;
			for (int j = 0; j < co - 2; j++)
			{
				for (int g = j + 1; g < co - 1; g++)
				{
					if (j == g) continue;
					if (k[j] == k[g]) eq++;
					cco++;
				}
			}
			double ff = (double)eq / (cco);
			f += ff;
		}
		f /= m;
		if (abs(f - 0.065) < 0.005) find = true;
		m++;
	}
	return m - 1;
}
int *crack(int m, string c)
{
	int *key = new int[m];
	int count = c.length();
	int ln = count / m + 1;
	char *k = new char[ln];
	double f = 0;
	for (int i = 0; i < m; i++)
	{
		int co = 0;
		double f[26] = { 0 };
		for (int j = 0; j < ln; j++)
		{
			if ((j * m + i) < (c.length() - 1))
			{
				k[j] = c[j * m + i];
				f[k[j] - 'A'] += 1;
				co++;
			}
		}
		
		for (int j = 0; j < 26; j++)
		{
			f[j] /= (double)co;
		}
		double Mgi[26] = { 0 };
		for (int g = 0; g < 26; g++)
		{
			double Mg = 0;
			for (int j = 0; j < 26; j++)
			{
				Mg += cp[j] * f[(j + g) % 26];
			}
			Mgi[g] = Mg;
		}
		int p = 0;
		double min = INT_MAX;
		for (int j = 0; j < 26; j++)
		{
			if (abs(Mgi[j] - 0.065) < min)
			{
				min = abs(Mgi[j] - 0.065);
				p = j;
			}
		}
		key[i] = p;
	}
	return key;
}
简单文字分割V1

简单文字分割V1

已知BUG:当字体中某些左右结构的字和标点符号有中间缝隙时会导致切成两块,速度慢
记得要

using AForge.Imaging.Filters;

稍微写了下,因为是遍历像素点,所以效率感觉挺低的。

        public static List<Bitmap> filter(Bitmap segb)
        {
            var list = new List<Bitmap>();
            int[] rows = new int[segb.Height];
            for (int y = 0; y < segb.Height; y++)
            {
                for (int x = 0; x < segb.Width; x++)
                {
                    var pixel = segb.GetPixel(x, y);
                    if (pixel.R == 0) rows[y] = ++rows[y];
                }
            }
            int bottom = 0, top = 0;
            for (int y = 0; y < rows.Length; y++)
            {
                if (rows[y] > 0 || (y + 1 < rows.Length && rows[y + 1] > 0))
                {
                    if (top == 0) top = y;
                    else bottom = y;
                }
                else
                {
                    if ((top > 0 || bottom > 0) && bottom - top > 0)
                    {
                        Crop corp = new Crop(new Rectangle(0, top, segb.Width, bottom - top + 1));
                        var small = corp.Apply(segb);
                        list.Add(small);
                    }
                    top = bottom = 0;
                }
            }



            var corplist = new List<Bitmap>();
            foreach (var b in list)
            {
                int[] cols = new int[b.Width];
                for (int x = 0; x < b.Width; x++)
                {
                    for (int y = 0; y < b.Height; y++)
                    {
                        var pixel = b.GetPixel(x, y);
                        if (pixel.R == 0) cols[x] = ++cols[x];
                    }
                }
                int left = 0, right = 0;
                for (int i = 0; i < cols.Length; i++)
                {
                    if (cols[i] > 0 || (i + 1 < cols.Length && cols[i + 1] > 0))
                    {
                        if (left == 0) left = i;
                        else right = i;

                    }
                    else
                    {
                        if ((left > 0 || right > 0))
                        {
                            Crop corp = new Crop(new Rectangle(left, 0, right - left + 1, b.Height));
                            var small = corp.Apply(b);
                            corplist.Add(small);
                        }

                        left = right = 0;
                    }
                }
            }
            return corplist;
        }