Advertisement
erirawan

ecnrypt-rijndael-128

Apr 25th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mCrypt = require('mcrypt').MCrypt
  2.  
  3. /*
  4.  *encrypt police vehicle no
  5.  *encrypt with algorithm rijndael-128, cbc
  6.  *random key
  7.  */
  8.  
  9. function encrypt(value) {
  10.     const theKey = getKey()
  11.     const iv = '7yR83bkw5rpOGR4m'
  12.     const encryptPoliceNo = new mCrypt('rijndael-128', 'cbc')
  13.     encryptPoliceNo.validateKeySize(false)
  14.     encryptPoliceNo.validateIvSize(false)
  15.     encryptPoliceNo.open(theKey, iv)
  16.  
  17.     const cipherText = encryptPoliceNo.encrypt(value)
  18.     const buf = Buffer.from(cipherText, 'utf8').toString('hex')
  19.     return buf
  20. }
  21.  
  22. function getKey() {
  23.     const keyLength = 16
  24.     const chars = '1234567890abcdefghijklmnopqrstuvwxyz'
  25.  
  26.     if (keyLength > 0) {
  27.         const charLength = chars.length - 1
  28.         let theKey = ''
  29.         for (let idx = 1; idx < keyLength; idx = theKey.length) {
  30.             theKey += chars.charAt(Math.floor(Math.random() * charLength))
  31.         }
  32.  
  33.         return theKey
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement