Ejemplo n.º 1
0
        public byte[] Decrypt(Encrypted e)
        {
            if (e.KeyId != Id)
            {
                throw ExceptionFactory.MakeInvalidOperation("RSA key: mismatching key id");
            }

            if (e.Scheme != EncryptionScheme)
            {
                throw ExceptionFactory.MakeInvalidOperation(
                          string.Format("RSA key: invalid encryption scheme '{0}', expected '{1}'",
                                        e.Scheme,
                                        EncryptionScheme));
            }

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(Parameters);
                return(rsa.Decrypt(e.Ciphertext, true));
            }
        }
Ejemplo n.º 2
0
        public static byte[] DecodeHex(this string s)
        {
            if (s.Length % 2 != 0)
            {
                throw ExceptionFactory.MakeInvalidOperation(
                          "DecodeHex: input length must be multiple of 2");
            }

            var bytes = new byte[s.Length / 2];

            for (var i = 0; i < s.Length / 2; ++i)
            {
                var b = 0;
                for (var j = 0; j < 2; ++j)
                {
                    b <<= 4;
                    var c = char.ToLower(s[i * 2 + j]);
                    if (c >= '0' && c <= '9')
                    {
                        b |= c - '0';
                    }
                    else if (c >= 'a' && c <= 'f')
                    {
                        b |= c - 'a' + 10;
                    }
                    else
                    {
                        throw ExceptionFactory.MakeInvalidOperation(
                                  "DecodeHex: input contains invalid characters");
                    }
                }

                bytes[i] = (byte)b;
            }

            return(bytes);
        }