コード例 #1
0
        public ISecurable DecryptContent(ISecurable pStr)
        {
            byte[] sBytes  = pStr.GetBytes();
            byte[] content = StringSecurer.FromBase64BytesToBytes(sBytes);
            var    cms     = new EnvelopedCms();

            cms.Decode(content);
            try
            {
                cms.Decrypt();
            }
            catch (Exception ex)
            {
                throw new ProtectedStringDecryptionException(ex);
            }
            var pts = StringSecurer.FromBase64Bytes(cms.ContentInfo.Content);

            return(pts);
        }
コード例 #2
0
        public static ISecurable ReadFromRegistry(string registryKey, string valueName)
        {
            object     regData = Registry.GetValue(registryKey, valueName, null);
            ISecurable isec    = null;

            if (regData is byte[] bytes)
            {
                isec = StringSecurer.FromBase64Bytes(bytes);
            }
            else if (regData is string strData)
            {
                isec = StringSecurer.FromBase64String(strData);
            }
            else
            {
                throw new InvalidCastException("The resulting registry data cannot be converted to a Securable object.");
            }

            return(isec); // the output is still encrypted...
        }
コード例 #3
0
        public ISecurable EncryptString(ISecurable pts)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("SecurityManager");
            }

            if (this.Certificate == null)
            {
                throw new InvalidOperationException("The encryption certificate is still not set!  Use the 'SetCertificate' method first.");
            }

            var cinfo     = new ContentInfo(pts.GetBytes());
            var cms       = new EnvelopedCms(cinfo);
            var recipient = new CmsRecipient(this.Certificate);

            cms.Encrypt(recipient);
            var base64 = StringSecurer.ToBase64Securable(cms.Encode());

            return(base64);
        }