• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

C# to PBWIN

Started by Douglas Martin, February 28, 2014, 11:22:35 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Douglas Martin

I am trying to write an interface to VeriFone's new XPOINT credit card readers. The examples they provided are in C# and Java.

Not very many lines of C# code to build the interface. Is it possible to translat this code to PBWIN?

Here are the examples

// Building an XML Document
---------------------------------------------------------------------------
var writerSettings = new XmlWriterSettings()
{
OmitXmlDeclaration = true,
Encoding = Encoding.UTF8
};

var request = new XDocument();
using (var writer = request.CreateWriter(),writerSettings)
{
    writer.WriteStartDocument();
    writer.WriteStartElement("TRANSACTION");
    writer.WriteElementString("FUNCTION_TYPE", "SECURITY");
    writer.WriteElementString("COMMAND", "REGISTER");
    writer.WriteElementString("ENTRY_CODE", entryCode);
    writer.WriteElementString("KEY", publicEncodedKey);
    writer.WriteEndElement();
    writer.WriteEndDocument();
}
---------------------------------------------------------------------------
//  Opening/Closing a Network Socket
---------------------------------------------------------------------------
using (var socket = new TcpClient(address, port))
{
// do stuff
}

// Writing To the Network Socket
---------------------------------------------------------------------------
var writerSettings = new XmlWriterSettings()
{
OmitXmlDeclaration = true,
Encoding = Encoding.UTF8
};

using (var writer = XmlWriter.Create(socket.GetStream(), writerSettings))
{
request.WriteTo(writer);
}

// Reading From a Network Socket
----------------------------------------------------------------------------
var readerSettings = new XmlReaderSettings()
{
    CloseInput = false,
    IgnoreWhitespace = true,
    ConformanceLevel = ConformanceLevel.Fragment
};

var reader = XmlReader.Create(stream, readerSettings);
var response = new XDocument();
using (var writer = response.CreateWriter())
{
    int depth = 0;
    while (reader.Read())
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element:
                depth++;
                writer.WriteStartElement(reader.Name);
                writer.WriteAttributes(reader, false);
                break;
            case XmlNodeType.Text:
                writer.WriteString(reader.Value);
                break;
            case XmlNodeType.EndElement:
                writer.WriteEndElement();
                if (--depth == 0)
                {
                    reader.Close();
                    return response;
                }
                break;
            default:
                break;
        }
    }
}
-----------------------------------------------------------------------------
// Generate Random Number Between 0 and 9999
-----------------------------------------------------------------------------
var random = new Random();
var entryCode = random.Next(9999).ToString();

// Generate RSA Public/Private Key Pair With a Size of 2048
-----------------------------------------------------------------------------
var cryptoProvider = new RSACryptoServiceProvider(2048);

// Encode the Public Key
-----------------------------------------------------------------------------
// example uses Bouncy Castle to der-encode the public key
var publicKey = DotNetUtilities.GetRsaPublicKey(cryptoProvider);
var publicKeyInfo = SubjectPublicKeyInfoFactory
.CreateSubjectPublicKeyInfo(publicKey);
var publicEncodedBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
var publicEncodedString = Convert.ToBase64String(publicEncodedBytes);

// Base64 Decode the MAC_KEY Value
-----------------------------------------------------------------------------
var macKeyBase64Decoded = Convert.FromBase64String(encryptedMacKey);

// Decrypt the MAC_KEY
-----------------------------------------------------------------------------
var macKey = cryptoProvider.Decrypt(macKeyBase64Decoded, false);

Generate the Next Counter Value
-----------------------------------------------------------------------------
// The COUNTER value is an integer that must be incremented by at least 1 for each message attempt with the POS. The steps here apply to all non-security commands as well.
var nextCounter = (++counter).ToString();

Compute the Hash Using HMAC SHA-256
-----------------------------------------------------------------------------
// Point uses a Hash-based message authentication code (HMAC) to verify message authenticity. The hashing algorithm is SHA-256. The MAC value is a hash of the COUNTER.
// convert counter to bytes
var counterBytes = Encoding.UTF8.GetBytes(counter);

// import AES 128 MAC_KEY
var hmac = new HMACSHA256(macKey);
var macBytes = hmac.ComputeHash(counterBytes);

// Base 64 Encode the MAC
-----------------------------------------------------------------------------
// The COUNTER must be incremented by at least 1 for each message attempt with the POS. The counter must be an integer.
var mac = Convert.ToBase64String(macBytes);











Doug

Eddy Van Esch

Ehhmmm ... :

var cryptoProvider = new RSACryptoServiceProvider(2048);

???????

Mike Stefanik

You're going to find yourself knee-deep in the CryptoAPI if you want to implement something like this in PB, there's not going to be any one-to-one direct translation there. What might be easiest for you to do is create an class in C# that implements the functionality that you need using their reference code, and then expose those methods with a COM interface (which is not difficult to do, there's just certain restrictions on the data types that you can use with public methods and properties). You'd now have a COM server that you could use in your PB application like any other COM object. The only real downside is that your application would require the .NET Framework be installed. Today, however, that's not really such a big deal since they're typically installed and updated as part of the operating system and installers can handle the prereqs for your end-user.


Mike Stefanik
sockettools.com