示例#1
0
        public void decryptFile(string inputFileName, string outputFileName, string privateKey, bool OAEPpadding)
        {
            using (FileStream stReader = new FileStream(inputFileName, FileMode.Open, FileAccess.Read))
            {
                RSACryptoServiceProvider rsa = PEM.ImportPrivateKey(privateKey);
                int blocksize = rsa.KeySize / 8;
                if (ProgressConfigure != null)
                {
                    ProgressConfigure(stReader.Length / blocksize);
                }

                using (FileStream stWriter = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    byte[] buff = new byte[blocksize];
                    while (stReader.Read(buff, 0, buff.Length) > 0)
                    {
                        byte[] buffDencrypted = rsa.Decrypt(buff, OAEPpadding);
                        stWriter.Write(buffDencrypted, 0, buffDencrypted.Length);
                        buff = new byte[blocksize];

                        if (ProgressChanged != null)
                        {
                            ProgressChanged();
                        }
                    }
                }
            }
        }
示例#2
0
        public static string decrypt(string privateKey, string Hash, bool OAEPpadding)
        {
            string decryptedText         = string.Empty;
            RSACryptoServiceProvider rsa = PEM.ImportPrivateKey(privateKey);

            byte[] inputBytes     = Convert.FromBase64String(Hash);
            int    room           = rsa.KeySize / 8;
            int    remainingBytes = inputBytes.Length;
            int    startIndex     = 0;

            while (remainingBytes > 0)
            {
                int lengthToDencrypt;
                if (remainingBytes > room)
                {
                    lengthToDencrypt = room;
                }
                else
                {
                    lengthToDencrypt = remainingBytes;
                }

                byte[] block = new byte[lengthToDencrypt];
                Array.Copy(inputBytes, startIndex, block, 0, lengthToDencrypt);
                decryptedText  += Encoding.ASCII.GetString(rsa.Decrypt(block, OAEPpadding));
                startIndex     += lengthToDencrypt;
                remainingBytes -= lengthToDencrypt;
            }
            return(decryptedText);
        }
示例#3
0
        public static Key getPublicKeyfromPrivateKey(string privateKey)
        {
            RSACryptoServiceProvider rsa = PEM.ImportPrivateKey(privateKey);

            return(new Key()
            {
                privateKey = privateKey, publicKey = PEM.ExportPublicKey(rsa), keySize = rsa.KeySize
            });
        }
示例#4
0
        public static Key ReadPrivateKey(Stream FileStream)
        {
            Key myKey = new Key();

            using (FileStream)
            {
                using (StreamReader reader = new StreamReader(FileStream))
                {
                    myKey.privateKey = reader.ReadToEnd();
                    RSACryptoServiceProvider csp = PEM.ImportPrivateKey(myKey.privateKey);
                    myKey.publicKey = PEM.ExportPublicKey(PEM.ImportPrivateKey(myKey.privateKey));
                    myKey.keySize   = csp.KeySize;
                }
            }
            return(myKey);
        }
示例#5
0
 public static bool validate(string key, bool isprivate)
 {
     try
     {
         RSACryptoServiceProvider rsa;
         if (isprivate)
         {
             rsa = PEM.ImportPrivateKey(key);
         }
         else
         {
             rsa = PEM.ImportPublicKey(key);
         }
         return(true);
     }
     catch
     {
         return(false);
     }
 }
示例#6
0
        public static string Sign(string privateKey, string inputString, string HashAlgo)
        {
            RSACryptoServiceProvider rsa = PEM.ImportPrivateKey(privateKey);

            return(Convert.ToBase64String(rsa.SignData(Encoding.ASCII.GetBytes(inputString), getHashingAlgorithm(HashAlgo))));
        }