Example #1
0
        public static byte[] RsaPkcs15Sha_Encrypt(
            ArraySegment <byte> data,
            X509Certificate2 cert,
            SecurityPolicy policy)
        {
            RSA key            = cert.PublicKey.Key as RSA;
            int plainBlockSize = UASecurity.GetPlainBlockSize(cert, UASecurity.UseOaepForSecurityPolicy(policy));

            if ((uint)(data.Count % plainBlockSize) > 0U)
            {
                throw new Exception(string.Format("Input data is not a multiple of block size, {0}/{1}", data.Count, plainBlockSize));
            }

            byte[]       data1        = new byte[plainBlockSize];
            MemoryStream memoryStream = new MemoryStream();

            for (int index = 0; index < data.Count; index += plainBlockSize)
            {
                Array.Copy(data.Array, data.Offset + index, data1, 0, data1.Length);
                byte[] buffer = key.Encrypt(data1, UASecurity.UseOaepForSecurityPolicy(policy));
                memoryStream.Write(buffer, 0, buffer.Length);
            }
            memoryStream.Close();
            return(memoryStream.ToArray());
        }
Example #2
0
        public static byte[] RsaPkcs15Sha_Decrypt(
            ArraySegment <byte> data,
            X509Certificate2 cert,
            RSA rsaPrivate,
            SecurityPolicy policy)
        {
            int cipherTextBlockSize = UASecurity.GetCipherTextBlockSize(cert);

            byte[] buffer1 = new byte[data.Count / cipherTextBlockSize * UASecurity.GetPlainBlockSize(cert, UASecurity.UseOaepForSecurityPolicy(policy))];
            int    length  = rsaPrivate.KeySize / 8;

            UASecurity.GetPlainBlockSize(cert, UASecurity.UseOaepForSecurityPolicy(policy));
            if ((uint)(data.Count % length) > 0U)
            {
                throw new Exception(string.Format("Input data is not a multiple of block size, {0}/{1}", data.Count, length));
            }

            MemoryStream memoryStream = new MemoryStream(buffer1);

            byte[] data1 = new byte[length];
            for (int offset = data.Offset; offset < data.Offset + data.Count; offset += length)
            {
                Array.Copy(data.Array, offset, data1, 0, data1.Length);
                byte[] buffer2 = rsaPrivate.Decrypt(data1, UASecurity.UseOaepForSecurityPolicy(policy));
                memoryStream.Write(buffer2, 0, buffer2.Length);
            }
            memoryStream.Close();
            return(buffer1);
        }
Example #3
0
        public static int CalculatePaddingSize(
            X509Certificate2 cert,
            SecurityPolicy policy,
            int position,
            int sigSize)
        {
            int plainBlockSize = UASecurity.GetPlainBlockSize(cert, UASecurity.UseOaepForSecurityPolicy(policy));
            int num            = plainBlockSize - (position + sigSize) % plainBlockSize;

            if (num < 0)
            {
                throw new Exception();
            }

            return(num);
        }