Expand the key from a 7-byte password key into a 8-byte DES key
482711
def _expand_des_key(key):
        
        key = key[:7] + b'\0' * (7 - len(key))
        byte = struct.unpack_from('BBBBBBB', key)
        s  = struct.pack('B', ((byte[0] >> 1) & 0x7f) << 1)
        s += struct.pack("B", ((byte[0] & 0x01) << 6 | ((byte[1] >> 2) & 0x3f)) << 1)
        s += struct.pack("B", ((byte[1] & 0x03) << 5 | ((byte[2] >> 3) & 0x1f)) << 1)
        s += struct.pack("B", ((byte[2] & 0x07) << 4 | ((byte[3] >> 4) & 0x0f)) << 1)
        s += struct.pack("B", ((byte[3] & 0x0f) << 3 | ((byte[4] >> 5) & 0x07)) << 1)
        s += struct.pack("B", ((byte[4] & 0x1f) << 2 | ((byte[5] >> 6) & 0x03)) << 1)
        s += struct.pack("B", ((byte[5] & 0x3f) << 1 | ((byte[6] >> 7) & 0x01)) << 1)
        s += struct.pack("B", (byte[6] & 0x7f) << 1)
        return s
15626
def cipher(rkey, pt, Nk=4):
    """"""
    assert Nk in {4, 6, 8}
    Nr = Nk + 6

    rkey = rkey.reshape(4*(Nr+1), 32)
    pt = pt.reshape(128)

    # first round
    state = add_round_key(pt, rkey[0:4])

    for i in range(1, Nr):
        state = sub_bytes(state)
        state = shift_rows(state)
        state = mix_columns(state)
        state = add_round_key(state, rkey[4*i:4*(i+1)])

    # final round
    state = sub_bytes(state)
    state = shift_rows(state)
    state = add_round_key(state, rkey[4*Nr:4*(Nr+1)])

    return state
1247700
def modify_key_parity(key):
    
    validated_key = b''
    for byte in key:
        if parityOf(int(byte)) == -1:
            byte_candidate = int(byte) + 1 
            while parityOf(byte_candidate) == -1:
                byte_candidate = divmod(byte_candidate + 1, 256)[1]
            validated_key += bytes([byte_candidate])

        else:
            validated_key += bytes([byte])
    return validated_key
1164475
def Expandkey(key, clen):
		
		import sha
		from string import join
		from array import array

		blocks = (clen + 19) / 20
		xkey = []
		seed = key
		for i in xrange(blocks):
			seed = sha.new(key + seed).digest()
			xkey.append(seed)
		j = join(xkey, '')
		return array('L', j)
418792
function addRoundKey($round)
    {
        $temp = "";

        for ($i = 0; $i < 4; $i++) {
            for ($j = 0; $j < $this->Nb; $j++) {
                // place the i-th byte of the j-th word from expanded key w into temp
                $temp = $this->w[$round * $this->Nb + $j] >> (3 - $i) * 8;
                // Cast temp from a 32-bit word into an 8-bit byte.
                $temp %= 256;
                // Can't do unsigned shifts, so we need to make this temp positive
                $temp            = ($temp < 0 ? (256 + $temp) : $temp);
                $this->s[$i][$j] ^= $temp; // xor temp with the byte at location (i,j) of the state
            }
        }
    }
