Returns a 32 byte array containing the private key, or null if the key is
encrypted or public only

@return -
978696
public byte[] getPrivKeyBytes() {
        if (privKey == null) {
            return null;
        } else if (privKey instanceof BCECPrivateKey) {
            return bigIntegerToBytes(((BCECPrivateKey) privKey).getD(), 32);
        } else {
            return null;
        }
    }
339112
public byte[] decryptAES(byte[] cipher) {

        if (privKey == null) {
            throw new MissingPrivateKeyException();
        }
        if (!(privKey instanceof BCECPrivateKey)) {
            throw new UnsupportedOperationException("Cannot use the private key as an AES key");
        }

        AESFastEngine engine = new AESFastEngine();
        SICBlockCipher ctrEngine = new SICBlockCipher(engine);

        KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray(((BCECPrivateKey) privKey).getD()));
        ParametersWithIV params = new ParametersWithIV(key, new byte[16]);

        ctrEngine.init(false, params);

        int i = 0;
        byte[] out = new byte[cipher.length];
        while (i < cipher.length) {
            ctrEngine.processBlock(cipher, i, out, i);
            i += engine.getBlockSize();
            if (cipher.length - i < engine.getBlockSize())
                break;
        }

        // process left bytes
        if (cipher.length - i > 0) {
            byte[] tmpBlock = new byte[16];
            System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
            ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
            System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
        }

        return out;
    }
1247954
public static PublicKey getEncryptCertPublicKey() {
		if (null == encryptCert) {
			String path = SDKConfig.getConfig().getEncryptCertPath();
			if (!isEmpty(path)) {
				encryptCert = initCert(path);
				return encryptCert.getPublicKey();
			} else {
				LogUtil.writeErrorLog("acpsdk.encryptCert.path is empty");
				return null;
			}
		} else {
			return encryptCert.getPublicKey();
		}
	}
932047
public static PrivateKey createPrivateKey(byte[] privateKey) {
    if (privateKey == null) {
      return null;
    }

    try {
      KeyFactory fac = KeyFactory.getInstance("RSA");
      EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKey);
      return fac.generatePrivate(spec);
    }
    catch (NoSuchAlgorithmException e) {
      throw new IllegalStateException(e);
    }
    catch (InvalidKeySpecException e) {
      throw new IllegalStateException(e);
    }
  }
50768
public byte[] toByteArray() {
        final ByteBuffer buff = ByteBuffer.allocate(getSize());

        // add revision
        buff.put(revision);

        // add reserved
        buff.put((byte) 0x00);

        // add contro flags
        buff.put(controlFlags[1]);
        buff.put(controlFlags[0]);

        // add offset owner
        buff.position(4);

        int nextAvailablePosition = 20;
        // add owner SID
        if (owner == null) {
            buff.putInt(0);
        } else {
            buff.put(Hex.reverse(NumberFacility.getBytes(nextAvailablePosition)));
            buff.position(nextAvailablePosition);
            buff.put(owner.toByteArray());
            nextAvailablePosition += owner.getSize();
        }

        // add offset group
        buff.position(8);

        // add group SID
        if (group == null) {
            buff.putInt(0);
        } else {
            buff.put(Hex.reverse(NumberFacility.getBytes(nextAvailablePosition)));
            buff.position(nextAvailablePosition);
            buff.put(group.toByteArray());
            nextAvailablePosition += group.getSize();
        }

        // add offset sacl
        buff.position(12);

        // add SACL
        if (sacl == null) {
            buff.putInt(0);
        } else {
            buff.put(Hex.reverse(NumberFacility.getBytes(nextAvailablePosition)));
            buff.position(nextAvailablePosition);
            buff.put(sacl.toByteArray());
            nextAvailablePosition += sacl.getSize();
        }

        // add offset dacl
        buff.position(16);

        // add DACL
        if (dacl == null) {
            buff.putInt(0);
        } else {
            buff.put(Hex.reverse(NumberFacility.getBytes(nextAvailablePosition)));
            buff.position(nextAvailablePosition);
            buff.put(dacl.toByteArray());
        }

        return buff.array();
    }
