예제 #1
0
        public static byte[] EncryptClient(byte[] plaintextBytes, byte[] serverAuthPubKey, byte[] serverSessionPublicKey, byte[] singleClientPrivateKey)
        {
            if (plaintextBytes == null)
            {
                throw new ArgumentNullException(nameof(plaintextBytes));
            }
            if (serverAuthPubKey == null)
            {
                throw new ArgumentNullException(nameof(serverAuthPubKey));
            }
            if (serverSessionPublicKey == null)
            {
                throw new ArgumentNullException(nameof(serverSessionPublicKey));
            }
            if (singleClientPrivateKey == null)
            {
                throw new ArgumentNullException(nameof(singleClientPrivateKey));
            }

            var hashedSharedSecretBytes = VCL.Instance().CalculateAndHashSharedSecret(singleClientPrivateKey, serverSessionPublicKey);
            var authSecretBytes         = VCL.Instance().CalculateAndHashSharedSecret(singleClientPrivateKey, serverAuthPubKey);
            var keyMaterial64           = ToKeyMaterial64(hashedSharedSecretBytes, authSecretBytes);

            CipherV2 cipher = VCL.Instance().BinaryEncrypt(new Clearbytes(plaintextBytes), keyMaterial64, new RoundsExponent(RoundsExponent.DontMakeRounds), VCL.GetContext()).Result;

            return(VCL.Instance().BinaryEncodeXDSSec(cipher, VCL.GetContext()).Result);
        }
예제 #2
0
        public static byte[] Decrypt(byte[] cipherV2Bytes, byte[] publicKey, byte[] privateKey, byte[] privateAuthKey)
        {
            var hashedSharedSecretBytes = VCL.Instance().CalculateAndHashSharedSecret(privateKey, publicKey);
            var authSecretBytes         = VCL.Instance().CalculateAndHashSharedSecret(privateAuthKey, publicKey);

            var keyMaterial64 = ToKeyMaterial64(hashedSharedSecretBytes, authSecretBytes);

            CipherV2 cipherV2FromClient    = VCL.Instance().BinaryDecodeXDSSec(cipherV2Bytes, VCL.GetContext()).Result;
            var      binaryDecryptResponse = VCL.Instance().BinaryDecrypt(cipherV2FromClient, keyMaterial64, VCL.GetContext());

            if (!binaryDecryptResponse.IsSuccess && binaryDecryptResponse.Error == LocalizableStrings.MsgPasswordError)
            {
                return(null);
            }
            return(binaryDecryptResponse.Result.GetBytes());
        }