738919
function mixkey(seed, key) {
  var stringseed = seed + '', smear, j = 0;
  while (j < stringseed.length) {
    key[mask & j] =
      mask & ((smear ^= key[mask & j] * 19) + stringseed.charCodeAt(j++));
  }
  return tostring(key);
}
252781
function AES_Decrypt(block, key) {
	var l = key.length;
	AES_AddRoundKey(block, key.slice(l - 16, l));
	AES_ShiftRows(block, AES_ShiftRowTab_Inv);
	AES_SubBytes(block, AES_Sbox_Inv);
	for(var i = l - 32; i >= 16; i -= 16) {
		AES_AddRoundKey(block, key.slice(i, i + 16));
		AES_MixColumns_Inv(block);
		AES_ShiftRows(block, AES_ShiftRowTab_Inv);
		AES_SubBytes(block, AES_Sbox_Inv);
	}
	AES_AddRoundKey(block, key.slice(0, 16));
	return block;
}
635921
function (sessionKey) {

        //The first 16 bytes (MSB first) will be the key, the next 16 bytes (MSB first) will be the initialization vector (IV), and the final 8 bytes (MSB first) will be the salt.
        var key = new Buffer(16); //just the key... +8); //key plus salt
        var iv = new Buffer(16); //initialization vector

        sessionKey.copy(key, 0, 0, 16); //copy the key
        //sessionKey.copy(key, 16, 32, 40); //append the 8-byte salt
        sessionKey.copy(iv, 0, 16, 32); //copy the iv

        //are we not be doing something with the salt here?

        return crypto.createCipheriv('aes-128-cbc', key, iv);
    }
834507
function authKeyWithSaltToStorableBuffer(authKey, serverSalt)
{
  return Buffer.concat([authKey.id, authKey.value, serverSalt], 272);
}
1120995
private static void E( byte[] key, byte[] data, byte[] e ) {
        byte[] key7 = new byte[7];
        byte[] e8 = new byte[8];

        for( int i = 0; i < key.length / 7; i++ ) {
            System.arraycopy( key, i * 7, key7, 0, 7 );
            DES des = new DES( key7 );
            des.encrypt( data, e8 );
            System.arraycopy( e8, 0, e, i * 8, 8 );
        }
    }
539613
function (sessionKey) {
        //The first 16 bytes (MSB first) will be the key, the next 16 bytes (MSB first) will be the initialization vector (IV), and the final 8 bytes (MSB first) will be the salt.
        var key = new Buffer(16); //just the key... +8); //key plus salt
        var iv = new Buffer(16); //initialization vector

        sessionKey.copy(key, 0, 0, 16); //copy the key
        //sessionKey.copy(key, 16, 32, 40); //append the 8-byte salt?
        sessionKey.copy(iv, 0, 16, 32); //copy the iv

        //are we not be doing something with the salt here?

        return crypto.createDecipheriv('aes-128-cbc', key, iv);
    }
