Android常用的数据加密方式代码详解

  /**************************************************

  * 1.什么是RSA 非对称加密?

  *

  * 2.

  *************************************************/

  private final static String RSA = "RSA";

  //加密方式 RSA

  public final static int DEFAULT_KEY_SIZE = 1024;

  private final static int DECRYPT_LEN = DEFAULT_KEY_SIZE / 8;

  //解密长度

  private final static int ENCRYPT_LEN = DECRYPT_LEN - 11;

  //加密长度

  private static final String DES_CBC_PKCS5PAD = "DES/CBC/PKCS5Padding";

  //加密填充方式

  private final static int MODE_PRIVATE = 1;

  //私钥加密

  private final static int MODE_PUBLIC = 2;

  //公钥加密

  /**

  * 随机生成RSA密钥对,包括PublicKey,PrivateKey

  *

  * @param keyLength 秘钥长度,范围是 512~2048,一般是1024

  * @return KeyPair

  */

  public static KeyPair generateRSAKeyPair(int keyLength) {

  try {

  KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");

  kpg.initialize(keyLength);

  return kpg.genKeyPair();

  }

  catch (NoSuchAlgorithmException e) {

  e.printStackTrace();

  return null;

  }

  }

  /**

  * 得到私钥

  *

  * @return PrivateKey

  * @throws NoSuchAlgorithmException

  * @throws InvalidKeySpecException

  */

  public static PrivateKey getPrivateKey(String key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {

  byte[] privateKey = Base64.decode(key, Base64.URL_SAFE);

  PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);

  KeyFactory kf = KeyFactory.getInstance(RSA);

  return kf.generatePrivate(keySpec);

  }

  /**

  * 得到公钥

  *

  * @param key

  * @return PublicKey

  * @throws NoSuchAlgorithmException

  * @throws InvalidKeySpecException

  */

  public static PublicKey getPublicKey(String key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {

  byte[] publicKey = Base64.decode(key, Base64.URL_SAFE);

  X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);

  KeyFactory kf = KeyFactory.getInstance(RSA);

  return kf.generatePublic(keySpec);

  }

  /**

  * 私钥加密

  *

  * @param data

  * @param key

  * @return

  * @throws Exception

  */

  public static byte[] encryptByRSA(byte[] data, Key key) throws Exception {

  // 数据加密

  Cipher cipher = Cipher.getInstance(RSA);

  cipher.init(Cipher.ENCRYPT_MODE, key);

  return cipher.doFinal(data);

  }

  /**

  * 公钥解密

  *

  * @param data 待解密数据

  * @param key 密钥

  * @return byte[] 解密数据

  */

  public static byte[] decryptByRSA(byte[] data, Key key) throws Exception {

  // 数据解密

  Cipher cipher = Cipher.getInstance(RSA);

  cipher.init(Cipher.DECRYPT_MODE, key);

  return cipher.doFinal(data);

  }

  public static void encryptByRSA(String source, String dest, Key key) {

  rasEncrypt(MODE_ENCRYPTION, source, dest, key);

  }

  public static void decryptByRSA(String source, String dest, Key key) {

  rasEncrypt(MODE_DECRYPTION, source, dest, key);

  }

  public static void rasEncrypt(int mode, String source, String dest, Key key) {

  Log.i(TAG, "start===encryptByRSA mode--->>" + mode);

  FileInputStream fis = null;

  FileOutputStream fos = null;

  try {

  fis = new FileInputStream(source);

  fos = new FileOutputStream(dest);

  int size = mode == MODE_ENCRYPTION ? ENCRYPT_LEN : DECRYPT_LEN;

  byte buff[] = new byte[size];

  byte buffResult[];

  while ((fis.read(buff)) != -1) {

  buffResult = mode == MODE_ENCRYPTION ? encryptByRSA(buff, key) : decryptByRSA(buff, key);

  if (buffResult != null) {

  fos.write(buffResult);

  }

  }

  Log.i(TAG, "end===encryptByRSA");

  }

  catch (IOException e) {

  e.printStackTrace();

  Log.e(TAG, "encryptByRSA failed err: " + e.getMessage());

  }

  catch (Exception e) {

  e.printStackTrace();

  }

  finally {

  try {

  if (fis != null) {

  fis.close();

  }

  if (fos != null) {

  fos.close();

  }

  }

  catch (IOException e) {

  e.printStackTrace();

  }

  }

  }