900467
public static byte[] generateRandomUUIDBytes() 
   {
      if (rand == null)
         rand = new SecureRandom();
      
      byte[] buffer = new byte[16];
      rand.nextBytes(buffer);

      // Set version to 3 (Random)
      buffer[6] = (byte) ((buffer[6] & 0x0f) | 0x40);
      // Set variant to 2 (IETF)
      buffer[8] = (byte) ((buffer[8] & 0x3f) | 0x80);
      
      return buffer;
   }
503751
public byte[] getPrivKeyBytes33() {
        byte[] bytes33 = new byte[33];
        byte[] priv = getPrivKeyBytes();
        System.arraycopy(priv, 0, bytes33, 33 - priv.length, priv.length);
        return bytes33;
    }
1021520
def rfc6979_ecdsa_nonce(hash, privkey)
      raise ArgumentError, "Hash must be 32 bytes long" if hash.bytesize != 32
      raise ArgumentError, "Private key must be 32 bytes long" if privkey.bytesize != 32

      autorelease do |pool|
        order = self.group_order

        # Step 3.2.a. hash = H(message). Already performed by the caller.

        # Step 3.2.b. V = 0x01 0x01 0x01 ... 0x01 (32 bytes equal 0x01)
        v = "\x01".b*32

        # Step 3.2.c. K = 0x00 0x00 0x00 ... 0x00 (32 bytes equal 0x00)
        k = "\x00".b*32

        # Step 3.2.d. K = HMAC-SHA256(key: K, data: V || 0x00 || int2octets(privkey) || bits2octets(hash))
        h1 = pool.new_bn(hash)
        BN_div(nil, h1, h1, order, pool.bn_ctx) # h1 = h1 % order
        h1data = data_from_bn(h1, min_length: 32)
        k = BTC.hmac_sha256(key: k, data: v + "\x00".b + privkey + h1data)

        # Step 3.2.e. V = HMAC-SHA256(key: K, data: V)
        v = BTC.hmac_sha256(key: k, data: v)

        # Step 3.2.f. K = HMAC-SHA256(key: K, data: V || 0x01 || int2octets(privkey) || bits2octets(hash))
        k = BTC.hmac_sha256(key: k, data: v + "\x01".b + privkey + h1data)

        # Step 3.2.g. V = HMAC-SHA256(key: K, data: V)
        v = BTC.hmac_sha256(key: k, data: v)

        # Step 3.2.h.
        zero32 = "\x00".b*32
        10000.times do
          t = BTC.hmac_sha256(key: k, data: v)
          tn = pool.new_bn(t)
          if BN_cmp(tn, order) < 0
            nonce = data_from_bn(tn, min_length: 32)
            if nonce != zero32
              return nonce
            end
          end
          # Note: the probability of not succeeding at the first try is about 2^-127.
          k = BTC.hmac_sha256(key: k, data: v + zero32)
          v = BTC.hmac_sha256(key: k, data: v)
        end
        # we generated 10000 numbers, none of them is good -> fail.
        raise "Cannot find any good ECDSA nonce after 10000 iterations of RFC6979."
      end
    end
