Java tutorial
//package com.java2s; import java.security.Key; import java.security.PublicKey; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import android.util.Base64; public class Main { public static PublicKey publicKey = null; public static String encryption(String string) throws Exception { // TODO Auto-generated method stub //generate symmetric key KeyGenerator keygt = KeyGenerator.getInstance("AES"); keygt.init(128); SecretKey symkey = keygt.generateKey(); //get it encoded byte[] aes_ba = symkey.getEncoded(); //create cipher SecretKeySpec skeySpec = new SecretKeySpec(aes_ba, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); //encryption byte[] EncSymbyteArray = cipher.doFinal(string.getBytes()); //encrypt symKey with PublicKey // Key pubKey = getPublicKey(); Key pubKey = publicKey; //RSA cipher Cipher cipherAsm = Cipher.getInstance("RSA", "BC"); cipherAsm.init(Cipher.ENCRYPT_MODE, pubKey); //RSA encryption byte[] asymEncsymKey = cipherAsm.doFinal(aes_ba); // File f3 = new File(BASE_PATH,"enc.txt"); // File f3key = new File(BASE_PATH,"enckey.txt"); // File f3file = new File(BASE_PATH,"encfile.txt"); // writeToFile2(f3,f3key,f3file, asymEncsymKey, EncSymbyteArray); //byte != new String //return new String(byteMerger(asymEncsymKey, EncSymbyteArray)); return Base64.encodeToString(byteMerger(asymEncsymKey, EncSymbyteArray), Base64.DEFAULT); } public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) { byte[] byte_3 = new byte[byte_1.length + byte_2.length]; System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length); System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length); return byte_3; } }