public IAuthenticatedPublicKeyCryptoContext CreateCrypterInstance(PublicKeyCryptoKeyPair key, PublicKeyCryptoPublicKey publicKey)
        {
            // compute the shared secret
            var sharedSecret = new byte[_beforeNmBytes];
            var result = PlatformInvoke64.crypto_box_beforenm(sharedSecret, publicKey.PlainBytes, key.SecretKeyBytes);

            if (result != 0) throw new CryptographicException("Failed");

            return new AuthenticatedPublicKeyCryptoContext(this, sharedSecret);
        }
        public byte[] VerifyAndDecryptMessage(byte[] encryptedMessage, byte[] nonce, PublicKeyCryptoKeyPair key, PublicKeyCryptoPublicKey senderPublicKey)
        {
            // verify arguments
            if (nonce == null) throw new ArgumentNullException("nonce");
            if (nonce.Length != NonceSize)
                throw new ArgumentOutOfRangeException("nonce", string.Format("Nonce must be {0} bytes long", NonceSize));

            // decrypt
            var paddedMessage = new byte[encryptedMessage.Length];
            var result = PlatformInvoke64.crypto_box_open(paddedMessage, encryptedMessage, encryptedMessage.Length, nonce,
                senderPublicKey.PlainBytes, key.SecretKeyBytes);

            if (result != 0) throw new CryptographicException("Failed");

            var message = new byte[paddedMessage.Length - _zeroBytes];
            Buffer.BlockCopy(paddedMessage, (int)_zeroBytes, message, 0, message.Length);

            return message;
        }
        public byte[] EncryptMessage(byte[] message, byte[] nonce, PublicKeyCryptoKeyPair key, PublicKeyCryptoPublicKey recipientPublicKey)
        {
            // verify arguments
            if (nonce == null) throw new ArgumentNullException("nonce");
            if (nonce.Length != NonceSize)
                throw new ArgumentOutOfRangeException("nonce", string.Format("Nonce must be {0} bytes long", NonceSize));

            // pad the message (prepend zero bytes)
            var paddedMessage = new byte[_zeroBytes + message.Length];
            Buffer.BlockCopy(message, 0, paddedMessage, (int)_zeroBytes, message.Length);

            // encrypt
            var encryptedMessage = new byte[paddedMessage.Length];
            var result = PlatformInvoke64.crypto_box(encryptedMessage, paddedMessage, paddedMessage.Length, nonce, recipientPublicKey.PlainBytes,
                key.SecretKeyBytes);

            if (result != 0) throw new CryptographicException("Failed");

            return encryptedMessage;
        }
Exemplo n.º 4
0
 public PublicKeyCryptoKeyPair(byte[] secretKeyBytes, byte[] publicKey)
 {
     _secretKeyBytes = secretKeyBytes;
     _publicKey = new PublicKeyCryptoPublicKey(publicKey);
 }
        public byte[] VerifyAndDecryptMessage(byte[] encryptedMessage, byte[] nonce, PublicKeyCryptoKeyPair key, PublicKeyCryptoPublicKey senderPublicKey)
        {
            // verify arguments
            if (nonce == null)
            {
                throw new ArgumentNullException("nonce");
            }
            if (nonce.Length != NonceSize)
            {
                throw new ArgumentOutOfRangeException("nonce", string.Format("Nonce must be {0} bytes long", NonceSize));
            }

            // decrypt
            var paddedMessage = new byte[encryptedMessage.Length];
            var result        = PlatformInvoke64.crypto_box_open(paddedMessage, encryptedMessage, encryptedMessage.Length, nonce,
                                                                 senderPublicKey.PlainBytes, key.SecretKeyBytes);

            if (result != 0)
            {
                throw new CryptographicException("Failed");
            }

            var message = new byte[paddedMessage.Length - _zeroBytes];

            Buffer.BlockCopy(paddedMessage, (int)_zeroBytes, message, 0, message.Length);

            return(message);
        }
        public byte[] EncryptMessage(byte[] message, byte[] nonce, PublicKeyCryptoKeyPair key, PublicKeyCryptoPublicKey recipientPublicKey)
        {
            // verify arguments
            if (nonce == null)
            {
                throw new ArgumentNullException("nonce");
            }
            if (nonce.Length != NonceSize)
            {
                throw new ArgumentOutOfRangeException("nonce", string.Format("Nonce must be {0} bytes long", NonceSize));
            }

            // pad the message (prepend zero bytes)
            var paddedMessage = new byte[_zeroBytes + message.Length];

            Buffer.BlockCopy(message, 0, paddedMessage, (int)_zeroBytes, message.Length);

            // encrypt
            var encryptedMessage = new byte[paddedMessage.Length];
            var result           = PlatformInvoke64.crypto_box(encryptedMessage, paddedMessage, paddedMessage.Length, nonce, recipientPublicKey.PlainBytes,
                                                               key.SecretKeyBytes);

            if (result != 0)
            {
                throw new CryptographicException("Failed");
            }

            return(encryptedMessage);
        }
        public IAuthenticatedPublicKeyCryptoContext CreateCrypterInstance(PublicKeyCryptoKeyPair key, PublicKeyCryptoPublicKey publicKey)
        {
            // compute the shared secret
            var sharedSecret = new byte[_beforeNmBytes];
            var result       = PlatformInvoke64.crypto_box_beforenm(sharedSecret, publicKey.PlainBytes, key.SecretKeyBytes);

            if (result != 0)
            {
                throw new CryptographicException("Failed");
            }

            return(new AuthenticatedPublicKeyCryptoContext(this, sharedSecret));
        }
Exemplo n.º 8
0
 public PublicKeyCryptoKeyPair(byte[] secretKeyBytes, byte[] publicKey)
 {
     _secretKeyBytes = secretKeyBytes;
     _publicKey      = new PublicKeyCryptoPublicKey(publicKey);
 }