Android examples for java.security:RSA
encrypt By RSA Public Key
//package com.java2s; import java.security.interfaces.RSAPublicKey; import javax.crypto.Cipher; public class Main { public static String encryptByPublicKey(String data, RSAPublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); // ??/*w w w. java2s. c o m*/ int key_len = publicKey.getModulus().bitLength() / 8; // ? <= ??-11 String[] datas = splitString(data, key_len - 11); String mi = ""; // ???-11 for (String s : datas) { mi += bcd2Str(cipher.doFinal(s.getBytes())); } return mi; } public static String[] splitString(String string, int len) { int x = string.length() / len; int y = string.length() % len; int z = 0; if (y != 0) { z = 1; } String[] strings = new String[x + z]; String str = ""; for (int i = 0; i < x + z; i++) { if (i == x + z - 1 && y != 0) { str = string.substring(i * len, i * len + y); } else { str = string.substring(i * len, i * len + len); } strings[i] = str; } return strings; } public static String bcd2Str(byte[] bytes) { char temp[] = new char[bytes.length * 2], val; for (int i = 0; i < bytes.length; i++) { val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f); temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); val = (char) (bytes[i] & 0x0f); temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); } return new String(temp); } }