Beispiel #1
0
 /**
  * Parses the given ADAT response line and base64-decodes the data.
  *
  * @param reply The ADAT reply to parse.
  * @return the data in the reply, base64-decoded.
  * @since 3.0
  */
 public byte[] parseADATReply(String reply) {
   if (reply == null) {
     return null;
   } else {
     return Base64.decodeBase64(extractPrefixedData("ADAT=", reply));
   }
 }
  /**
   * Authenticate to the IMAP server by sending the AUTHENTICATE command with the selected
   * mechanism, using the given username and the given password.
   *
   * <p>
   *
   * @return True if successfully completed, false if not.
   * @exception IOException If an I/O error occurs while either sending a command to the server or
   *     receiving a reply from the server.
   * @exception NoSuchAlgorithmException If the CRAM hash algorithm cannot be instantiated by the
   *     Java runtime system.
   * @exception InvalidKeyException If the CRAM hash algorithm failed to use the given password.
   * @exception InvalidKeySpecException If the CRAM hash algorithm failed to use the given password.
   */
  public boolean auth(AuthenticatingIMAPClient.AUTH_METHOD method, String username, String password)
      throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
    if (!IMAPReply.isContinuation(sendCommand(IMAPCommand.AUTHENTICATE, method.getAuthName()))) {
      return false;
    }

    switch (method) {
      case PLAIN:
        {
          // the server sends an empty response ("+ "), so we don't have to read it.
          int result =
              sendData(
                  new String(
                      Base64.encodeBase64(("\000" + username + "\000" + password).getBytes())));
          if (result == IMAPReply.OK) {
            setState(IMAP.IMAPState.AUTH_STATE);
          }
          return result == IMAPReply.OK;
        }
      case CRAM_MD5:
        {
          // get the CRAM challenge (after "+ ")
          byte[] serverChallenge = Base64.decodeBase64(getReplyString().substring(2).trim());
          // get the Mac instance
          Mac hmac_md5 = Mac.getInstance("HmacMD5");
          hmac_md5.init(new SecretKeySpec(password.getBytes(), "HmacMD5"));
          // compute the result:
          byte[] hmacResult = _convertToHexString(hmac_md5.doFinal(serverChallenge)).getBytes();
          // join the byte arrays to form the reply
          byte[] usernameBytes = username.getBytes();
          byte[] toEncode = new byte[usernameBytes.length + 1 /* the space */ + hmacResult.length];
          System.arraycopy(usernameBytes, 0, toEncode, 0, usernameBytes.length);
          toEncode[usernameBytes.length] = ' ';
          System.arraycopy(hmacResult, 0, toEncode, usernameBytes.length + 1, hmacResult.length);
          // send the reply and read the server code:
          int result = sendData(new String(Base64.encodeBase64(toEncode)));
          if (result == IMAPReply.OK) {
            setState(IMAP.IMAPState.AUTH_STATE);
          }
          return result == IMAPReply.OK;
        }
      case LOGIN:
        {
          // the server sends fixed responses (base64("Username") and
          // base64("Password")), so we don't have to read them.
          if (sendData(new String(Base64.encodeBase64(username.getBytes()))) != IMAPReply.CONT) {
            return false;
          }
          int result = sendData(new String(Base64.encodeBase64(password.getBytes())));
          if (result == IMAPReply.OK) {
            setState(IMAP.IMAPState.AUTH_STATE);
          }
          return result == IMAPReply.OK;
        }
    }
    return false; // safety check
  }
 /**
  * 加密数据
  *
  * @param data 待加密数据
  * @param key 密钥
  * @return 加密后的数据
  */
 public static String encrypt(String data, String key) throws Exception {
   Key k = toKey(Base64.decodeBase64(key)); // 还原密钥
   // 使用PKCS7Padding填充方式,这里就得这么写了(即调用BouncyCastle组件实现)
   // Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
   Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); // 实例化Cipher对象,它用于完成实际的加密操作
   cipher.init(Cipher.ENCRYPT_MODE, k); // 初始化Cipher对象,设置为加密模式
   return Base64.encodeBase64String(
       cipher.doFinal(data.getBytes())); // 执行加密操作。加密后的结果通常都会用Base64编码进行传输
 }
 private Map<String, String> getAsMap() throws IOException, XPathExpressionException {
   InputStream zipInputStream =
       new ZipInputStream(new ByteArrayInputStream(Base64.decodeBase64(encodedZip)));
   ZipEntry zipFileEntry;
   Map<String, String> map = new HashMap<>();
   while ((zipFileEntry = ((ZipInputStream) zipInputStream).getNextEntry()) != null) {
     if (!zipFileEntry.getName().endsWith("-site.xml")) {
       continue;
     }
     byte[] bytes = IOUtils.toByteArray(zipInputStream);
     InputSource is = new InputSource(new ByteArrayInputStream(bytes));
     XPath xPath = XPathFactory.newInstance().newXPath();
     NodeList nodeList =
         (NodeList)
             xPath.evaluate(ConfigConstants.CONF_PROPERTY_XPATH, is, XPathConstants.NODESET);
     for (int i = 0; i < nodeList.getLength(); i++) {
       Node propNode = nodeList.item(i);
       String key = (String) xPath.evaluate("name/text()", propNode, XPathConstants.STRING);
       String value = (String) xPath.evaluate("value/text()", propNode, XPathConstants.STRING);
       map.put(key, value);
     }
   }
   return map;
 }
Beispiel #5
0
 public static SymmetricSecretKey valueOf(String key) {
   return decode(Base64.decodeBase64(key));
 }
Beispiel #6
0
 public static String decrypt(String value) {
   if (!value.isEmpty()) {
     return new String(Base64.decodeBase64(value));
   }
   return value;
 }
Beispiel #7
0
 public static String decodificarUrl(String urlCoded) {
   return new String(Base64.decodeBase64(urlCoded.getBytes()));
 }
 /**
  * 解密数据
  *
  * @param data 待解密数据
  * @param key 密钥
  * @return 解密后的数据
  */
 public static String decrypt(String data, String key) throws Exception {
   Key k = toKey(Base64.decodeBase64(key));
   Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
   cipher.init(Cipher.DECRYPT_MODE, k); // 初始化Cipher对象,设置为解密模式
   return new String(cipher.doFinal(Base64.decodeBase64(data))); // 执行解密操作
 }