Invalid character in a Base-64 string when Concatenating and Url encoding a string

Question: I'm trying to write some encryption code that is passed through a Url. For the sake of the issue I've excluded the actual encryption of the data and just shown the code causing the problem.
I take a salt value, convert it to a byte array and then convert that to a base64 string. This string I concatenate to another base64 string (which was previously a byte array).
These two base64 strings are then Url encoded.  Here's my code...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Web;

class RHEncryption
{
    private static readonly Encoding ASCII_ENCODING = new System.Text.ASCIIEncoding();
    private static readonly string SECRET_KEY = "akey";
    private static string md5(string text)
    {
        return BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(ASCII_ENCODING.GetBytes(text))).Replace("-", "").ToLower();
    }

    public string UrlEncodedData;

    public RHEncryption()
    {
        // encryption object
        RijndaelManaged aes192 = new RijndaelManaged();
        aes192.KeySize = 192;
        aes192.BlockSize = 192;
        aes192.Mode = CipherMode.CBC;
        aes192.Key = ASCII_ENCODING.GetBytes(md5(SECRET_KEY));
        aes192.GenerateIV();

        // convert Ivector to base64 for sending
        string base64IV = Convert.ToBase64String(aes192.IV);

        // salt value
        string s = "maryhadalittlelamb";
        string salt = s.Substring(0, 8);
        // convert to byte array
        // and base64 for sending
        byte[] saltBytes = ASCII_ENCODING.GetBytes(salt.TrimEnd('\0'));
        string base64Salt = Convert.ToBase64String(saltBytes);

        //url encode concatenated base64 strings
        UrlEncodedData = HttpUtility.UrlEncode(base64Salt + base64IV, ASCII_ENCODING);
    }

    public string UrlDecodedData()
    {
        // decode the url encode string
        string s = HttpUtility.UrlDecode(UrlEncodedData, ASCII_ENCODING);

        // convert back from base64
        byte[] base64DecodedBytes = null;
        try
        {
            base64DecodedBytes = Convert.FromBase64String(s);
        }
        catch (FormatException e)
        {
            Console.WriteLine(e.Message.ToString());
            Console.ReadLine();
        }
        return s;
    }
}

If I then call the UrlDecodedData method I get a

Invalid character in a Base-64 string

