Giter VIP home page Giter VIP logo

xtea-js's Introduction

XTEA

A pure JavaScript implementation of XTEA block cipher with support for ECB and CBC modes of operation.

The PKCS#7 padding is used for processing data not alligned to 8-byte block size.

The XTEA cipher algorithm is very effective and is supported by PHP's mcrypt cryptographic extension (in contrast to XXTEA cipher) so you may find this module useful when you need interoperability between JS and PHP and don't need stronger cryptography.

API

This module exports four functions:

encrypt(msg, key, [mode=ecb], [iv], [skippad])

Encrypts data using XTEA cipher using specified block cipher mode of operation and PKCS#7 padding.

Params:

  • Buffer msg Message to encrypt
  • Buffer key 128-bit encryption key (16 bytes)
  • string [mode=ecb] Block cipher mode of operation (currently only 'ecb' or 'cbc')
  • Buffer [iv] Optional IV
  • bool [skippad] Skip PKCS#7 padding postprocessing

Return:

  • Buffer

decrypt(msg, key, [mode=ecb], [iv], [skippad])

Decrypts data using XTEA cipher using specified block cipher mode of operation and PKCS#7 padding.

Params:

  • Buffer msg Ciphertext to decrypt
  • Buffer key 128-bit encryption key (16 bytes)
  • string [mode=ecb] Block cipher mode of operation (currently only 'ecb' or 'cbc')
  • Buffer [iv] Optional IV
  • bool [skippad] Skip PKCS#7 padding postprocessing

Return:

  • Buffer

encryptBlock( block, key )

Encrypts single block of data using XTEA cipher.

Params:

  • Buffer block 64-bit (8-bytes) block of data to encrypt
  • Buffer key 128-bit (16-bytes) encryption key

Return:

  • Buffer 64-bit of encrypted block

decryptBlock( block, key )

Decrypts single block of data using XTEA cipher.

Params:

  • Buffer block 64-bit (8-bytes) block of data to encrypt
  • Buffer key 128-bit (16-bytes) encryption key

Return:

  • Buffer 64-bit of encrypted block

Example usage

var xtea = require('xtea');

var plaintext = new Buffer('Zażółć gęślą jaźń', 'utf8');
var key = new Buffer('33fd7bd6d85ddbe134c23fcb09c37e5a', 'hex');
var ciphertext = xtea.encrypt( plaintext, key );

console.log( ciphertext.toString('hex') );
console.log( xtea.decrypt( ciphertext, key ).toString() );

expected output:

da9466824a7606cf8faa4bf462c667c1dc4a23cb508199fdd6689b5134640e09
Zażółć gęślą jaźń

Example PHP code

function xtea_encrypt($msg, $key, $mode, $iv) {
    $pad = 8 - (strlen($msg) % 8);
    $msg .= str_repeat(chr($pad), $pad);
    return mcrypt_encrypt(MCRYPT_XTEA, $key, $msg, $mode, $iv);
}

function xtea_decrypt($msg, $key, $mode, $iv) {
    $msg = mcrypt_decrypt(MCRYPT_XTEA, $key, $msg, $mode, $iv);
    $pad = ord($msg[ strlen($msg) - 1 ]);
    return substr($msg, 0, strlen($msg) - $pad);
}

Running tests

Install dev dependencies and execute npm run test:

$ npm install -d
$ npm run test

\newpage

License

The MIT License (MIT)

Copyright (c) 2015 Sebastian Smyczynski, Simplito Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

xtea-js's People

Contributors

goldenretriveryt avatar ssmyczynski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

xtea-js's Issues

RangeError buffer on encrypt function call

`var xtea = require('xtea');

var str = Buffer.from('encoder', 'utf8');
var key = Buffer.from('666f6f626172', 'hex');
var ciphertext = xtea.encrypt(str, key);
console.log(ciphertext.toString('hex'));`

i get this error buffer.js:839
throw new RangeError('Index out of range');
^

RangeError: Index out of range
at checkOffset (buffer.js:839:11)
at Buffer.readUInt32BE (buffer.js:913:5)

on encrypt function call
could you help me please ?

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.