⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 securityutils.java

📁 pastry的java实现的2.0b版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      hmac1.update(MathUtils.xor(realKey, HMAC_IPAD));      hmac1.update(text);      hmac2.update(MathUtils.xor(realKey, HMAC_OPAD));      hmac2.update(hmac1.digest());      return hmac2.digest();    }  }  /**   * Utility method for encrypting a block of data with symmetric encryption.   *   * @param data The data   * @param key The key   * @return The ciphertext   * @exception SecurityException If the encryption does not happen properly   */  public static byte[] encryptSymmetric(byte[] data, byte[] key) throws SecurityException {    return encryptSymmetric(data, key, 0, data.length);  }  /**   * Utility method for encrypting a block of data with symmetric encryption.   *   * @param data The data   * @param key The key   * @param offset The offset into the data   * @param length The length of data to write   * @return The ciphertext   * @exception SecurityException If the encryption does not happen properly   */  public static byte[] encryptSymmetric(byte[] data, byte[] key, int offset, int length) throws SecurityException {    try {      synchronized (cipherSymmetric) {        key = correctLength(key, SYMMETRIC_KEY_LENGTH);        SecretKeySpec secretKey = new SecretKeySpec(key, SYMMETRIC_ALGORITHM);        cipherSymmetric.init(Cipher.ENCRYPT_MODE, secretKey);        return cipherSymmetric.doFinal(data, offset, length);      }    } catch (InvalidKeyException e) {      throw new SecurityException("InvalidKeyException encrypting object: " + e);    } catch (IllegalBlockSizeException e) {      throw new SecurityException("IllegalBlockSizeException encrypting object: " + e);    } catch (BadPaddingException e) {      throw new SecurityException("BadPaddingException encrypting object: " + e);    }  }  /**   * Utility method for decrypting some data with symmetric encryption.   *   * @param data The data to decrypt   * @param key The key   * @return The decrypted data   * @exception SecurityException If the decryption does not happen properly   */  public static byte[] decryptSymmetric(byte[] data, byte[] key) throws SecurityException {    try {      key = correctLength(key, SYMMETRIC_KEY_LENGTH);      synchronized (cipherSymmetric) {        SecretKeySpec secretKey = new SecretKeySpec(key, SYMMETRIC_ALGORITHM);        cipherSymmetric.init(Cipher.DECRYPT_MODE, secretKey);        return cipherSymmetric.doFinal(data);      }    } catch (InvalidKeyException e) {      throw new SecurityException("InvalidKeyException decrypting object: " + e);    } catch (IllegalBlockSizeException e) {      throw new SecurityException("IllegalBlockSizeException decrypting object: " + e);    } catch (BadPaddingException e) {      throw new SecurityException("BadPaddingException decrypting object: " + e);    }  }  /**   * Utility method for signing a block of data with the a private key   *   * @param data The data   * @param key The key to use to sign   * @return The signature   * @exception SecurityException If the signing does not happen properly   */  public static byte[] sign(byte[] data, PrivateKey key) throws SecurityException {    try {      synchronized (signature) {        signature.initSign(key);        signature.update(hash(data));        return signature.sign();      }    } catch (InvalidKeyException e) {      throw new SecurityException("InvalidKeyException signing object: " + e);    } catch (SignatureException e) {      throw new SecurityException("SignatureException signing object: " + e);    }  }  /**   * Utility method for verifying a signature   *   * @param data The data to verify   * @param sig The proposed signature   * @param key The key to verify against   * @return Whether or not the sig matches.   * @exception SecurityException If the verification does not happen properly   */  public static boolean verify(byte[] data, byte[] sig, PublicKey key) throws SecurityException {    try {      synchronized (signature) {        signature.initVerify(key);        signature.update(hash(data));        return signature.verify(sig);      }    } catch (InvalidKeyException e) {      throw new SecurityException("InvalidKeyException verifying object: " + e);    } catch (SignatureException e) {      throw new SecurityException("SignatureException verifying object: " + e);    }  }  /**   * Encrypts the given byte[] using the provided public key. TO DO: Check   * length of input   *   * @param data The data to encrypt   * @param key The key to encrypt with   * @return The encrypted data   * @exception SecurityException If the encryption does not happen properly   */  public static byte[] encryptAsymmetric(byte[] data, PublicKey key) throws SecurityException {    try {      synchronized (cipherAsymmetric) {        cipherAsymmetric.init(Cipher.ENCRYPT_MODE, key);        return cipherAsymmetric.doFinal(data);      }    } catch (InvalidKeyException e) {      throw new SecurityException("InvalidKeyException encrypting object: " + e);    } catch (IllegalBlockSizeException e) {      throw new SecurityException("IllegalBlockSizeException encrypting object: " + e);    } catch (BadPaddingException e) {      throw new SecurityException("BadPaddingException encrypting object: " + e);    }  }  /**   * Decrypts the given byte[] using the provided private key. TO DO: Check   * length of input   *   * @param data The data to decrypt   * @param key The private key to use   * @return The decrypted data   * @exception SecurityException If the decryption does not happen properly   */  public static byte[] decryptAsymmetric(byte[] data, PrivateKey key) throws SecurityException {    try {      try {        // First try normal way of decrypting        synchronized (cipherAsymmetric) {          cipherAsymmetric.init(Cipher.DECRYPT_MODE, key);          return cipherAsymmetric.doFinal(data);        }      } catch (BadPaddingException e) {        // Trying deprecated way of decrypting        synchronized (deprecatedCipherAsymmetric) {          deprecatedCipherAsymmetric.init(Cipher.DECRYPT_MODE, key);          return deprecatedCipherAsymmetric.doFinal(data);        }      }    } catch (InvalidKeyException e) {      throw new SecurityException("InvalidKeyException decrypting object: " + e);    } catch (IllegalBlockSizeException e) {      throw new SecurityException("IllegalBlockSizeException decrypting object: " + e);    } catch (BadPaddingException e) {      throw new SecurityException("BadPaddingException decrypting object: " + e);    }  }  /**   * Utility method which will generate a non-weak DES key for applications to   * use.   *   * @return A new, random DES key   */  public static byte[] generateKeySymmetric() {    synchronized (generatorSymmetric) {      return generatorSymmetric.generateKey().getEncoded();    }  }  /**   * Utility method which will generate a non-weak DES key for applications to   * use.   *   * @return A new, random DES key   */  public static KeyPair generateKeyAsymmetric() {    synchronized (generatorAsymmetric) {      return generatorAsymmetric.generateKeyPair();    }  }  /**   * Utility method for ensuring the array is of the proper length. THis method   * enforces the length by appending 0's or returning a subset of the input   * array.   *   * @param data The input array   * @param length The length the array should be   * @return A correct-length array   */  private static byte[] correctLength(byte[] data, int length) {    byte[] result = new byte[length];    for (int i = 0; (i < data.length) && (i < result.length); i++) {      result[i] = data[i];    }    return result;  }  /**   * Initialize the ipad/opad buffers   */  static {    Arrays.fill(HMAC_IPAD, HMAC_IPAD_BYTE);    Arrays.fill(HMAC_OPAD, HMAC_OPAD_BYTE);  }  // ----- STATIC BLOCK TO INITIALIZE THE KEY GENERATORS -----  static {    // Add a provider for RSA encryption    Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 2);    try {      cipherSymmetric = Cipher.getInstance(SYMMETRIC_ALGORITHM);      cipherAsymmetric = Cipher.getInstance(ASYMMETRIC_ALGORITHM, "BC");      deprecatedCipherAsymmetric = Cipher.getInstance(DEPRECATED_ASYMMETRIC_ALGORITHM);      generatorSymmetric = KeyGenerator.getInstance(SYMMETRIC_GENERATOR);      generatorAsymmetric = KeyPairGenerator.getInstance(ASYMMETRIC_GENERATOR);      signature = Signature.getInstance(SIGNATURE_ALGORITHM);      hash = MessageDigest.getInstance(HASH_ALGORITHM);      apop = MessageDigest.getInstance(APOP_ALGORITHM);      hmac1 = MessageDigest.getInstance(HMAC_ALGORITHM);      hmac2 = MessageDigest.getInstance(HMAC_ALGORITHM);      generatorSymmetric.init(SYMMETRIC_KEY_LENGTH);    } catch (NoSuchAlgorithmException e) {      throw new SecurityException("NoSuchAlgorithmException on construction: " + e);    } catch (NoSuchPaddingException e) {      throw new SecurityException("NoSuchPaddingException on construction: " + e);    } catch (NoSuchProviderException e) {      throw new SecurityException("NoSuchProviderException on construction: " + e);    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -