Git Product home page Git Product logo

deepal / node-dukpt Goto Github PK

View Code? Open in Web Editor NEW
27.0 3.0 21.0 602 KB

Node JS Library for Derived Unique Key Per Transaction (DUKPT) Encryption ๐Ÿ’ณ๐Ÿ”‘๐Ÿ›ก

Home Page: https://jsblog.insiderattack.net/dukpt-derived-unique-key-per-transaction-with-node-js-72a6642ce89

License: MIT License

JavaScript 100.00%
dukpt-encryption aes node-dukpt dukpt encryption payments security javascript nodejs decryption

node-dukpt's Introduction

node-dukpt

npm version alt downloads

Derived Unique Key Per Transaction (DUKPT) Encryption with NodeJS

This the NodeJS implementation of DUKPT based on the vanilla javascript implementation of IDTech DUKPT encryption/decryption. This module provides Dukpt encryption using either 3DES or AES schemes.

Please note that AES encryption/decryption is currently only supported with NodeJS versions 6.x.x and above due to few limitations which will be addressed soon in a next release.

Don't hesitate to report any bugs in the Github Repository!. Many thanks to @jamiesoncj for providing resources.

Prerequisites

  • Node v12.0.0 or above

Installing

npm install dukpt --save

Using DUKPT

Initialize DUKPT by providing BDK and KSN:

const Dukpt = require('dukpt');

const encryptionBDK = '0123456789ABCDEFFEDCBA9876543210';
const ksn = 'FFFF9876543210E00008';
const keyMode = 'datakey'; // optional: defaults to 'datakey'
const plainTextCardData = '%B5452310551227189^DOE/JOHN      ^08043210000000725000000?';

const dukpt = new Dukpt(encryptionBDK, ksn);

After initializing, you can use dukptEncrypt and dukptDecrypt methods to encrypt/decrypt data using DUKPT.

Encrypting ascii data

Using 3DES,

const options = {
	inputEncoding: 'ascii', 
	outputEncoding: 'hex',
	encryptionMode: '3DES'
};
const encryptedCardData3Des = dukpt.dukptEncrypt(plainTextCardData, options);

or with AES,

const options = {
	inputEncoding: 'ascii', 
	outputEncoding: 'hex',
	encryptionMode: 'AES'
};
const encryptedCardDataAes = dukpt.dukptEncrypt(plainTextCardData, options);

Encrypting hex data

Using 3DES,

const options = {
	inputEncoding: 'hex',
	outputEncoding: 'hex',
	encryptionMode: '3DES'
};
const encryptedCardData3Des = dukpt.dukptEncrypt(plainTextCardData, options);

or using AES,

const options = {
	inputEncoding: 'hex',
	outputEncoding: 'hex',
	encryptionMode: 'AES'
};
const encryptedCardDataAes = dukpt.dukptEncrypt(plainTextCardData, options);

Decrypting data with ascii output encoding

const options = {
	outputEncoding: 'ascii',
	decryptionMode: '3DES',
	trimOutput: true
};

const decryptedCardData = dukpt.dukptDecrypt(encryptedCardData, options);

Decrypting data with hex output encoding

const options = {
	outputEncoding: 'hex',
	decryptionMode: '3DES',
	trimOutput: true
};

const decryptedCardData = dukpt.dukptDecrypt(encryptedCardData, options);

API

constructor Dukpt(bdk, ksn, [keyMode])

bdk

Base derivation key (BDK) for initialization

ksn

Key serial number (KSN) for initialization

See here for more information on BDK and KSN

keyMode

default: 'datakey'

Key mode for deriving session key from initial pin encryption key (IPEK). Possible values are:

  • datakey (default)
  • pinkey
  • mackey

Dukpt.prototype.dukptEncrypt(plainTextCardData, options) and Dukpt.prototype.dukptDecrypt(encryptedCardData, options)

options

You can use options object to provide additional options for the DUKPT encryption/decryption. This object is optional and, if you don't provide it, encryption/decryption will use the default values shipped with it.

Following listed are the available options.

Option Possible Values Default Value Description
outputEncoding ascii, hex For encryption hex, for decryption ascii Specify output encoding of encryption/decryption
inputEncoding ascii, hex For encryption ascii, for decryption hex Specify encoding of the input data for encryption/decryption
trimOutput (for decryption only) true, false false Specify whether to strip out null characters from the decrypted output
encryptionMode (for encryption only) 3DES/AES 3DES/AES Specify encryption scheme for dukpt
decryptionMode (for decryption only) 3DES/AES 3DES/AES Specify decryption scheme for dukpt

Tests

Tests can be run using gulp as follows:

npm run test

Roadmap

  • Support for DUKPT Encryption/Decryption with 3DES
  • Support for DUKPT Encryption/Decryption with AES

node-dukpt's People

Contributors

deepal avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-dukpt's Issues

node-dukpt

Hi, i would like to ask how do i specify the mode in 3DES from CBC to use ECB

Constructor not taking Keymode, Please update the code

function Dukpt(bdk, ksn) {
_classCallCheck(this, Dukpt);

    this.bdk = bdk;
    this.ksn = ksn;
    this._sessionKey = this._deriveDukptSessionKey();
}

_createClass(Dukpt, [{
    key: '_deriveDukptSessionKey',
    value: function _deriveDukptSessionKey() {
        var keyMode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'datakey';

dukpt AES not working

In dukpt.lib.js there is a check for KSN being 20 bytes regardless of encryption mode. If this check is updated to 24 to allow for the larger AES KSN then the the code gets further but the generated IPEK is incorrect. What testing was done for dukpt AES?

`yarn install` isn't creating a dist folder

After running yarn install, the dist folder for the project is missing. Since the package.json has the main pointed at dist/index.js, the build fails. I was able to temporarily get around the issue by pointing to src/index.js.

this._sessionKey.replace is not a function

I'm getting this._sessionKey.replace is not a function when trying to use this library, here is my code, what am I missing:

const Dukpt = require('dukpt');

const encryptionBDK = '15235124sdfasdfas1233123';
const ksn = '1234567890123456';

const encryptedCardData = 'asdfasfasd'

const dukpt = new Dukpt(encryptionBDK, ksn);

const options = {
    outputEncoding: 'ascii',
    decryptionMode: '3DES',
    trimOutput: true
};

const decryptedCardData = dukpt.dukptDecrypt(encryptedCardData, options);
console.log(decryptedCardData)

And a full error dump:

TypeError: this._sessionKey.replace is not a function
    at Dukpt.dukptDecrypt (C:\projects\...\node_modules\โ†[4mdukptโ†[24m\dist\lib\dukpt.lib.js:196:34)
    at Object.<anonymous> (C:\projects\...\test.js:19:33)
โ†[90m    at Module._compile (internal/modules/cjs/loader.js:1063:30)โ†[39m
โ†[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)โ†[39m
โ†[90m    at Module.load (internal/modules/cjs/loader.js:928:32)โ†[39m
โ†[90m    at Function.Module._load (internal/modules/cjs/loader.js:769:14)โ†[39m
โ†[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)โ†[39m
โ†[90m    at internal/main/run_main_module.js:17:47โ†[39m

Support for CBC MAC ?

Hi there,

Fantastic library. Are there plans to support generating MACs natively within the API? It's possible currently to generate a correct MAC for payloads 8 bytes or less, using the "mackey" key type and calling dukptEncrypt, by virtue of being within a single 8-byte block. Greater than 8 bytes though, and this no longer works.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.