package com.linekong.sdk.utils;

import java.security.KeyFactory;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import android.util.Base64;

/**
 * @author changcsw 2015-11-11
 * 
 */
public class RSAUtils {
    private static RSAPublicKey publicKey = null;
    /**************************** RSA 公钥加密解密**************************************/
    /**
     * 从字符串中加载公钥,从服务端获取
     * 
     * @param publicKeyStr
     *            公钥数据字符串
     * @throws Exception
     *             加载公钥时产生的异常
     */
    public static void loadPublicKey(String pubKey) {
        try {
            byte[] buffer = Base64.decode(pubKey, Base64.DEFAULT);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
            publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 公钥加密过程
     * 
     * @param publicKey
     *            公钥
     * @param plainData
     *            明文数据
     * @return
     * @throws Exception
     *             加密过程中的异常信息
     */
    public static String encryptWithRSA(String plainData) throws Exception {
        if (publicKey == null) {
            throw new NullPointerException("encrypt PublicKey is null !");
        }

        Cipher cipher = null;
        cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");// 此处如果写成"RSA"加密出来的信息JAVA服务器无法解析

        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] output = cipher.doFinal(plainData.getBytes("utf-8"));
        // 必须先encode成 byte[],再转成encodeToString,否则服务器解密会失败
        byte[] encode = Base64.encode(output, Base64.DEFAULT);
        return Base64.encodeToString(encode, Base64.DEFAULT);
    }

    /**
     * 公钥解密过程
     * 
     * @param publicKey
     *            公钥
     * @param encryedData
     *            明文数据
     * @return
     * @throws Exception
     *             加密过程中的异常信息
     */
    public static String decryptWithRSA(String encryedData) throws Exception {
        if (publicKey == null) {
            throw new NullPointerException("decrypt PublicKey is null !");
        }

        Cipher cipher = null;
        cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");// 此处如果写成"RSA"解析的数据前多出来些乱码
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        byte[] output = cipher.doFinal(Base64.decode(encryedData, Base64.DEFAULT));
        return new String(output);
    }
    /**************************** RSA 公钥加密解密**************************************/
}

更多相关文章

  1. Android拍照上传至PHP服务器并写入MySql数据库(下)
  2. 【android】当数据库需要更新时我们该怎么办?
  3. Android之解析JSON数据示例(android原生态,FastJson,Gson)
  4. Android 顶部下拉刷新添加数据&& 底部上拉刷新添加数据 ScrollLi
  5. android中的sqlite数据库加密
  6. 使用Vitamio打造自己的Android万能播放器(4)——本地播放(快捷搜索
  7. Android webview与js 交换JSON对象数据

随机推荐

  1. [React-Native]环境安装问题
  2. Android中的对话框AlertDialog使用技巧合
  3. Android(安卓)控件之ImageSwitcher图片切
  4. Android通过泛型来简化findViewById
  5. Android(安卓)Fragment回退栈的使用
  6. 【Android(安卓)初学】9、Activity生命周
  7. Android(安卓)Vibrator开启振动功能
  8. android计数器——基础编
  9. android 线程更新view及数据传送
  10. OnConfigurationChanged 配置变化。 屏幕