C# Convert.Frombase64string returns different from PHP base64_decode

Asked

Viewed 81 times

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!

Em php retorna este Qrcode correto!

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!

Em c# retorna este Qrcode incorreto

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?

  • Hello Leandro, I use UTF8 because the Convert.FromBase64String returns a byte[] and the dll that generates the qrcode var resultado = qrCode.Encode(textoDecode); receives a text. There is another way to do this?

  • What string does it wait for? If the type that was encoded is not text has no magic...

  • @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.

  • 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?

  • Type, what is the info that QR should store?

  • I’ve thought about it and tried to use it:using (MemoryStream ms = new MemoryStream(Bytes))&#xA; {&#xA; return Image.FromStream(ms);&#xA; } and error: Invalid parameter.

  • 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.

  • 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?

  • 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..

  • 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?

  • Is there any example?

  • What library you are using?

  • I’m using the Messagingtoolkit.Qrcode and already tried with Zxing.Net, no one gives the result of what I used in php.

  • 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...

  • 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.

  • include the QR codes generated in your question and the Serpro specification for the validation requirements

  • I edited the question.

  • Comrade, just for free, try this lib here & #Xa;https://github.com/codebude/QRCoder

  • @Márciocristian: With dll Qrcoder also didn’t work, I believe the problem was in the way that Frombase64 from C# works.

Show 15 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.