예제 #1
0
 /**
  * 私钥加密
  *
  * @param data 源数据
  * @param privateKey 私钥(BASE64编码)
  * @return
  * @throws Exception
  */
 public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception {
   byte[] keyBytes = Base64.decode(privateKey);
   PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
   KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
   Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
   Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
   cipher.init(Cipher.ENCRYPT_MODE, privateK);
   int inputLen = data.length;
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   int offSet = 0;
   byte[] cache;
   int i = 0;
   // 对数据分段加密
   while (inputLen - offSet > 0) {
     if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
       cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
     } else {
       cache = cipher.doFinal(data, offSet, inputLen - offSet);
     }
     out.write(cache, 0, cache.length);
     i++;
     offSet = i * MAX_ENCRYPT_BLOCK;
   }
   byte[] encryptedData = out.toByteArray();
   out.close();
   return encryptedData;
 }
예제 #2
0
 /**
  * 公钥解密
  *
  * @param encryptedData 已加密数据
  * @param publicKey 公钥(BASE64编码)
  * @return
  * @throws Exception
  */
 public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception {
   byte[] keyBytes = Base64Utils.decode(publicKey);
   X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
   KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
   Key publicK = keyFactory.generatePublic(x509KeySpec);
   Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
   cipher.init(Cipher.DECRYPT_MODE, publicK);
   int inputLen = encryptedData.length;
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   int offSet = 0;
   byte[] cache;
   int i = 0;
   // 对数据分段解密
   while (inputLen - offSet > 0) {
     if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
       cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
     } else {
       cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
     }
     out.write(cache, 0, cache.length);
     i++;
     offSet = i * MAX_DECRYPT_BLOCK;
   }
   byte[] decryptedData = out.toByteArray();
   out.close();
   return decryptedData;
 }
예제 #3
0
 // 私钥加密
 public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception {
   PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
   KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
   PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
   Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
   cipher.init(Cipher.ENCRYPT_MODE, privateKey);
   return cipher.doFinal(data);
 }
예제 #4
0
 // 公钥加密
 public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception {
   X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
   KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
   PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
   Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
   cipher.init(Cipher.ENCRYPT_MODE, publicKey);
   return cipher.doFinal(data);
 }
예제 #5
0
 public static byte[] encryptByPublicKey(byte[] paramArrayOfByte, String paramString)
     throws Exception {
   X509EncodedKeySpec localX509EncodedKeySpec = new X509EncodedKeySpec(decryptBASE64(paramString));
   KeyFactory localKeyFactory = KeyFactory.getInstance("RSA");
   PublicKey localPublicKey = localKeyFactory.generatePublic(localX509EncodedKeySpec);
   Cipher localCipher = Cipher.getInstance(localKeyFactory.getAlgorithm());
   localCipher.init(1, localPublicKey);
   return localCipher.doFinal(paramArrayOfByte);
 }
예제 #6
0
  /**
   * 用私钥解密通过公钥加密的数据
   *
   * @param data 通过公钥加密的数据
   * @param key 用来解密的私钥
   * @return 解密后的数据
   * @throws NoSuchAlgorithmException 假如用户的JDK不支持RSA
   * @throws InvalidKeySpecException 假如根据privateKey生成密钥失败
   * @throws InvalidKeyException 假如输入的RSA私钥不合法
   * @throws SignatureException 假如根据privateKey生成密钥失败
   * @throws UnsupportedEncodingException 假如privateKey不是使用UTF-8进行编码
   * @throws NoSuchPaddingException 假如产生的密钥对有问题
   * @throws BadPaddingException 假如输入的加密的数据填充数据错误
   * @throws IllegalBlockSizeException 假如输入的加密的数据字节数不是BlockSize的整数倍
   * @throws ShortBufferException
   */
  public static byte[] decryptByPrivateKey(byte[] data, Key privateKey)
      throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException,
          SignatureException, UnsupportedEncodingException, NoSuchPaddingException,
          IllegalBlockSizeException, BadPaddingException, ShortBufferException {
    // 对数据解密
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    return CipherUtil.process(cipher, 128, data);
  }