511202
function decompressKey(key) {
  const format = key[0];

  assert(key.length === 33);

  switch (format) {
    case 0x02:
    case 0x03:
      return key;
    case 0x04:
      key[0] = 0x02;
      break;
    case 0x05:
      key[0] = 0x03;
      break;
    default:
      throw new Error('Bad point format.');
  }

  // Decompress the key.
  const out = secp256k1.publicKeyConvert(key, false);

  // Reset the first byte so as not to
  // mutate the original buffer.
  key[0] = format;

  return out;
}
950119
function makeKey(aKey) {

        if (aKey &&
          utils.isAnArray(aKey)) {

          var keyLenght = aKey.length
            , tmpKey = []
            , index = 0
            , nValue
            , limitedKey
            , k64Cnt = keyLenght / 8
            , subkeyCnt = ROUND_SUBKEYS + 2 * ROUNDS
            , k32e = []
            , k32o = []
            , sBoxKey = []
            , i
            , j
            , offset = 0
            , q
            , A
            , B
            , subKeys = []
            , lB0
            , lB1
            , lB2
            , lB3
            , sBox = []
            , k0
            , k1
            , k2
            , k3;
          if (keyLenght < 8 ||
              keyLenght > 8 && keyLenght < 16 ||
              keyLenght > 16 && keyLenght < 24 ||
              keyLenght > 24 && keyLenght < 32) {

            for (index = 0; index < aKey.length + (8 - aKey.length); index += 1) {

              nValue = aKey[index];
              if (nValue !== undefined) {

                tmpKey.push(nValue);
              } else {

                tmpKey.push(0x00);
              }
            }
            aKey = tmpKey;
          } else if (keyLenght > 32) {

            limitedKey = [];
            for (index = 0; index < 32; index += 1) {

              limitedKey.push(aKey[index]);
            }
            aKey = limitedKey;
          }
          aKey = new Uint8Array(aKey);
          keyLenght = aKey.length;
          for (i = 0, j = k64Cnt - 1; i < 4 && offset < keyLenght; i += 1, j -= 1) {

            /*eslint-disable no-plusplus*/
            k32e[i] = aKey[offset++] & 0xFF | (aKey[offset++] & 0xFF) << 8 | (aKey[offset++] & 0xFF) << 16 | (aKey[offset++] & 0xFF) << 24;
            k32o[i] = aKey[offset++] & 0xFF | (aKey[offset++] & 0xFF) << 8 | (aKey[offset++] & 0xFF) << 16 | (aKey[offset++] & 0xFF) << 24;
            /*eslint-enable no-plusplus*/
            sBoxKey[j] = rsMDSEncode(k32e[i], k32o[i]);
          }
          for (i = q = 0; i < subkeyCnt / 2; i += 1, q += SK_STEP) {

            A = f32(k64Cnt, q, k32e);
            B = f32(k64Cnt, q + SK_BUMP, k32o);
            B = B << 8 | B >>> 24;
            A += B;
            subKeys[2 * i] = A;
            A += B;
            subKeys[2 * i + 1] = A << SK_ROTL | A >>> 32 - SK_ROTL;
          }
          k0 = sBoxKey[0];
          k1 = sBoxKey[1];
          k2 = sBoxKey[2];
          k3 = sBoxKey[3];
          for (i = 0; i < 256; i += 1) {

            lB0 = lB1 = lB2 = lB3 = i;
            switch (k64Cnt & 3) {

              case 1:
                sBox[2 * i] = MDS[0][P[P_01][lB0] & 0xFF ^ b0(k0)];
                sBox[2 * i + 1] = MDS[1][P[P_11][lB1] & 0xFF ^ b1(k0)];
                sBox[0x200 + 2 * i] = MDS[2][P[P_21][lB2] & 0xFF ^ b2(k0)];
                sBox[0x200 + 2 * i + 1] = MDS[3][P[P_31][lB3] & 0xFF ^ b3(k0)];
                break;

              case 0:
                lB0 = P[P_04][lB0] & 0xFF ^ b0(k3);
                lB1 = P[P_14][lB1] & 0xFF ^ b1(k3);
                lB2 = P[P_24][lB2] & 0xFF ^ b2(k3);
                lB3 = P[P_34][lB3] & 0xFF ^ b3(k3);
                /* falls through */

              case 3:
                lB0 = P[P_03][lB0] & 0xFF ^ b0(k2);
                lB1 = P[P_13][lB1] & 0xFF ^ b1(k2);
                lB2 = P[P_23][lB2] & 0xFF ^ b2(k2);
                lB3 = P[P_33][lB3] & 0xFF ^ b3(k2);
                /* falls through */

              case 2:
                sBox[2 * i] = MDS[0][P[P_01][P[P_02][lB0] & 0xFF ^ b0(k1)] & 0xFF ^ b0(k0)];
                sBox[2 * i + 1] = MDS[1][P[P_11][P[P_12][lB1] & 0xFF ^ b1(k1)] & 0xFF ^ b1(k0)];
                sBox[0x200 + 2 * i] = MDS[2][P[P_21][P[P_22][lB2] & 0xFF ^ b2(k1)] & 0xFF ^ b2(k0)];
                sBox[0x200 + 2 * i + 1] = MDS[3][P[P_31][P[P_32][lB3] & 0xFF ^ b3(k1)] & 0xFF ^ b3(k0)];
                break;

              default:
            }
          }

          return [sBox, subKeys];
        }

        throw 'key passed is undefined or not an array';
      }
