Using actix-web's web::Bytes, I can get the payload from a form submission. It is a simple form with one input named username and another input named text. The raw bytes stream looks like this
b"username=User&text=%22Hello%2C+World%21%22"
The content in the text submitted is simply "Hello, World!".
Without using serde, what methods can I use to convert the above %22Hello%2C+World%21%22 into the intended string "Hello, World!"?
Your data is encoded as application/x-www-form-urlencoded. you can use a crate like form-urlencoded to parse it.
Example:
let x = form_urlencoded::parse("%23first=%25try%25");
println!("{:?}", x); // [("#first", "%try%")]
I have a strange issue that I am finding when decoding base64 strings in Powershell.
$url = "https://*******.search.windows.net/indexes/azureblob-index/docs?api-version=2019-05-06&search=*"
$headers = #{
"api-version" = "2019-05-06"
"Content-Type" = "application/json"
"api-key" = "**********"
}
$result = Invoke-webrequest -Uri $url -Headers $headers -Method Get | ConvertFrom-Json
$values = $result.value
foreach ($value in $values)
{
$path = $value.metadata_storage_path
$bloburl = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($path))
$bloburl
}
The **** are hiding sensitive information, obviously.
So I am trying to return an Azure blob URL which is encoded. It managed to decode, however, it returns and error and seems to add a character to the end of the URL - making it out of sync with what the base64 decoding is expecting.
Result looks like this -
https://*******.blob.core.windows.net/files/REPORTS/*****/SEISMIC_ACQUISITION/ACQUISITION_REPORT_APPENDIX4_DAY_LOGS_JD_201.pdf5
It is always a number 5 that is added to the end of the string.
Any ideas as to what is going on here?
This is the full code - there is nothing else going on.
All that is happening is sending a search query to Azure search and returning the urls of blobs/documents which match the search query. Azure returns a base64 string and I want to decode that to plain readable text.
The error is:
Exception calling "FromBase64String" with "1" argument(s): "Invalid length for a Base-64 char array or string."
At line:25 char:9
+ $bloburl = [System.Text.Encoding]::UTF8.GetString([System.Con ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
It occurs at the line where it converts from base64.
The metadata_storage_path "base64 encoded" value is apparently a mangled version of base64 where any trailing "=" are removed, and a digit placed there to indicate how many "=" were removed. This is designed to allow the base64 string to be used a bit easier in urls.
See this question for more details:
How to decode metadata_storage_path produced by Azure Search indexer in .NET Core
You'll need to compensate for this modification to get back to a valid base64 encoded string before you can decode it. The linked answer gives some options for how to do this.
I am working with some data in node which has been stored in a database after being converted from a normal text string to a hex one.
When I get this data back I can log the hex value in the console and check it on a site like this and it works however when I run it through what I think should convert it, it gives me nothing.
It does work however if I manually enter the string (and not get it from the chunk in the web request).
This works:
var hex = "7b226461746574696d65223a22323031372d30372d31325432303a35323a34342e3432395a222c226465636973696f6e223a227061727479227d";
var hexData = new Buffer(hex, 'hex').toString('utf8');
console.log(hexData);
However this does not:
var hex = chunk.toString();
console.log(hex); // outputs "7b226461746574696d65223a22323031372d30372d31325432303a35323a34342e3432395a222c226465636973696f6e223a227061727479227d"
var hexData = new Buffer(hex, 'hex').toString('utf8');
console.log(hexData);
Even though I can see the data is the same in the console output
using nodejs
I am trying to generate an unique URL for user to conform email address. From that URL user will be able to verify the email account by decrypting the ciphertext and comparing ciphertext data with database . I am using CryptoJS to generate the url.
let url = 'http://localhost:4000/newUser/get/'+ciphertext ;
Problem is that in ciphertext, it contains forward slash " / " eg:
http://localhost:4000/newUser/get/U2FsdGVkX189ZNKKQrYgqU90DDwkl/W3hRTSGO1yvUMaDilPJmz9YYI3d1/E3i9C
Router is processing " / " on the URL, thus router is searching for the directory that is actually part of ciphertext. If there is any solution for not including " / " or special characters in ciphertext, please help. Thanks in advance.
You can easily replace the special characters with any of text like:
ciphertext.toString().replace('+','xMl3Jk').replace('/','Por21Ld').replace('=','Ml32');
Do not forget to replace these strings back with special characters
dataString.toString().replace('xMl3Jk', '+' ).replace('Por21Ld', '/').replace('Ml32', '=');
Hope this will help to solve your problem
However, .replace() will only replace first occurrence.
To be more precise, you can use something like this :
// Original Text
var ciphertext = 'asda+dasd+asdas/asdasd/sadasdasd/dadasd=adsasda=dasd=';
// Replaced Text
var dataString = ciphertext.replace(/\+/g,'p1L2u3S').replace(/\//g,'s1L2a3S4h').replace(/=/g,'e1Q2u3A4l');
console.log(dataString);
// Back to Original Text
ciphertext = dataString.replace(/p1L2u3S/g, '+' ).replace(/s1L2a3S4h/g, '/').replace(/e1Q2u3A4l/g, '=');
console.log(ciphertext);
IMHO, for #JaiKumarRajput's answer,
He encoded the string with,
ciphertext.toString().replace('+','xMl3Jk').replace('/','Por21Ld').replace('=','Ml32');
Now, I have no idea how xMl3Jk, Por21Ld, Ml32 works. So, i also don't know if it can mess my string somehow.
Plus, As I have to perform this on decoding as well. So, Why wont I use something like this (What already exists),
encodeURIComponent(ciphertext.toString('base64'))
I know it still introduces % char. But as its getting used in URL. In which its a escape char.
How does it matter more than doing something that can mess my code up ??
NOTE: I used it and had no issue, It doesn't mean I either had found any issue with the top one. That didn't feel neat. Its only my humble opinion, So if u don't like it? Ignore it.
There is no option for excluding / while crypto generate a encryped string.
I had face the same issue and then i found urlencode
const urlencode = require('urlencode');
let xx = {
chatRoomId: 37,
userId: 1,
doctorId: 2
}
xx = JSON.stringify(xx)
//encode
let x = (urlencode(xx, 'gbk')); // '%CB%D5%C7%A7'
// decode gbk
let y = urlencode.decode(x, 'gbk'); // original plain text
console.log(y)
// Original Text
let ciphertext = 'asda+dasd+asdas/asdasd/sadasdasd/dadasd=adsasda=dasd=';
let encodeText = encodeURIComponent(ciphertext);
console.log(encodeText)
let decodeText = decodeURIComponent(encodeText);
console.log(decodeText)
you can use like this.
first encode your ciphertext like above and concatenate that encodeText with ulr
let url = 'http://localhost:4000/newUser/get/'+encodeText ;
and then decode that encodeText from ulr
I have got the same problem, this works out for me:
const aesKey = CryptoJS.enc.Utf8.parse('aeskeyaeskeyaeskeyaeskeyaeskey32');
const aesIv = CryptoJS.enc.Utf8.parse('0123456789abcdef');
const aesOptions = {
iv: aesIv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
};
const plaintext = 'iamtheoriginal';
const ciphertext = CryptoJS.AES.encrypt(plaintext, aesKey, aesOptions).ciphertext.toString(); // 'c3a48990119bdfe40b6c32ec2aca8b93'
const encoded = {ciphertext: CryptoJS.enc.Hex.parse(ciphertext)};
const decodedText = CryptoJS.enc.Utf8.stringify(CryptoJS.AES.decrypt(encoded, aesKey, aesOptions)); // 'iamtheoriginal'
The ciphertext is 'c3a48990119bdfe40b6c32ec2aca8b93', has no special chars, and can be decrypted back to the original text, and it can encrypt all utf8 words like Chinese and even emoji.
I'm new to Powershell and I wonder if there is any way to parse input from a string according to a format, just like the sscanf() function in PHP?
URL Refered
The PHP sscanf can be emulated with matching the input with a regex. As per the PHP manual page example, let's parse author data into XML like so,
$auth = "24`tLewis Carroll" # ` is the escape char in PSh
$mc = [regex]::Match($auth, "(\d+)\t(\w+)\s(\w+)")
write-host $("<author id='{0}'>`n <firstname>{1}</firstname>`n <surname>{2}</surname>`n</author>" -f $mc.Groups[1].Value, $mc.Groups[2].Value, $mc.Groups[3].Value)
Output
<author id='24'>
<firstname>Lewis</firstname>
<surname>Carroll</surname>
</author>