-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
464 lines (393 loc) · 22.8 KB
/
Program.cs
File metadata and controls
464 lines (393 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
using System;
using System.Collections;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using ISO_8583_MessageSandbox.Properties;
namespace ISO_8583_MessageSandbox
{
internal class Program
{
private static readonly IPAddress DestinationIp = IPAddress.Parse(Settings.Default.IPAddress);
private static readonly int Port = Settings.Default.Port;
private static readonly Socket Socket = new Socket(DestinationIp.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
private static string _transactionId = "";
private static string _systemAuditNumber = "";
private static bool _authRequestApproved;
private static void Main(string[] args)
{
try
{
Socket.Connect(new IPEndPoint(DestinationIp, Port));
Console.WriteLine("Connected to {0}", DestinationIp);
var msisdn = Settings.Default.msisdn;
_transactionId = DateTime.Now.ToString("yyyyddMMHHmmss") + msisdn.Substring(msisdn.Length - 3, 3);
_systemAuditNumber = DateTime.Now.ToString("HHmmss");
SendEcho();
Socket.Disconnect(false);
Console.WriteLine("Disconnected from {0}", DestinationIp);
var response = Console.ReadLine();
if (response == "exit")
Environment.Exit(0);
}
catch (Exception)
{
if (!Socket.Connected) throw;
Socket.Disconnect(false);
Console.WriteLine("Disconnected from {0}", DestinationIp);
throw;
}
}
/// <summary>
/// Sends the echo(0800) message to destination IP.
/// </summary>
private static void SendEcho()
{
try
{
// ===================================================================================================================
// The message comprises of the following: length indicator, message type identifier, and the bitmaps & data elements.
// ===================================================================================================================
// ==========================================
// MTI Message Type Indicator Length
// ==========================================
var mti = Encoding.UTF8.GetBytes("0800");
// =====================================================================================
// Each bitmap is 8 bytes in length.
// Each of the bits in its binary representation corresponds to a field in the message.
// Bit 1 indicates the most significant bit.
// primary bitmap: Indicating the presence of Data Elements 1 up to 64
// binary representation: 1000001000111000000000000000000000000000000000000000000000000000
// secondary bitmap: Indicating the presence of Data Elements 65 up to 128
// binary representation: 0000010000000000000000000000000000000000000000000000000000000000
// 127 bitmap: Indicating the presence of Data Elements 1 up to 25 on field 127
// =====================================================================================
var primaryBitmap = CreateBitmap(true, new[] { 1, 7, 11, 12, 13 }); // Indicates presence of fields 7,11,12,13
var secondaryBitmap = CreateBitmap(false, new[] { 70 }); // Indicates presence of field 70
var messageBitmap = new byte[primaryBitmap.Length + secondaryBitmap.Length];
// ==================================================================
// combine the primary bitmap and secondary bitmap into 1 byte array
// ==================================================================
Buffer.BlockCopy(primaryBitmap, 0, messageBitmap, 0, primaryBitmap.Length);
Buffer.BlockCopy(secondaryBitmap, 0, messageBitmap, primaryBitmap.Length, secondaryBitmap.Length);
// add all data elements to one string
var dataElement7 = DateTime.Now.ToString("MMddHHmmss"); // Transmission date and time Lenght:10
var dataElement11 = _systemAuditNumber; // Systems trace audit number Lenght:6
var dataElement12 = DateTime.Now.ToString("HHmmss"); // Time, local transaction Lenght:6
var dataElement13 = DateTime.Now.ToString("MMdd"); // Date, local transaction Lenght:4
var dataElement70 = "301"; // Network management info code Lenght:3
var dataElements = new StringBuilder();
dataElements.Append(dataElement7);
dataElements.Append(dataElement11);
dataElements.Append(dataElement12);
dataElements.Append(dataElement13);
dataElements.Append(dataElement70);
// add elements string to byte array
var elements = Encoding.UTF8.GetBytes(dataElements.ToString());
// create a message byte array for the mti, bitmap and elements
var msg = new byte[mti.Length + messageBitmap.Length + elements.Length];
// populate the message byte array
Buffer.BlockCopy(mti, 0, msg, 0, mti.Length);
Buffer.BlockCopy(messageBitmap, 0, msg, mti.Length, messageBitmap.Length);
Buffer.BlockCopy(elements, 0, msg, mti.Length + messageBitmap.Length, elements.Length);
// =================================================================================================
// Message Length Indicator
// A 2-byte header is to be prefixed to all messages, indicating the length of the message
// The first byte contains the quotient of the length of the message (excluding this header) and 256.
// The second byte contains the remainder of this division
// =================================================================================================
// create the message header
var messageHeader = new byte[2];
var messageLength = msg.Length;
messageHeader[0] = Convert.ToByte(messageLength / 256);
messageHeader[1] = Convert.ToByte(messageLength % 256);
// create the message including the header
var message = new byte[messageHeader.Length + msg.Length];
Buffer.BlockCopy(messageHeader, 0, message, 0, messageHeader.Length);
Buffer.BlockCopy(msg, 0, message, messageHeader.Length, msg.Length);
// decode the message again for testing
message.DecodeMessage();
// send the message to destination ip address
SendMessage(message);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
/// <summary>
/// Sends the authorization request.
/// </summary>
private static void SendAuthRequest()
{
try
{
var primaryAccountNumber = "0000000000000000001"; //2
var processingCode = "220000"; //3
var amount = "000000000100"; //4
var transmissionDateTime = DateTime.Now.ToString("MMddHHmmss"); //7
// var systemAuditNumber = "000002"; //11
var timeLocalTransaction = DateTime.Now.ToString("HHmmss"); //12
var dateLocalTransaction = DateTime.Now.ToString("MMdd"); //13
var dateExpiration = "9911"; //14 YYMM
var serviceProviderId = "1000"; //18
var posEntryMode = "021"; //22
var posConditionCode = "00"; //25
var posPinCaptureCode = "05"; //26
var institutionId = Settings.Default.InstitutionId; //32
var msisdn = Settings.Default.msisdn; //37
var terminalId = "internet"; //41
var retailerId = "retailerId"; //42
retailerId = retailerId.PadRight(15, ' ');
retailerId = retailerId.Substring(0, 15);
var locationName = "online"; // 43
locationName = locationName.PadRight(40, ' ');
locationName = locationName.Substring(0, 40);
var currencyCode = "710"; //49
var pinData = "00012345"; //52
var echoData = "Echo Data"; //59
var terminalType = "000000000000090"; //123
// =====================================
// Field 127.9 Transaction Id ...17 (... indicates three digit length indicator)
// =====================================
_transactionId = _transactionId.Length.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0') + _transactionId;
var mti = Encoding.UTF8.GetBytes("0100");
//var tertiaryBitmapLength = "025";
var primaryBitmap = CreateBitmap(true, new[] { 1, 2, 3, 4, 7, 11, 12, 13, 14, 18, 22, 25, 26, 32, 37, 41, 42, 43, 49, 52, 59 });
var secondaryBitmap = CreateBitmap(false, new[] { 123, 127 });
var tertiaryBitmap = CreateBitmap(true, new[] { 9 });
var messageBitmap = new byte[primaryBitmap.Length + secondaryBitmap.Length];
// ==================================================================
// combine the primary bitmap and secondary bitmap into 1 byte array
// ==================================================================
Buffer.BlockCopy(primaryBitmap, 0, messageBitmap, 0, primaryBitmap.Length);
Buffer.BlockCopy(secondaryBitmap, 0, messageBitmap, primaryBitmap.Length, secondaryBitmap.Length);
// add all data elements to one string
var dataElements = new StringBuilder();
dataElements.Append(primaryAccountNumber.With2DigitLengthIndicator());
dataElements.Append(processingCode);
dataElements.Append(amount);
dataElements.Append(transmissionDateTime);
dataElements.Append(_systemAuditNumber);
dataElements.Append(timeLocalTransaction);
dataElements.Append(dateLocalTransaction);
dataElements.Append(dateExpiration);
dataElements.Append(serviceProviderId);
dataElements.Append(posEntryMode);
dataElements.Append(posConditionCode);
dataElements.Append(posPinCaptureCode);
dataElements.Append(institutionId.With2DigitLengthIndicator());
dataElements.Append(msisdn);
dataElements.Append(terminalId);
dataElements.Append(retailerId);
dataElements.Append(locationName);
dataElements.Append(currencyCode);
dataElements.Append(pinData);
dataElements.Append(echoData.With3DigitLengthIndicator());
dataElements.Append(terminalType.With3DigitLengthIndicator());
// add elements string to byte array
var elements = Encoding.UTF8.GetBytes(dataElements.ToString());
// ==========================================
// build field 127.9
// ==========================================
var transactionIdBytes =
Encoding.UTF8.GetBytes(_transactionId); // the transaction id includes the 3 digit length indicator
var field127Length = (_transactionId.Length + 25).ToString().PadLeft(3, '0');
var field127LengthBytes = Encoding.UTF8.GetBytes(field127Length);
var field127Bytes = new byte[field127LengthBytes.Length + tertiaryBitmap.Length +
transactionIdBytes.Length];
Buffer.BlockCopy(field127LengthBytes, 0, field127Bytes, 0, field127LengthBytes.Length);
Buffer.BlockCopy(tertiaryBitmap, 0, field127Bytes, field127LengthBytes.Length, tertiaryBitmap.Length);
Buffer.BlockCopy(transactionIdBytes, 0, field127Bytes,
field127LengthBytes.Length + tertiaryBitmap.Length, transactionIdBytes.Length);
var totalElements = new byte[elements.Length + field127Bytes.Length];
Buffer.BlockCopy(elements, 0, totalElements, 0, elements.Length);
Buffer.BlockCopy(field127Bytes, 0, totalElements, elements.Length, field127Bytes.Length);
// build the message including field 127.9
var msg = new byte[mti.Length + messageBitmap.Length + totalElements.Length];
Buffer.BlockCopy(mti, 0, msg, 0, mti.Length);
Buffer.BlockCopy(messageBitmap, 0, msg, mti.Length, messageBitmap.Length);
Buffer.BlockCopy(totalElements, 0, msg, mti.Length + messageBitmap.Length, totalElements.Length);
// create the message header
var messageHeader = new byte[2];
var messageLength = msg.Length;
messageHeader[0] = Convert.ToByte(messageLength / 256);
messageHeader[1] = Convert.ToByte(messageLength % 256);
var message = new byte[messageHeader.Length + msg.Length];
Buffer.BlockCopy(messageHeader, 0, message, 0, messageHeader.Length);
Buffer.BlockCopy(msg, 0, message, messageHeader.Length, msg.Length);
//DecodeMessage(message);
SendMessage(message);
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
/// <summary>
/// Sends the transaction request.
/// </summary>
private static void SendTransactionRequest()
{
try
{
var primaryAccountNumber = "0000000000000000001"; //2
var processingCode = "220000"; //3
var amount = "000000000100"; //4
var transmissionDateTime = DateTime.Now.ToString("MMddHHmmss"); //7
// var systemAuditNumber = "000002"; //11
var timeLocalTransaction = DateTime.Now.ToString("HHmmss"); //12
var dateLocalTransaction = DateTime.Now.ToString("MMdd"); //13
var dateExpiration = "9911"; //14 YYMM
var serviceProviderId = "1000"; //18
var posEntryMode = "021"; //22
var posConditionCode = "00"; //25
var posPinCaptureCode = "05"; //26
var institutionId = "901051"; //32
var msisdn = Settings.Default.msisdn; //37
var terminalId = "internet"; //41
var retailerId = "retailerId"; //42
retailerId = retailerId.PadRight(15, ' ');
retailerId = retailerId.Substring(0, 15);
var locationName = "online"; // 43
locationName = locationName.PadRight(40, ' ');
locationName = locationName.Substring(0, 40);
var currencyCode = "710"; //49
var pinData = "00012345"; //52
var echoData = "Echo Data"; //59
var terminalType = "000000000000090"; //123
// =====================================
// Field 127.9 Transaction Id ...17 (... indicates three digit length indicator)
// =====================================
_transactionId = _transactionId.Length.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0') +
_transactionId;
var mti = Encoding.UTF8.GetBytes("0200");
var primaryBitmap = CreateBitmap(true,
new[] { 1, 2, 3, 4, 7, 11, 12, 13, 14, 18, 22, 25, 26, 32, 37, 41, 42, 43, 49, 52, 59 });
var secondaryBitmap = CreateBitmap(false, new[] { 123, 127 });
var tertiaryBitmap = CreateBitmap(true, new[] { 9 });
var messageBitmap = new byte[primaryBitmap.Length + secondaryBitmap.Length];
// ==================================================================
// combine the primary bitmap and secondary bitmap into 1 byte array
// ==================================================================
Buffer.BlockCopy(primaryBitmap, 0, messageBitmap, 0, primaryBitmap.Length);
Buffer.BlockCopy(secondaryBitmap, 0, messageBitmap, primaryBitmap.Length, secondaryBitmap.Length);
// add all data elements to one string
var dataElements = new StringBuilder();
dataElements.Append(primaryAccountNumber.With2DigitLengthIndicator());
dataElements.Append(processingCode);
dataElements.Append(amount);
dataElements.Append(transmissionDateTime);
dataElements.Append(_systemAuditNumber);
dataElements.Append(timeLocalTransaction);
dataElements.Append(dateLocalTransaction);
dataElements.Append(dateExpiration);
dataElements.Append(serviceProviderId);
dataElements.Append(posEntryMode);
dataElements.Append(posConditionCode);
dataElements.Append(posPinCaptureCode);
dataElements.Append(institutionId.With2DigitLengthIndicator());
dataElements.Append(msisdn);
dataElements.Append(terminalId);
dataElements.Append(retailerId);
dataElements.Append(locationName);
dataElements.Append(currencyCode);
dataElements.Append(pinData);
dataElements.Append(echoData.With3DigitLengthIndicator());
dataElements.Append(terminalType.With3DigitLengthIndicator());
// add elements string to byte array
var elements = Encoding.UTF8.GetBytes(dataElements.ToString());
// ==========================================
// build field 127.9
// ==========================================
var transactionIdBytes =
Encoding.UTF8.GetBytes(_transactionId); // the transaction id includes the 3 digit length indicator
var field127Length = (_transactionId.Length + 25).ToString().PadLeft(3, '0');
var field127LengthBytes = Encoding.UTF8.GetBytes(field127Length);
var field127Bytes = new byte[field127LengthBytes.Length + tertiaryBitmap.Length +
transactionIdBytes.Length];
Buffer.BlockCopy(field127LengthBytes, 0, field127Bytes, 0, field127LengthBytes.Length);
Buffer.BlockCopy(tertiaryBitmap, 0, field127Bytes, field127LengthBytes.Length, tertiaryBitmap.Length);
Buffer.BlockCopy(transactionIdBytes, 0, field127Bytes,
field127LengthBytes.Length + tertiaryBitmap.Length, transactionIdBytes.Length);
var totalElements = new byte[elements.Length + field127Bytes.Length];
Buffer.BlockCopy(elements, 0, totalElements, 0, elements.Length);
Buffer.BlockCopy(field127Bytes, 0, totalElements, elements.Length, field127Bytes.Length);
// build the message including field 127.9
var msg = new byte[mti.Length + messageBitmap.Length + totalElements.Length];
Buffer.BlockCopy(mti, 0, msg, 0, mti.Length);
Buffer.BlockCopy(messageBitmap, 0, msg, mti.Length, messageBitmap.Length);
Buffer.BlockCopy(totalElements, 0, msg, mti.Length + messageBitmap.Length, totalElements.Length);
// create the message header
var messageHeader = new byte[2];
var messageLength = msg.Length;
messageHeader[0] = Convert.ToByte(messageLength / 256);
messageHeader[1] = Convert.ToByte(messageLength % 256);
var message = new byte[messageHeader.Length + msg.Length];
Buffer.BlockCopy(messageHeader, 0, message, 0, messageHeader.Length);
Buffer.BlockCopy(msg, 0, message, messageHeader.Length, msg.Length);
//DecodeMessage(message);
SendMessage(message);
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
/// <summary>
/// Creates the bitmap byte array.
/// </summary>
/// <param name="isPrimary">if set to <c>true</c> [is primary].</param>
/// <param name="values">The values.</param>
/// <returns>The bitmap byte array</returns>
private static byte[] CreateBitmap(bool isPrimary, int[] values)
{
var bits = new BitArray(new byte[8]);
var bitmapString = string.Empty;
if (isPrimary)
for (var i = 1; i < bits.Length + 1; i++)
if (values.Any(indicator => indicator == i))
bitmapString += "1";
else
bitmapString += "0";
else
for (var i = 65; i < bits.Length * 2 + 1; i++)
if (values.Any(indicator => indicator == i))
bitmapString += "1";
else
bitmapString += "0";
var bitmap = bitmapString.BinaryToHexString().HexToByteArray();
return bitmap;
}
/// <summary>
/// Sends the message to destination IP.
/// </summary>
/// <param name="message">The message.</param>
private static void SendMessage(byte[] message)
{
try
{
var byteCount = Socket.Send(message, SocketFlags.None);
Console.WriteLine("Sent {0} bytes.", byteCount);
// Get reply from the server.
var buffer = new byte[1024];
byteCount = Socket.Receive(buffer, SocketFlags.None);
var receivedBytes = new byte[byteCount];
if (byteCount > 0)
{
Console.WriteLine("Received {0} bytes.", byteCount);
for (var i = 0; i < byteCount; i++)
receivedBytes[i] = buffer[i];
}
// decode the message received
receivedBytes.DecodeMessage();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}