839485
function encrypt(key, iv, data) {
    if(!Buffer.isBuffer(key)) {
        assert(typeof key === 'string');
        key = Buffer.from(key, 'binary')
    }

    if(!Buffer.isBuffer(iv)) {
        assert(typeof iv === 'string');
        iv = Buffer.from(iv, 'binary')
    }

    if(!Buffer.isBuffer(data)) {
        assert(typeof data === 'string');
        data = Buffer.from(data, 'binary')
    }

    var crypted = aes.encipher(data, key, iv);

    crypted = Buffer.from(crypted, 'binary');
    
    let ret = Buffer.alloc(crypted.length*2);
    for(let i = 0; i < crypted.length; i++){
        ret[2*i] = (crypted[i] >> 4) + 97;
        ret[2*i+1] = (crypted[i] & 0x0F) + 97;
    }
    return ret.toString('binary');
}
412416
public static function keyExpansion($key) {  // generate Key Schedule from Cipher Key [§5.2]
    $Nb = 4;              // block size (in words): no of columns in state (fixed at 4 for AES)
    $Nk = count($key)/4;  // key length (in words): 4/6/8 for 128/192/256-bit keys
    $Nr = $Nk + 6;        // no of rounds: 10/12/14 for 128/192/256-bit keys

    $w = array();
    $temp = array();

    for ($i=0; $i<$Nk; $i++) {
      $r = array($key[4*$i], $key[4*$i+1], $key[4*$i+2], $key[4*$i+3]);
      $w[$i] = $r;
    }

    for ($i=$Nk; $i<($Nb*($Nr+1)); $i++) {
      $w[$i] = array();
      for ($t=0; $t<4; $t++) $temp[$t] = $w[$i-1][$t];
      if ($i % $Nk == 0) {
        $temp = self::subWord(self::rotWord($temp));
        for ($t=0; $t<4; $t++) $temp[$t] ^= self::$rCon[$i/$Nk][$t];
      } else if ($Nk > 6 && $i%$Nk == 4) {
        $temp = self::subWord($temp);
      }
      for ($t=0; $t<4; $t++) $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t];
    }
    return $w;
  }
16381
private static byte[] applyCipher(byte[] keyBytes, byte[] textBytes) {
        byte[] result = new byte[textBytes.length];
        for (int i = 0; i < result.length; i++) {
            int keyPos = i % keyBytes.length;
            result[i] = (byte) (textBytes[i] ^ keyBytes[keyPos]);
        }
        return result;
    }
1075700
private static Key str2Key_impl(String s) {
    String key = s;
    byte[] kb = new byte[(key.length() - 1) / 2];
    int i = 0, j = 0;
    if( (key.length() > 2) && (key.charAt(0) == '%') && (key.charAt(1) >= '0') && (key.charAt(1) <= '9') ) {
      // Dehexalate until '%'
      for( i = 1; i < key.length(); i += 2 ) {
        if( key.charAt(i) == '%' ) break;
        char b0 = (char) (key.charAt(i    ) - '0');
        if( b0 > 9 ) b0 += '0' + 10 - 'A';
        char b1 = (char) (key.charAt(i + 1) - '0');
        if( b1 > 9 ) b1 += '0' + 10 - 'A';
        kb[j++] = (byte) ((b0 << 4) | b1);  // De-hexelated byte
      }
      i++;                      // Skip the trailing '%'
    }
    // a normal key - ASCII with special characters encoded after % sign
    for( ; i < key.length(); ++i ) {
      byte b = (byte) key.charAt(i);
      if( b == '%' ) {
        switch( key.charAt(++i) ) {
        case '%':  b = '%';  break;
        case 'c':  b = ':';  break;
        case 'd':  b = '.';  break;
        case 'g':  b = '>';  break;
        case 'l':  b = '<';  break;
        case 'q':  b = '"';  break;
        case 's':  b = '/';  break;
        case 'b':  b = '\\'; break;
        case 'z':  b = '\0'; break;
        default:
          Log.warn("Invalid format of filename " + s + " at index " + i);
        }
      }
      if( j >= kb.length ) kb = Arrays.copyOf(kb, Math.max(2, j * 2));
      kb[j++] = b;
    }
    // now in kb we have the key name
    return Key.make(Arrays.copyOf(kb, j));
  }