exception.  This is generated because the base64Salt variable contains an invalid character (I'm guessing a line termination) but I can't seem to strip it off.
Update 1
One thing I've found since posting is that if I change the order of string to be encoded no exception is raised.
So changing the following code from
UrlEncodedData = HttpUtility.UrlEncode(base64Salt + base64IV, ASCII_ENCODING);

To
UrlEncodedData = HttpUtility.UrlEncode(base64IV + base64Salt, ASCII_ENCODING);

Seems to work, unfortunately I can't reorder as this information is passed to a third party component.
Comment: It is sheer luck that `base64IV + base64Salt` happened to not break that time. In general catenating Base64 strings may not give you a Base64 string, so don't do it.
Answer: Here is your problem:
UrlEncodedData = HttpUtility.UrlEncode(base64Salt + base64IV, ASCII_ENCODING);
...
string s = HttpUtility.UrlDecode(UrlEncodedData, ASCII_ENCODING);
base64DecodedBytes = Convert.FromBase64String(s);

You are catenating two Base64-encoded strings then trying to Base64-decode the combination. This will only work if your base64Salt is a multiple of six bits, and in your case it is not. Data not such a multiple gets Base64-encoded with a trailing '=' character, catenating it puts the '=' in the middle of the string, which is not a valid Base64 string.
You have to catenate the original data before encoding it. This will only work if you know the original data lengths. If you do not, you will have to encode the salt and vector separately, and catenate them to the URL; e.g. http://yoursite/yourPage?salt=mcMDaw%20VUsZX&iv=VUsZXyg6n61e6SavmMrtAWa0A
Comment: @Dour thanks for replying. Should i concatenate and then convert to base64?
Comment: You can do that if you will always know where to split the decoded byte[] into salt and vector. In your example the salt is the first 8 bytes. Will this always be true forever and cannot ever change? If so, catenate, encode, decode, then split out the first 8 bytes again. If there is any possibility this could ever change, you will have to encode them separately. Or you could add a byte prefix saying how many bytes are salt. E.g. byte[] = { 32, 12, 104, 74, ... } means the first 32 bytes are the salt.
Comment: i'm lucky this time that the byte array offsets are fixed.


Como Criar uma nova Conta (Domínio) via SSH?

Question: Há um tempo atrás fiz uma pergunta - já resolvida - referente a como fazer uma atualização automática remotamente usando PHP (Veja aqui)
Agora eu gostaria de criar via SSH uma nova conta no servidor, a fim de replicar o sistema.
Ou seja, o cliente compraria o sistema no site, e o script PHP criaria (fazendo uso de exec, isto é, comandos SSH, se não houver um modo melhor) uma nova conta para este cliente, instalaria o sistema e deixaria a loja pronta para uso.
O meu servidor usa o sistema WHM/CPanel. Ou seja, ao ser criada a nova conta, o CPanel deve ser instalado nesta nova conta corretamente.
Como fazer isso?
Apenas o comando (ou conjuntos de comandos) SSH para a criação de uma nova conta/domínio já seria o suficiente.
Answer: Criar conta cPanel via terminal
O cPanel vem com um script chamado createacct (Inglês) para criar contas a partir da linha de comandos. Podes fazer uso do mesmo para resolver a questão da criação da conta:
/scripts/createacct example.com utilizador password

O problema com este comando quando queremos utilizar o mesmo a partir de um script é o facto de ele pedir confirmação de algumas coisas aquando da sua execução.
Isto pode ser contornado conforme podemos ler neste tópico (Inglês) do forum da cPanel que nos indica um link para:
Como tornar o /scripts/createacct não-interactivo (Inglês)
PKGRESTORE=1 /scripts/createacct example.com utilizador password

└─────┬────┘ └─────────────────────────┬────────────────────────┘
executar em                     o comando para
  modo não                   criação de uma conta
interactivo                  a partir do terminal

PHP e SSH
Para fazeres uso de SSH através do PHP, assumindo ser a tua ideia, precisas de instalar a seguinte extensão PECL:
PHP SSH2 Installing/Configuring (Inglês)
Depois basta estabeleceres uma ligação e executares o script que contém o comando em cima indicado.
Consulta o manual PHP Secure Shell2 (Inglês) para veres como fazer cada um dos passos. Ia colocar aqui, mas fica extenso e se não conheces, vais sempre precisar de ler o manual.

PHP e SSH (sem instalar a extenção)
Se precisar de te conectar a um servidor via SSH mas não tens possibilidade de instalar a extensão ssh2, a seguinte implementação em PHP puro pode resolver a tua questão:
PHP Secure Communications Library - Net_SSH (Inglês)
Exemplo:
<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.example.com');
if (!$ssh->login('utilizador', 'password')) {
    exit('Login Falhou');
}

echo $ssh->exec('meuScript');
echo $ssh->exec('ls -la');
?>


Attention methods

Question: When using Attention, for example with LSTM (but not necessarily), one can use the following methods to attend:

MLP: $ug(W^1v+W^2q)$
dot product: $v \cdot q$
biaffine transform: $v^TWq$

($v$ is the attended vector which is used for prediction, $q$ is the query vector determining the weighted sum, used in the encoded input vectors attention)
What are the pros/cons of using each attention method? Or, what are the practical differences in the result?
Comment: You introduced the new tag [attention], can you please write a tag wiki?
Answer: I think the main difference is just the amount of model capacity / complexity you want -- you'd probably start with dot product, and then pick increasingly elaborate methods if that doesn't fit properly.
Another consideration is the "type" of the vectors. If both $v$ and $q$ are word/sentence embeddings, a dot product seems straightforward, but what if $v$ is a sentence embedding and $q$ is the encoded form of an image? Then, taking the dot product makes less sense, since you are saying that the components of $v$ should somehow correspond to the same components in $q$.
Of course, this can also be a good thing if you are trying to come up with a single embedding space for multimodal inputs. So depending on this, you may or may not try to use biaffine attention, which doesn't assume $v$ and $q$ are the same type.
As for bi-affine vs MLP, note that bi-affine allows the easy modeling of quadratic interactions between $v$ and $q$, whereas MLP is more "linear". (See this related question on quadratic neurons in NTNs: What is the "expressive power" of the composition function in a Recursive Neural Tensor Network?)

Attention methods


My floyd rose is badly sharp on return

Question: If I pull up on my whammy bar and then go to tune, the entire guitar is sharp. II have an Edge zero 2 on an Ibanez js140... any help?
Answer: This is one of the minor hazards with floating tremolos - they will go out of tune.
There are a few partial solutions:

locking nut - this stops the string slipping through the nut and catching sharp or flat
knife edge trem fulcrum - reduces the possibility of slipping out of tune
zero point trem - springs both directions try to return the trem to an in tune position

They are all partial solutions though. In reality the best way to get back to perfect tuning is also learn the right behaviours. If you have pulled the string sharp, dip the trem slightly to flatten the rest position, and do the opposite if you have dive bombed.
That said, Ibanez trems should be pretty good, so you may want to check the locking nut is locked, there are no flat spots on the knife edges and the tension springs aren't catching on anything.

My floyd rose is badly sharp on return


Why is my bandwidth averaging 3 kbps on a 10 mbps device?

Question: I have a server with a 100 mbps port and connection that I rate limited to 10 mbps/full duplex because I am only paying for a 10 mbps connection and there are steep overage charges.  Ever since then, the bandwidth has absolutely plummeted.  I turned off autoneg because I otherwise would not be able to limit the rate to 10 from 100 because the network is operating so much faster.  I need a way to average 10 mbps on a server with multiple users logging in on that 100 mbps port and connection.  Can this be done without having these issues?
Answer: You have created a duplex mismatch. Your port is set to 10/Full and your ISP's port is set to 10/Half.
Either set your port to half-duplex (highly undesirable) or contact your ISP and ask them to set their port to 10/Full.
According to Cisco's official training material for their most basic networking certification:

When one device has disabled autonegotiation, and the other device uses autonegotiation, the device using autonegotiation chooses the default duplex setting based on the current speed. The defaults are as follows:
• If the speed is not known, use 10 Mbps, half duplex.
• If the speed is somehow known to be 10 or 100 Mbps, default to use half duplex.
• If the speed is somehow known to be 1000 Mbps, default to use full duplex.
Comment: By the way, I would strongly recommend reading the book that I referenced and getting your CCENT certification (at minimum). Having this foundational knowledge makes all of your real-world networking problems much easier to understand.


Android Send and Receive Data simultaneously

Question: I am trying to receive data on my android phone, from the server. I do not understand why it always crashes... Please help!
seekbarbrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekbarbrightness, int progress, boolean b) {
            if(b==true) {
                seekbarvalue = seekbarbrightness.getProgress();
                multiplier = (double) seekbarvalue / 100;
                finallumens = (int) (multiplier * LoginActivity.enterlumens);
                tblumens.setText(String.valueOf(finallumens) + " Lumens");
                if (LoginActivity.getSocket() != null) {
                    try {

                        InputStreamReader streamReader = new InputStreamReader(LoginActivity.getSocket().getInputStream());
                        BufferedReader reader = new BufferedReader(streamReader);
                        String str = reader.readLine();
                        tbvolts.setText("Voltage: "+str);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                else{
                    Toast.makeText(MainActivity.this, "NOT connected To Socket, please disconnect and reconnect!", Toast.LENGTH_SHORT).show();
                }
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

If I remove the receive data codes, and place the following code in the try and catch block, it works.
LoginActivity.getSocket().getOutputStream().write(String.valueOf(multiplier).getBytes());

I'm not sure what I am doing wrong. But below is the error I get in logcat and my app just crashes.
Exception in MessageQueue callback: handleReceiveCallback.
android.os.NetworkOnMainThreadException

This is my Async Method I created. Is it correct? And any ideas how I could extract the str value in my AsyncTask and place it into a Textview? I can only Printout the value but not setting the text with it.
class ReceiveData extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... args) {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(LoginActivity.getSocket().getInputStream()));
            in.readLine();
            String str = in.readLine();
            return str;

        } catch (IOException e) {
            e.printStackTrace();
            return "fail";
        }

    }

Comment: https://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception
Comment: @DiegoTorresMilano I dont get it.. When i switch my code to sending out bytes, it works. But only when receiving data it doesnt..
Comment: You should not do _any_ network operation on main thread
Answer: Long running  task puts more load on main thread that's why NetworkOnMainThreadException  showing, main thread is always used to perform small task like update UI etc.  To Perform long running task use AsyncTask.
https://stackoverflow.com/a/6343299/4741746
This link show you how to use  AsyncTask. in android
and after completing task if you want to update UI than  you can use onPostExecute method of AsyncTask
also look at this post it help you -https://stackoverflow.com/a/12650928/4741746
Comment: Another method I tried was placing the network operation into another thread, and calling thread.start() in the main thread. It works this way, but is there any practice to this?
Comment: There are multiple ways like Handler , Thread , etc but best is AsyncTask
Comment: Most of use library like retrofit , volley, loopj async library etc  in which retrofit is best
Comment: but ill suggest you to start with async task than move forward for best practice
Comment: Ahh I see. It seems my thread can only run twice, and anymore than that it will crash. May I ask for the AsyncTask, in my case, I only need to put the inputstream codes into the onPostExecute? My socket creation were created in another activity.
Comment: Yes you can use async task in this situation and put that inpute stream code in do in background method not in post execute  ....do in background  method work in background thread where as onPostExecute work on main thread   refer -https://developer.android.com/reference/android/os/AsyncTask.html
Comment: If you don't mind, I added the AsyncTask I created. I am not very sure if it is correct. Please have a look.
Comment: Befor using Async Task please read all the details how it work - developer.android.com/reference/android/os/AsyncTask.html


Help with subgroup contain

Question: Let $G$ a group, $H$ a subgroup of $G$ and define $N=\bigcap_{x\in G}{xHx^{-1}}$, show that for all $a\in G$ $N\subseteq aNa^{-1}$.
Any hint or advice?
Answer: Hint: It is enough to prove that $a^{-1}Na \subseteq N$ for all $a\in G$.
Solution:

$N \subseteq xHx^{-1}$ for all $x \in G$. Therefore, $a^{-1}Na \subseteq a^{-1}xHx^{-1}a=y H y^{-1}$ for $y=a^{-1}x$. As $x$ runs through $G$, so does $y=a^{-1}x$. Therefore, $a^{-1}Na  \subseteq \bigcap_{y\in G}{yHy^{-1}} = N$.

Help with subgroup contain


In jQuery, why do I get unrecognized expression error when select option value is less than 1?

Question: I am trying to dynamically populate select options using values (getval = 2, 1, 0.5, 0.25). Why am I getting unrecognized expression error when the value is less than 1:
html:
<option name={{ $papersizeItem['value'] }} >{{ $papersizeItem['title'] }}</option>

jquery:
var getval = 0.5;
$("#s2q14_1 option[name="+getval+"]").prop('selected', true);

Expected output:
<option name=0.5 selected>some name</option>

Error:
Unrecognized expression: #s2q14_1 option[name=0.5]

Comment: The window to the code is too narrow. Please paste a detailed code, context and the expected output
Answer: The issue is because the . in the getval value is being interpreted as a class selector. You cannot have a class selector within an attribute selector, so the sizzle engine returns that error.
To fix this wrap the attribute value in quotes so there is no ambiguity:
$('#s2q14_1 option[name="' + getval + '"]').prop('selected', true);

Alternative using a template literal:
$(`#s2q14_1 option[name="${getval}"]`).prop('selected', true);

It's also worth noting that name is not a valid attribute for the option element. I'd suggest finding an alternative, such as a data attribute.
Answer: let getVal = 2.5;
 $(`#s2q14_1 option[name="${getVal}"]`).prop('selected', true);


Are t tests of coefficients in multiple regression post hoc tests?

Question: In multiple regression, if a global F test is significant, then are t tests (or Wald tests) for the coefficients considered to be multiple comparisons and post hoc tests and should they be adjusted?
Answer: General discussion of post hoc analyses

post hoc analyses are typically contrasted with a priori, where post hoc analyses are, in some sense, performed after seeing the data, and a priori analyses are set out before seeing the data. In this sense the terms map on closely to the concepts of exploratory versus confirmatory data analysis.
Arguably, the prototypical post hoc analysis involves comparing all pairwise comparisons of means in the context of an ANOVA where there are three or more levels to one of the independent variables. SPSS, for example, has a post hoc button specifically designed for running this form of analysis.
However, while pairwise comparisons of group means is the prototypical example, any analysis that involves running statistical tests after seeing the data could be described as post hoc.
When thinking about the general label "post hoc", it is helpful to think about its purpose. It is used as a caution about using standard inferential procedures on the problem at hand either because many significance tests are being run, or even if only a few significance tests are being performed, there are many that could have been performed had the data been different. Thus, post hoc statistical tests typically try to make adjustments to how inferences are made in order to control Type I error rates over both the analyses that are performed and those that could have been performed had the data been different.

The term "post hoc" applied to regression coefficients

Thus, the examination of individual significance tests for regression coefficients could be labelled post hoc if the examination is done after seeing the data. Also, if you wanted to control your family wise error rate and you saw the set of significance tests associated with the regression coefficients as a family, then you could apply something like a Bonferroni adjustment to the individual significance tests. That said, many researchers might interpret an overall model more holistically, and it is ultimately up to the reader how they choose to interpret the significance tests associated with individual coefficients.
I would not describe the examination of a set of significance tests for regression coefficients as "multiple comparisons", because I think the term "comparisons" pertains more to comparing group means. I'd prefer a term like "multiple significance tests".
Answer: It depends on your state of knowledge before the study. If you went into the study knowing that there were variables that were highly likely to be "significant" predictors of the outcome, and you were mostly interested in the influence of some new measure, say "M1", then the F-test is basically uninteresting and you are primarily interested in the relationship of M1 to the outcome. Then the relationship's features and statistical measures of credibility are not "post hoc", ... they are the primary study question.


Lie bracket of $\mathfrak{so}(3)$

Question: I know that for $\mathfrak{so}(3)=\mathcal{L}(SO(3))$, the set of $3\times 3$ real antisymmetric matrices, we can define a basis
$$T^1=\begin{pmatrix}0&0&0\\ 0&0&-1\\ 0&1&0\end{pmatrix}\quad
T^2=\begin{pmatrix}0&0&1\\ 0&0&0\\ -1&0&0\end{pmatrix}\quad
T^3=\begin{pmatrix}0&-1&0\\ 1&0&0\\ 0&0&0\end{pmatrix}$$
such that
$$\left(T^a\right)_{bc}=-\epsilon_{abc}$$
and the Lie bracket is then
$$\left[T^a,T^b\right]=\epsilon_{abc}T^c$$
I can see that it works by explicitly putting in the matrices $T^a$ defined above, but how would one manipulate the $\epsilon$ symbols to show it formally?
Comment: @DietrichBurde Yes, but what I meant was, using only the formal definition of $T^a$ in terms of $\epsilon_{abc}$, how do I compute the commutator $[T^a,T^b]$?
Comment: @DietrichBurde Again, yes, that's the first step in expanding the commutator. The $T^a$ are then given by $(T^a)_{bc}=-\epsilon_{abc}$, i.e. $a$ is the index of the generator and $b,c$ are matrix indices (row, column). So the commutator can be expressed as some $\epsilon\epsilon-\epsilon\epsilon$. My question is then, what is the proper way of writing down the indices, and how do I simplify this expression?
Answer: Is this are you looking for?
(using Einstein convention)
$$\left[T^a,T^b\right]^i_j=(T^aT^b )_{ij}-(T^bT^a )_{ij}$$
$$=(T^a)^i_p(T^b)^p_j-(T^b)^i_q(T^a)^q_j=\epsilon_{aip}\epsilon_{bpj}-\epsilon_{biq}\epsilon_{aqj}.$$
And then whatever you need to do you probably will need to use the following identity:
$$\epsilon_{ijk}\epsilon_{mnk}=\delta_{im}\delta_{jn}-\delta_{in}\delta_{jm}$$
EDIT:
Applying the formula to the previous one (if I'm not mistaking) you get
$$\left[T^a,T^b\right]^i_j=\delta_{aj}\delta_{bi}-\delta_{bj}\delta_{ai}$$
Which indeed is
$$[T^a,T^b]=\epsilon_{abc}T^c$$
Comment: Doesn't that evaluate to zero?
Comment: Sorry you're right, now should be ok and much clearer
Comment: I think it's still zero ^^
Comment: Check a little bit better... you just have to plug in correctly the formula I gave you with the deltas in the epsilon formula. If you still have a problem write me...

Lie bracket of $\mathfrak{so}(3)$


How can I remove a white space above my header using PHP?

Question: complete newbie here.
I am trying to edit a plugin page on my Wordpress website. There are three plugin pages, two look the same, but the other has a white space above the text.
This is the code on the page with the white space:

<?php echo sprintf( __( 'Here You Can View & Shortlist Your Videos', 'auditions' ), $client_name ); ?>

In comparison, this is the code on one of the pages that is styled nicely, without the white space:

<?php echo sprintf( __( 'This is Your Shortlist Where You Can Download Your Videos', 'auditions' ), $client_name ); ?>

I would be ever so grateful for any help on this!
Thank you.
Comment: Thank you so much @Samuel !
----
I tried taking the line height out, but it didn't help... please could you copy and paste the code without the line height but in case I misunderstood.. Thanks so much
Comment: The different is `line-height` attribute in your first example. If you remove it; it should remove the white space above the text.
Comment: Try using inspect element with your browser and try removing some css in real time in it and with that you can probably identify what's causing the spacing issue.
Comment: Thank you @JesusErwinSuarez ! I will give it a shot...
Answer: Try using inspect element with your browser and try removing some css in real time in it and with that you can probably identify what's causing the spacing issue