Пример #1
0
 public bool CheckSign(byte[] file, byte[] signature, PrivateKey key)
 {
     var decryptedHash = rsa.Decrypt(signature, key);
     var calculatedHash = hasher.CalcHash(file);
     return Enumerable.SequenceEqual(decryptedHash, calculatedHash);
 }
Пример #2
0
        public byte[] Decrypt(byte[] message, PrivateKey key)
        {
            var keySize = key.r.ToByteArray().Length;
            var encryptedBlockSize = keySize;
            var sourceBlockSize = keySize - 2;

            var numberBlocks = message.Length / encryptedBlockSize;
            if (message.Length % encryptedBlockSize != 0)
                throw new ArgumentException("Wrong size of encrypted message");

            var decryptedMessage = new byte[numberBlocks * sourceBlockSize];

            Parallel.For(0, numberBlocks - 1, (i) =>
            //for (var i = 0; i < numberBlocks; i++)
            {
                var part = new byte[encryptedBlockSize];

                Array.Copy(message, i * encryptedBlockSize, part, 0, encryptedBlockSize);
                var Mi = new BigInteger(part);
                var mi = BigInteger.ModPow(Mi, key.d, key.r);
                Array.Copy(mi.ToByteArray(), 0, decryptedMessage, i * sourceBlockSize, sourceBlockSize);
            });

            var lastPart = new byte[encryptedBlockSize];
            Array.Copy(message, (numberBlocks - 1) * encryptedBlockSize, lastPart, 0, encryptedBlockSize);
            var lMi = new BigInteger(lastPart);
            var lmi = BigInteger.ModPow(lMi, key.d, key.r);
            var lmiBytes = lmi.ToByteArray();
            var countBytes = lmiBytes.Length;
            Array.Copy(lmi.ToByteArray(), 0, decryptedMessage, (numberBlocks - 1) * sourceBlockSize, countBytes);

            var realSize = sourceBlockSize * (numberBlocks - 1) + countBytes;
            var trimMessage = new byte[realSize];
            Array.Copy(decryptedMessage, 0, trimMessage, 0, realSize);

            return trimMessage;
        }