예제 #7
0
  /**
   * 用公钥解密
   *
   * @param data 加密数据
   * @param key 密钥
   * @return
   * @throws Exception
   */
  public static byte[] decryptByPublicKey(byte[] data, String key) throws Exception {
    // 对私钥解密
    byte[] keyBytes = decryptBASE64(key);
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

    // 对数据解密
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, publicKey);

    return cipher.doFinal(data);
  }
예제 #8
0
  /**
   * 用私钥加密
   *
   * @param data 加密数据
   * @param key 密钥
   * @return
   * @throws Exception
   */
  public static byte[] encryptByPrivateKey(byte[] data, String key) throws Exception {
    // 解密密钥
    byte[] keyBytes = decryptBASE64(key);
    // 取私钥
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);

    // 对数据加密
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, privateKey);

    return cipher.doFinal(data);
  }
예제 #9
0
  /**
   * 用私钥加密数据
   *
   * @param data 等待加密的原始数据
   * @param key 用来加密的私钥
   * @return 加密后的数据
   * @throws NoSuchAlgorithmException 假如用户的JDK不支持RSA
   * @throws InvalidKeySpecException 假如根据privateKey生成密钥失败
   * @throws InvalidKeyException 假如输入的RSA私钥不合法
   * @throws SignatureException 假如根据privateKey生成密钥失败
   * @throws UnsupportedEncodingException 假如privateKey不是使用UTF-8进行编码
   * @throws NoSuchPaddingException 假如产生的密钥对有问题
   * @throws BadPaddingException 假如输入的加密的数据填充数据错误
   * @throws IllegalBlockSizeException 假如输入的加密的数据字节数不是BlockSize的整数倍
   * @throws ShortBufferException
   */
  public static byte[] encryptByPrivateKey(byte[] data, String key)
      throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException,
          SignatureException, UnsupportedEncodingException, NoSuchPaddingException,
          IllegalBlockSizeException, BadPaddingException, ShortBufferException {
    // 对密钥解密
    byte[] keyBytes = Base64ForServer.base64toByte(key);

    // 取得私钥
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

    // 对数据加密
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, privateKey);

    return CipherUtil.process(cipher, 117, data);
  }
예제 #10
0
  /**
   * 用公钥解密通过私钥加密的数据
   *
   * @param data 通过私钥加密的数据
   * @param key 用来解密的公钥
   * @return 解密后的数据
   * @throws NoSuchAlgorithmException 假如用户的JDK不支持RSA
   * @throws InvalidKeySpecException 假如根据privateKey生成密钥失败
   * @throws InvalidKeyException 假如输入的RSA私钥不合法
   * @throws SignatureException 假如根据privateKey生成密钥失败
   * @throws UnsupportedEncodingException 假如privateKey不是使用UTF-8进行编码
   * @throws NoSuchPaddingException 假如产生的密钥对有问题
   * @throws BadPaddingException 假如输入的加密的数据填充数据错误
   * @throws IllegalBlockSizeException 假如输入的加密的数据字节数不是BlockSize的整数倍
   * @throws ShortBufferException
   */
  public static byte[] decryptByPublicKey(byte[] data, String key)
      throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException,
          SignatureException, UnsupportedEncodingException, NoSuchPaddingException,
          IllegalBlockSizeException, BadPaddingException, ShortBufferException {
    // 对密钥解密
    byte[] keyBytes = Base64ForServer.base64toByte(key);

    // 取得公钥
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key publicKey = keyFactory.generatePublic(x509KeySpec);

    // 对数据解密
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, publicKey);

    return CipherUtil.process(cipher, 128, data);
  }
예제 #11
0
 /**
  * 使用私钥进行解密
  *
  * @param date
  * @param privateKey
  * @return
  * @throws Exception
  */
 private static byte[] decryptByPrivateKey(byte[] date, PrivateKey privateKey) throws Exception {
   KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
   Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
   cipher.init(Cipher.DECRYPT_MODE, privateKey);
   return cipher.doFinal(date);
 }