Example #1
0
        /// <summary>
        /// Creates an Encryptor instance initialized with the specified SEALContext,
        /// public key, and/or secret key.
        /// </summary>
        /// <param name="context">The SEALContext</param>
        /// <param name="publicKey">The public key</param>
        /// <param name="secretKey">The secret key</param>
        /// <exception cref="ArgumentNullException">if context is null</exception>
        /// <exception cref="ArgumentException">if the encryption parameters are not valid</exception>
        /// <exception cref="ArgumentException">if publicKey is not valid</exception>
        /// <exception cref="ArgumentException">if secretKey is not null and not valid</exception>
        public Encryptor(SEALContext context, PublicKey publicKey, SecretKey secretKey = null)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (!context.ParametersSet)
            {
                throw new ArgumentException("Encryption parameters are not set correctly");
            }
            if (!ValCheck.IsValidFor(publicKey, context))
            {
                throw new ArgumentException("Public key is not valid for encryption parameters");
            }

            if (null == secretKey)
            {
                NativeMethods.Encryptor_Create(
                    context.NativePtr, publicKey.NativePtr, IntPtr.Zero, out IntPtr ptr);
                NativePtr = ptr;
            }
            else
            {
                if (!ValCheck.IsValidFor(secretKey, context))
                {
                    throw new ArgumentException("Secret key is not valid for encryption parameters");
                }
                NativeMethods.Encryptor_Create(
                    context.NativePtr, publicKey.NativePtr, secretKey.NativePtr, out IntPtr ptr);
                NativePtr = ptr;
            }
        }
Example #2
0
        /// <summary>
        /// Loads a SecretKey from an input stream overwriting the current SecretKey.
        /// The loaded SecretKey is verified to be valid for the given SEALContext.
        /// </summary>
        /// <param name="context">The SEALContext</param>
        /// <param name="stream">The stream to load the SecretKey from</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <exception cref="ArgumentException">if the context is not set or encryption
        /// parameters are not valid</exception>
        /// <exception cref="ArgumentException">if SecretKey could not be read from
        /// stream or is invalid for the context</exception>
        public void Load(SEALContext context, Stream stream)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            UnsafeLoad(stream);
            if (!ValCheck.IsValidFor(this, context))
            {
                throw new ArgumentException("SecretKey data is invalid for context");
            }
        }