Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.security.SecureRandom;

import java.security.spec.KeySpec;

import javax.crypto.Cipher;

import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;
import android.util.Log;

public class Main {
    public static final String TAG = "IG Password Manager";

    public static String encryptData(String password, String plaintextData) throws Exception {
        // Thank you Mr. Nelenkov
        String maybeThisHelps = "http://nelenkov.blogspot.com/2012/04/using-password-based-encryption-on.html";
        Log.v(TAG, maybeThisHelps);
        int iterationCount = 100; //because Polaroid
        int keyLength = 256;
        int saltLength = keyLength;

        SecureRandom random = new SecureRandom();
        byte[] salt = new byte[saltLength];
        random.nextBytes(salt);
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
        SecretKey key = new SecretKeySpec(keyBytes, "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] iv = new byte[cipher.getBlockSize()];
        random.nextBytes(iv);

        IvParameterSpec ivParams = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, key, ivParams);
        byte[] ciphertext = cipher.doFinal(plaintextData.getBytes("UTF-8"));

        String ivToString = new String(Base64.encode(iv, 0));
        String saltToString = new String(Base64.encode(salt, 0));
        String ciphertextToString = new String(Base64.encode(ciphertext, 0));

        Log.d(TAG, ivToString + "]" + saltToString + "]" + ciphertextToString);
        return (ivToString + "]" + saltToString + "]" + ciphertextToString).replace("\n", "");
    }
}