public List <IVertex> Adjacent(IVertex vertex) { List <IVertex> result = new List <IVertex>(); uint[,] state = ((GameVertex)vertex).State; // Найдем пустую клеточку (ноль) int i, j = 0; for (i = 0; i < dimension; i++) { for (j = 0; j < dimension; j++) { if (state[i, j] == 0) { break; } } if (j != dimension && state[i, j] == 0) { break; } } // Сформируем список возможных состояний, в которые можно перейти, передвинув фишку на свободное место GameVertex newVertex; uint[,] newState; if (i != 0) { newState = (uint[, ])state.Clone(); newState[i, j] = newState[i - 1, j]; newState[i - 1, j] = 0; newVertex = new GameVertex(dimension, newState); result.Add(newVertex); } if (j != 0) { newState = (uint[, ])state.Clone(); newState[i, j] = newState[i, j - 1]; newState[i, j - 1] = 0; newVertex = new GameVertex(dimension, newState); result.Add(newVertex); } if (i != dimension - 1) { newState = (uint[, ])state.Clone(); newState[i, j] = newState[i + 1, j]; newState[i + 1, j] = 0; newVertex = new GameVertex(dimension, newState); result.Add(newVertex); } if (j != dimension - 1) { newState = (uint[, ])state.Clone(); newState[i, j] = newState[i, j + 1]; newState[i, j + 1] = 0; newVertex = new GameVertex(dimension, newState); result.Add(newVertex); } return(result); }
} //PrngDotNetBase.Step protected void BlockSeek(ulong blockOffset, uint[,] matrix) { // compute 'blockOffset'-th power of 'matrix' uint[,] matrixa = (uint[, ])(matrix.Clone()); uint[,] matrixb = new uint[55, 55]; uint[,] seekmatrixa = new uint[55, 55]; uint[,] seekmatrixb = new uint[55, 55]; for (int i = 0; i < 55; i++) // initialize seek matrix to identity matrix { seekmatrixa[i, i] = 1; } bool desta = false; // allocate two matrices and switch between them, instead of constantly allocating new matrices bool seekdesta = false; for (; ;) { if ((blockOffset & 1) != 0) { MultiplyMatrix((seekdesta ? seekmatrixa : seekmatrixb), (seekdesta ? seekmatrixb : seekmatrixa), (desta ? matrixb : matrixa)); seekdesta = !seekdesta; } if ((blockOffset >>= 1) == 0) { break; } MultiplyMatrix((desta ? matrixa : matrixb), (desta ? matrixb : matrixa), (desta ? matrixb : matrixa)); // compute square of matrix desta = !desta; } // apply seek matrix to current state vector if (seekdesta) { seekmatrixa = seekmatrixb; // if 'seekmatrixa' is set to be the next destination, then 'seekmatrixb' must contain the product last computed } int[] newstate = new int[55]; for (int i = 0; i < 55; i++) { uint accumulator = 0; for (int k = 0; k < 55; k++) { accumulator += unchecked ((uint)(((ulong)seekmatrixa[i, k] * (ulong)this.SeedArray[1 + k]) % MBIG)); if (accumulator >= MBIG) { accumulator -= MBIG; } } newstate[i] = (int)accumulator; } Array.Copy(newstate, 0, this.SeedArray, 1, 55); } //PrngDotNetBase.BlockSeek
private void init() { if (_key.Length < MIN_KEY_SIZE || _key.Length > MAX_KEY_SIZE) { _isGood = false; Dbg.ERROR_MSG("Blowfish::init: invalid length " + _key.Length); return; } short i; short j; short k; uint data; uint datal; uint datar; P = _P.Clone() as uint[]; S = _S.Clone() as uint[, ]; j = 0; for (i = 0; i < N + 2; ++i) { data = 0x00000000; for (k = 0; k < 4; ++k) { data = (data << 8) | _key[j]; j++; if (j >= _key.Length) { j = 0; } } P[i] = P[i] ^ data; } datal = 0x00000000; datar = 0x00000000; for (i = 0; i < N + 2; i += 2) { encipher(ref datal, ref datar); P[i] = datal; P[i + 1] = datar; } for (i = 0; i < 4; ++i) { for (j = 0; j < 256; j += 2) { encipher(ref datal, ref datar); S[i, j] = datal; S[i, j + 1] = datar; } } _isGood = true; }
/// <summary> /// Constructs and initializes a blowfish instance with the supplied key. /// </summary> /// <param name="key">The key to cipher with.</param> private void InitCryptBlowfish(byte[] key) { if (key == null) { throw new ArgumentNullException("Key is null!"); } isDisposed = false; short i; short j; short k; uint data; uint datal; uint datar; P = _P.Clone() as uint[]; S = _S.Clone() as uint[, ]; j = 0; for (i = 0; i < N + 2; ++i) { data = 0x00000000; for (k = 0; k < 4; ++k) { data = (data << 8) | key[j]; j++; if (j >= key.Length) { j = 0; } } P[i] = P[i] ^ data; } datal = 0x00000000; datar = 0x00000000; for (i = 0; i < N + 2; i += 2) { Encipher(ref datal, ref datar); P[i] = datal; P[i + 1] = datar; } for (i = 0; i < 4; ++i) { for (j = 0; j < 256; j += 2) { Encipher(ref datal, ref datar); S[i, j] = datal; S[i, j + 1] = datar; } } }
private void BlockSeek(ulong blockOffset, uint[,] matrix) { // compute 'absoffset'-th power of 'stepmatrix' uint[,] matrixa = (uint[, ])(matrix.Clone()); uint[,] matrixb = new uint[31, 31]; uint[,] seekmatrixa = new uint[31, 31]; uint[,] seekmatrixb = new uint[31, 31]; for (int i = 0; i < 31; i++) // initialize seek matrix to identity matrix { seekmatrixa[i, i] = 1; } bool desta = false; // allocate two matrices and switch between them, instead of constantly allocating new matrices bool seekdesta = false; for (; ;) { if ((blockOffset & 1) != 0) { MultiplyMatrix((seekdesta ? seekmatrixa : seekmatrixb), (seekdesta ? seekmatrixb : seekmatrixa), (desta ? matrixb : matrixa)); seekdesta = !seekdesta; } if ((blockOffset >>= 1) == 0) { break; } MultiplyMatrix((desta ? matrixa : matrixb), (desta ? matrixb : matrixa), (desta ? matrixb : matrixa)); // compute square of matrix desta = !desta; } // apply seek matrix to current state vector if (seekdesta) { seekmatrixa = seekmatrixb; // if 'seekmatrixa' is set to be the next destination, then 'seekmatrixb' must contain the product last computed } uint[] newstate = new uint[31]; for (int i = 0; i < 31; i++) { uint accumulator = 0; for (int k = 0; k < 31; k++) { accumulator += unchecked ((uint)((ulong)seekmatrixa[i, k] * (ulong)this._State[k])); } newstate[i] = accumulator; } Array.Copy(newstate, 0, this._State, 0, 31); }
public Blowfish(byte[] key) { short i; short j; short k; uint data; uint datal; uint datar; P = _P.Clone() as uint[]; S = _S.Clone() as uint[, ]; //first step key extension j = 0; for (i = 0; i < N + 2; ++i) { data = 0x00000000; for (k = 0; k < 4; ++k) { data = (data << 8) | key[j]; j++; if (j >= key.Length) { j = 0; } } P[i] = P[i] ^ data; } // //update P keys datal = 0x00000000; datar = 0x00000000; for (i = 0; i < N + 2; i += 2) { Encipher(ref datal, ref datar); P[i] = datal; P[i + 1] = datar; } //update s blocks for (i = 0; i < 4; ++i) { for (j = 0; j < 256; j += 2) { Encipher(ref datal, ref datar); S[i, j] = datal; S[i, j + 1] = datar; } } }
public Blowfish(byte[] key, bool modifiedBlowfish) { short i; this.modifiedBlowfish = modifiedBlowfish; P = _P.Clone() as uint[]; S = _S.Clone() as uint[, ]; if (modifiedBlowfish) { S[0, 118] = Bswap(S[0, 118]); } short j = 0; for (i = 0; i < N + 2; ++i) { uint data = 0x00000000; for (short k = 0; k < 4; ++k) { data = (data << 8) | key[j]; j++; if (j >= key.Length) { j = 0; } } P[i] = P[i] ^ data; } uint datal = 0x00000000; uint datar = 0x00000000; for (i = 0; i < N + 2; i += 2) { Encipher(ref datal, ref datar); P[i] = datal; P[i + 1] = datar; } for (i = 0; i < 4; ++i) { for (j = 0; j < 256; j += 2) { Encipher(ref datal, ref datar); S[i, j] = datal; S[i, j + 1] = datar; } } }
public void setMatrix(uint[,] newMatrix) { list = new List <List <Node> >(); matrix = (uint[, ])newMatrix.Clone(); for (uint i = 0; i < rows; i++) { list.Add(new List <Node>()); for (uint j = 0; j < columns; j++) { list[(int)i].Add(new Node(new Tuple <uint, uint>(i, j), valores[matrix[i, j]])); } } }
/// <summary> /// Constructs and initializes a blowfish instance with the supplied key. /// </summary> /// <param name="key">The key to cipher with.</param> public Blowfish(byte[] key) { short i; short j; short k; uint data; uint datal; uint datar; P = (uint[])_P.Clone(); S = (uint[, ])_S.Clone(); j = 0; for (i = 0; i < N + 2; ++i) { data = 0x00000000; for (k = 0; k < 4; ++k) { data |= (uint)(key[j] << (k * 8)); j++; if (j >= key.Length) { j = 0; } } P[i] = P[i] ^ data; } datal = 0x00000000; datar = 0x00000000; for (i = 0; i < N + 2; i += 2) { Encipher(ref datal, ref datar); P[i] = datal; P[i + 1] = datar; } for (i = 0; i < 4; ++i) { for (j = 0; j < 256; j += 2) { Encipher(ref datal, ref datar); S[i, j] = datal; S[i, j + 1] = datar; } } }
/***********************************************************************************************************/ #region ------ Constructor ------ /// <summary> /// Constructs and initializes a blowfish instance with the supplied secret string. /// </summary> /// <param name="secretKey">The secret string to cipher with.</param> public BlowfishSymmetricCipher(string secretKey) { short i, j, k; uint data, datal, datar; // Convert secret string into a byte array. byte[] key = SerializeDeserializeObject.StringToUTF8ByteArray(secretKey); P = _P.Clone() as uint[]; S = _S.Clone() as uint[, ]; j = 0; for (i = 0; i < N + 2; i++) { data = 0x00000000; for (k = 0; k < 4; k++) { data = (data << 8) | key[j]; j++; if (j >= key.Length) { j = 0; } } P[i] = P[i] ^ data; } datal = 0x00000000; datar = 0x00000000; for (i = 0; i < N + 2; i += 2) { Encipher(ref datal, ref datar); P[i] = datal; P[i + 1] = datar; } for (i = 0; i < 4; i++) { for (j = 0; j < 256; j += 2) { Encipher(ref datal, ref datar); S[i, j] = datal; S[i, j + 1] = datar; } } }
public Blowfish(byte[] key) { short i; short j; short k; uint data; uint datal; uint datar; P = _P.Clone() as uint[]; S = _S.Clone() as uint[, ]; j = 0; for (i = 0; i < N + 2; ++i) { data = 0x00000000; for (k = 0; k < 4; ++k) { data = (data << 8) | key[j]; j++; if (j >= key.Length) { j = 0; } } P[i] = P[i] ^ data; } datal = 0x00000000; datar = 0x00000000; for (i = 0; i < N + 2; i += 2) { EncryptPair(ref datal, ref datar); P[i] = datal; P[i + 1] = datar; } for (i = 0; i < 4; ++i) { for (j = 0; j < 256; j += 2) { EncryptPair(ref datal, ref datar); S[i, j] = datal; S[i, j + 1] = datar; } } }
public BlowfishKey(byte[] key) { _p = (uint[])P.Clone(); _s = (uint[, ])S.Clone(); short i = 0; for (short j = 0; j < 18; j += 1) { uint data = 0u; for (short k = 0; k < 4; k += 1) { data = data << 8 | key[i]; i++; if (i >= key.Length) { i = 0; } } _p[j] ^= data; } uint a = 0u; uint b = 0u; for (short j = 0; j < 18; j += 2) { ProcessTableE(ref a, ref b); _p[j + 0] = a; _p[j + 1] = b; } for (short j = 0; j < 4; j += 1) { for (i = 0; i < 256; i += 2) { ProcessTableE(ref a, ref b); _s[j, i + 0] = a; _s[j, i + 1] = b; } } }
/// <summary> /// Initializes a Blowfish with a specific key. /// </summary> /// <param name="key">The key used to expend the algorithm.</param> public Blowfish(ReadOnlySpan <byte> key) { this._pArray = (uint[])PArray.Clone(); this._sBoxes = (uint[, ])SBoxes.Clone(); var keyIndex = 0; for (var i = 0; i < 18; i++) { uint data = 0; for (var j = 0; j < 4; j++) { data = (data << 8) | key[keyIndex++]; if (keyIndex >= key.Length) { keyIndex = 0; } } this._pArray[i] = PArray[i] ^ data; } uint left = 0; uint right = 0; for (var i = 0; i < 18; i += 2) { this.Encrypt(ref left, ref right); this._pArray[i + 0] = left; this._pArray[i + 1] = right; } for (var i = 0; i < 4; i++) { for (var j = 0; j < 256; j += 2) { this.Encrypt(ref left, ref right); this._sBoxes[i, j + 0] = left; this._sBoxes[i, j + 1] = right; } } }
public Blowfish(byte[] key) { int j = 0; uint data_left = 0, data_right = 0; P_box = static_P_box.Clone() as uint[]; S_box = static_S_box.Clone() as uint[, ]; for (int i = 0; i < round + 2; ++i) { uint data = 0; for (int k = 0; k < 4; ++k) { data = (data << 8) | key[j]; j++; if (j >= key.Length) { j = 0; } } P_box[i] = P_box[i] ^ data; } for (int i = 0; i < round + 2; i += 2) { Encipher(ref data_left, ref data_right); P_box[i] = data_left; P_box[i + 1] = data_right; } for (int i = 0; i < 4; ++i) { for (j = 0; j < 256; j += 2) { Encipher(ref data_left, ref data_right); S_box[i, j] = data_left; S_box[i, j + 1] = data_right; } } }
/// <summary> /// Медианная фильтрация /// </summary> /// <param name="inputBitmap">Изображение</param> /// <param name="size">Размер скользящей апертуры (она квадратная)</param> /// <returns>Отфильтрованное изображение</returns> public static unsafe Bitmap Medfilt2D(Bitmap inputBitmap, int size) { int bitHeight = inputBitmap.Height, bitWidth = inputBitmap.Width, edgex = size / 2, edgey = size / 2; uint[,] inputPixelValue = PicToIntArray(inputBitmap), outputPix = new uint[bitWidth, bitHeight]; outputPix = (uint[, ])inputPixelValue.Clone(); DateTime start = DateTime.Now; Parallel.For(edgex, bitWidth - edgex, (x) => { for (int y = edgey; y < bitHeight - edgey; y++) { uint[] window = new uint[size * size]; for (int fx = 0; fx < size; fx++) { for (int fy = 0; fy < size; fy++) { window[fx * size + fy] = inputPixelValue[x + fx - edgex, y + fy - edgey]; } } Sort(window); outputPix[x, y] = window[(window.Length) / 2]; } }); Debug.WriteLine("Medfilt2D: {0}", (DateTime.Now - start)); return(IntArrayToBitmap(outputPix)); }
public IEnumerable <Sample> GetData() { if (reader == null) { available = GetCPUInfo(); if (available) { //rtcloop = new Thread(RTCLoop); //rtcloop.IsBackground = true; //rtcloop.Start(); tscloop = new Thread(TSCLoop); tscloop.IsBackground = true; tscloop.Start(); reader = new Thread(ReadLoop); reader.IsBackground = true; reader.Start(); Thread.Sleep(1100); } } if (available) { var data = samples.Clone() as uint[, ]; int r = 0; uint eax = 0, edx = 0; var temps = new List <double>(); double multiplier = 0; // get max multipler, only works up to max 4 core turbo multiplier for (int i = 0; i < CORE_COUNT; i++) { UIntPtr mask = (UIntPtr)(1 << (i * UIntPtr.Size / 4)); r = Ols.RdmsrPx(0x198, ref eax, ref edx, mask); multiplier += eax & 0xff; } multiplier /= CORE_COUNT; // MSR_PLATFORM_INFO // used for speed calculations r = Ols.Rdmsr(0xce, ref eax, ref edx); var maxnoturboratio = (eax >> 8) & 0xff; var fsb = speed / maxnoturboratio; var rtcfsb = (rtcspeed == 0 ? speed : rtcspeed) / maxnoturboratio; for (int i = 0; i < CORE_COUNT; i++) { double sample = 0; for (int j = 0; j < SAMPLE_RATE; j++) { sample += GetTemp(data[j, i]); } temps.Add(sample / SAMPLE_RATE); } // in Windows we trust... yield return(Sample.Other("CPU Load", pc.NextValue(), true)); yield return(Sample.Other("Multiplier", multiplier, true)); yield return(Sample.Other("FSB (TSC)", fsb, true)); //yield return Sample.Other("FSB (RTC)", rtcfsb, false); for (int i = 0; i < temps.Count; i++) { yield return(Sample.Temperature("CORE " + (i + 1), temps[i], true)); } yield return(Sample.Temperature("CORE AVG", temps.Average(), false)); yield return(Sample.Temperature("CORE MAX", temps.Max(), false)); } }
/// <summary> /// Constructs and initializes a blowfish instance with the supplied key. /// </summary> /// <param name="key">The key to cipher with.</param> public Blowfish(byte[] key) { P = _P.Clone() as uint[]; S = _S.Clone() as uint[, ]; Init(key); }