123103
public static byte[] decrypt(byte[] cipherByteArray, PrivateKey privateKey) {
        try {
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            return cipher.doFinal(cipherByteArray);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }
1039581
public byte[] getPublic() {
        keyNotNull(pair.getPublic());
        Key key = pair.getPublic();
        return getKeyBytes(key);
    }
1143061
public String getEncryptedValue() {
        try {
            synchronized (this) {
                if (iv == null) { //if we were created from plain text or other reason without iv
                    iv = KEY.newIv();
                }
            }
            Cipher cipher = KEY.encrypt(iv);
            byte[] encrypted = cipher.doFinal(this.value.getBytes(UTF_8));
            byte[] payload = new byte[1 + 8 + iv.length + encrypted.length];
            int pos = 0;
            // For PAYLOAD_V1 we use this byte shifting model, V2 probably will need DataOutput
            payload[pos++] = PAYLOAD_V1;
            payload[pos++] = (byte)(iv.length >> 24);
            payload[pos++] = (byte)(iv.length >> 16);
            payload[pos++] = (byte)(iv.length >> 8);
            payload[pos++] = (byte)(iv.length);
            payload[pos++] = (byte)(encrypted.length >> 24);
            payload[pos++] = (byte)(encrypted.length >> 16);
            payload[pos++] = (byte)(encrypted.length >> 8);
            payload[pos++] = (byte)(encrypted.length);
            System.arraycopy(iv, 0, payload, pos, iv.length);
            pos+=iv.length;
            System.arraycopy(encrypted, 0, payload, pos, encrypted.length);
            return "{"+new String(Base64.getEncoder().encode(payload))+"}";
        } catch (GeneralSecurityException e) {
            throw new Error(e); // impossible
        }
    }
539344
public byte[][] keypair()
    {
        byte[][] pair = new byte[2][];

        byte[] publicKey = new byte[Size.PUBLICKEY.bytes()];
        byte[] secretKey = new byte[Size.SECRETKEY.bytes()];

        int rc = curve25519xsalsa20poly1305.crypto_box_keypair(publicKey, secretKey);
        assert (rc == 0);

        pair[0] = publicKey;
        pair[1] = secretKey;

        return pair;
    }
251499
public byte[] getBytes() {
    byte[] result = new byte[2];
    result[0] = (byte) (value & BYTE_MASK);
    result[1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
    return result;
  }
1059583
public final byte[] getBytes() {
		final int length = bytes.length;
		final byte[] result = new byte[length];

		// Make a defensive copy
		System.arraycopy(bytes, 0, result, 0, length);

		return result;
	}
1071414
public byte[] getByteArray(final String key) {
		final String[] value = getArray(key);
		if (value != null) {
			final byte[] result = new byte[value.length];
			for (int i = 0; i < value.length; i++) {
				result[i] = Byte.parseByte(value[i]);
			}
			return result;
		}
		return null;
	}
927843
byte[] getRawData() {

    // Header: [Version | Options]
    byte[] header = new byte[] { (byte) getVersionNumber(), 0 };

    if (isPasswordBased) {
      header[1] |= FLAG_PASSWORD;
    }

    // Pack result
    final int dataSize;

    if (isPasswordBased) {
      dataSize = header.length + encryptionSalt.length + hmacSalt.length
          + iv.length + ciphertext.length + hmac.length;
    } else {
      dataSize = header.length + iv.length + ciphertext.length + hmac.length;
    }

    byte[] result = new byte[dataSize];

    System.arraycopy(header, 0, result, 0, header.length);

    if (isPasswordBased) {
      System.arraycopy(encryptionSalt, 0, result, header.length,
          encryptionSalt.length);
      System.arraycopy(hmacSalt, 0, result, header.length
          + encryptionSalt.length, hmacSalt.length);
      System.arraycopy(iv, 0, result, header.length + encryptionSalt.length
          + hmacSalt.length, iv.length);
      System.arraycopy(ciphertext, 0, result, header.length
          + encryptionSalt.length + hmacSalt.length + iv.length,
          ciphertext.length);
      System.arraycopy(hmac, 0, result, header.length + encryptionSalt.length
          + hmacSalt.length + iv.length + ciphertext.length, hmac.length);
    } else {
      System.arraycopy(iv, 0, result, header.length, iv.length);
      System.arraycopy(ciphertext, 0, result, header.length + iv.length,
          ciphertext.length);
      System.arraycopy(hmac, 0, result, header.length + iv.length
          + ciphertext.length, hmac.length);
    }

    return result;
  }
1228664
public byte[] toByteArray()
	{
		final byte[] data = new byte[7];
		data[0] = (byte) oindex;
		data[1] = (byte) id;
		data[2] = (byte) pindex;
		data[3] = (byte) (write ? 0x80 : 0x00);
		data[3] |= pdt & 0x3f;
		data[4] = (byte) (maxElems >> 8);
		data[5] = (byte) maxElems;
		data[6] = (byte) (rLevel << 4 | (wLevel & 0x0f));
		return data;
	}