2
I’m doing the Decode in c# of a Base64 to be able to generate a Qrcode, in PHP it works normal but in c# the return of the Decode is different. This Base64 is created and encrypted by Serpro and they ask to do the Decode of Base64 and with the byte[] that returns from the Code generate a Qrcode. PHP code working normal:
<?php
include('C:\php\lib\phpqrcode\qrlib.php');
$file = 'D:\Teste\qrcode.png';
$a = base64_decode('X0gJQwUAEQBGMEQCIFFztpz6Yi2OHOR9QafHjL4y8MtlwHG7fgE9q9KycCBXAiBlU+xqMnYHBdpArpgxZee/PvsqO5BmEyvAdqSFtLzCNgAAAAxJBBhFVBJBlhBVlAA=');
QRcode::png($a, $file, QR_ECLEVEL_M, 3, 1);
?>
In php returns this correct Qrcode, validated by Serpro!
Code in c# with different code
// Dll MessagingToolkit.QRCode
string stringBase64Endode = "X0gJQwUAEQBGMEQCIFFztpz6Yi2OHOR9QafHjL4y8MtlwHG7fgE9q9KycCBXAiBlU+xqMnYHBdpArpgxZee/PvsqO5BmEyvAdqSFtLzCNgAAAAxJBBhFVBJBlhBVlAA=";
string textoDecode = Encoding.UTF8.GetString(Convert.FromBase64String(stringBase64Endode));
var qrCode = new QRCodeEncoder();
qrCode.QRCodeBackgroundColor = Color.White;
qrCode.QRCodeForegroundColor = Color.Black;
qrCode.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCode.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
qrCode.QRCodeScale = 2;
var resultado = qrCode.Encode(textoDecode);
resultado.Save("D:\Teste\qrcode.png");
In c# returns this incorrect Qrcode, not validated by Serpro!
How do I make Encoding.UTF8.Getstring(Convert.Frombase64string()) in c# return the same result as base64_decode in php?
I don’t understand... your string is already in Base64, why you need UTF8 in Decode?
– Leandro Angelo
Hello Leandro, I use UTF8 because the
Convert.FromBase64String
returns a byte[] and the dll that generates the qrcodevar resultado = qrCode.Encode(textoDecode);
receives a text. There is another way to do this?– Alex
What string does it wait for? If the type that was encoded is not text has no magic...
– Leandro Angelo
@Leandroangelo: Todas dll que conheço em c# que gera qrcode recebe um texto, em php eu posso jogar direto o retorno do
base64_decode
to generate qrcode but, in c# I don’t know how to do.– Alex
Have you ever tried looking if this Base64 of yours isn’t already png with the QR code? You know how a QR works, how it is composed and what kind of information it stores?
– Leandro Angelo
Type, what is the info that QR should store?
– Leandro Angelo
I’ve thought about it and tried to use it:
using (MemoryStream ms = new MemoryStream(Bytes))
 {
 return Image.FromStream(ms);
 }
and error: Invalid parameter.– Alex
This qrcode goes on car board, the Serpro generates the Base64 with an encrypted string, this encrypted string must go in qrcode. The process they ask for is to do the Base64 Code and the result put in the qrcode.
– Alex
and how do you intend to decrypt? What in fact you are trying to do, read or generate a QR? Or the point is that in fact if you generate two QR’s, one with PHP’s biloteca and the other with this in C#, the image looks different?
– Leandro Angelo
I’m trying to generate qrcode and I can’t decrypt, I just have to do Decode in Base64 and with the result create qrcode the way I showed in php that works normally..
– Alex
So... that’s what I’m trying to say... you don’t have to decode that initial string, you have to generate using it. After all... it comes as byte or string?
– Leandro Angelo
Is there any example?
– Alex
What library you are using?
– Leandro Angelo
I’m using the Messagingtoolkit.Qrcode and already tried with Zxing.Net, no one gives the result of what I used in php.
– Alex
The result is an identical QR code? You know this is not necessary and what matters is the reading result... The appearance will change by several parameters such as correction points, error tolerance and etc... Hardly two different libraries will generate exactly the same, unless you set everything exactly the same and they share the same algorithm for the generation...
– Leandro Angelo
Is that the example in php returns the correct qrcode that can be validated by Rpro, already in c# with these two dll returns a different qrcode and that does not validate by Rpro.
– Alex
include the QR codes generated in your question and the Serpro specification for the validation requirements
– Leandro Angelo
I edited the question.
– Alex
Comrade, just for free, try this lib here & #Xa;https://github.com/codebude/QRCoder
– Márcio Cristian
@Márciocristian: With dll Qrcoder also didn’t work, I believe the problem was in the way that Frombase64 from C# works.
– Alex