예제 #1
0
 private PrivateKey GeneratePrivateKey(PrivateComponents privateComponents, PublicKey publicKey)
 {
     var phi = (privateComponents.q - 1) * (privateComponents.p - 1);
     var d = BigIntegerHelper.GetInverse(publicKey.e, phi);
     return new PrivateKey
     {
         d = d,
         r = publicKey.r
     };
 }
예제 #2
0
 private PublicKey GeneratePublicKey(PrivateComponents key)
 {
     var r = key.q * key.p;
     var phi = (key.q - 1) * (key.p - 1);
     var rLength = r.ToByteArray().Length;
     var e = generator.GetPrimeNumber(rLength - 2, rLength - 2, n => n < phi);
     return new PublicKey
     {
         e = e,
         r = r
     };
 }
예제 #3
0
        public byte[] Decrypt(byte[] message, PrivateComponents privateComponents, PublicKey publicKey)
        {
            var phi = (privateComponents.q - 1) * (privateComponents.p - 1);
            var d = BigIntegerHelper.GetInverse(publicKey.e, phi);

            return Decrypt(message, new PrivateKey { d = d, r = publicKey.r });

            //var keySize = publicKey.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, d, publicKey.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, d, publicKey.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;
        }