示例#1
0
文件: OCPAgent.java 项目: ccbon/ocp
 private Address[] getAddressList(Key key) throws Exception {
   Address[] result = new Address[backupNbr];
   for (byte i = 0; i < backupNbr; i++) {
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     baos.write(i);
     baos.write(key.getBytes());
     Address address = new Address(hash(baos.toByteArray()));
     baos.close();
     result[i] = address;
   }
   return result;
 }
示例#2
0
  /**
   * Decrypts a given byte stream using the AES cipher, CBC mode, and PKCS5 Padding algorithms.
   *
   * @param input - Bytes to be decrypted
   * @param key - Symmetric key to decrypt
   * @param iv - Initialization vector
   * @return decrypted byte stream
   * @throws NoSuchAlgorithmException
   * @throws NoSuchPaddingException
   * @throws InvalidKeyException
   * @throws InvalidAlgorithmParameterException
   * @throws ShortBufferException
   * @throws IllegalBlockSizeException
   * @throws BadPaddingException
   * @throws UnsupportedEncodingException
   */
  public static byte[] AESCBCdecrypt(byte[] input, Key key, byte[] iv)
      throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
          InvalidAlgorithmParameterException, ShortBufferException, IllegalBlockSizeException,
          BadPaddingException, UnsupportedEncodingException {
    int plaintext_len;
    byte[] keyBytes, plaintext;
    SecretKeySpec keySpec;
    IvParameterSpec ivSpec;
    Cipher cipher;

    keyBytes = key.getBytes();
    keySpec = new SecretKeySpec(keyBytes, "AES");
    ivSpec = new IvParameterSpec(iv);
    cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
    plaintext = new byte[cipher.getOutputSize(input.length)];

    plaintext_len = cipher.update(input, 0, input.length, plaintext, 0);
    plaintext_len += cipher.doFinal(plaintext, plaintext_len);
    return plaintext;
  }