java中非对称加密(RSA)的使用

由于android和后台进行传输数据为了数据的安全我们一般都会对数据进行加密处理,一般比较常用的都是对称加密(DES)。这样可以防止攻击者截获网络包获取数据。但是这这种方式也是有风险,就是如果攻击者反编译了你的代码,就可以获取你的秘钥那么就可以解析你的数据了。
所以我们在传输及其重要的数据的时候会使用非对称加密,非对称加密分为公钥和私钥,用公钥进行加密,用秘钥进行解密。一般是客户端用公钥钥把数据加密后传给后台,后台再用私钥进行解密。这样就算攻击者反编译了你的代码也无法解密
使用方式如下
public class TestRAS {
    /** 指定加密算法为RSA */
    private static final String ALGORITHM = "RSA";
    /** 密钥长度,用来初始化 */
    private static final int KEYSIZE = 512;
    static String privateKeyString;
    static String publicKeyString;
    /**
     * 得到秘钥的方法
     * 生成的公钥和私钥记录下来用于以后使用
     * @throws Exception
     */
  private static void generateKeyPair() throws Exception {

        /** RSA算法要求有一个可信任的随机数源 */
        SecureRandom secureRandom = new SecureRandom();

        /** 为RSA算法创建一个KeyPairGenerator对象 */
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);

        /** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */
        keyPairGenerator.initialize(KEYSIZE, secureRandom);

        /** 生成密匙对 */
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        /** 得到公钥 */
        PublicKey publicKey = keyPair.getPublic();

        /** 得到私钥 */
        PrivateKey privateKey = keyPair.getPrivate();

        /**得到字符串*/
        privateKeyString=getKeyString(privateKey);
        publicKeyString=getKeyString(publicKey);
        System.out.println("公钥:"+publicKeyString);
        System.out.println("私钥:"+privateKeyString);

    }
  /** 
   * 得到公钥 
   *  
   * @param key 
   *            密钥字符串(经过base64编码) 
   * @throws Exception 
   */  
  public static PublicKey getPublicKey(String key) throws Exception {  
      byte[] keyBytes;  
      keyBytes = (new BASE64Decoder()).decodeBuffer(key);  
      X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);  
      KeyFactory keyFactory = KeyFactory.getInstance("RSA");  
      PublicKey publicKey = keyFactory.generatePublic(keySpec);  
      return publicKey;  
  }  

  /** 
   * 得到私钥 
   *  
   * @param key 
   *            密钥字符串(经过base64编码) 
   * @throws Exception 
   */  
  public static PrivateKey getPrivateKey(String key) throws Exception {  
      byte[] keyBytes;  
      keyBytes = (new BASE64Decoder()).decodeBuffer(key);  
      PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);  
      KeyFactory keyFactory = KeyFactory.getInstance("RSA");  
      PrivateKey privateKey = keyFactory.generatePrivate(keySpec);  
      return privateKey;  
  }  
  /** 
   * 得到密钥字符串(经过base64编码) 
   *  
   * @return 
   */  
  public static String getKeyString(Key key) throws Exception {  
      byte[] keyBytes = key.getEncoded();  
      String s = (new BASE64Encoder()).encode(keyBytes);  
      return s;  
  }     

  /**
   * 加密方法
   * @param source 源数据
   * @return
   * @throws Exception
   */
  public static String encrypt(String source) throws Exception {
      generateKeyPair();
      PublicKey publicKey = null;
      ObjectInputStream ois = null;
//      publicKey=getPublicKey(publicKeyString);
      publicKey=getPublicKey(publicKeyString);//通过公钥字符生成公钥对象

      /** 得到Cipher对象来实现对源数据的RSA加密 */
      Cipher cipher = Cipher.getInstance(ALGORITHM);
      cipher.init(Cipher.ENCRYPT_MODE, publicKey);
      byte[] b = source.getBytes();
      /** 执行加密操作 */
      byte[] b1 = cipher.doFinal(b);
      BASE64Encoder encoder = new BASE64Encoder();
      return encoder.encode(b1);
  }

  /**
   * 解密算法
   * @param cryptograph    密文
   * @return
   * @throws Exception
   */
  public static String decrypt(String cryptograph) throws Exception {
      PrivateKey privateKey;
      privateKey=getPrivateKey(privateKeyString);//通过私钥字符获取私钥对象
      /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */
      Cipher cipher = Cipher.getInstance(ALGORITHM);
      cipher.init(Cipher.DECRYPT_MODE, privateKey);
      BASE64Decoder decoder = new BASE64Decoder();
      byte[] b1 = decoder.decodeBuffer(cryptograph);

      /** 执行解密操作 */
      byte[] b = cipher.doFinal(b1);
      return new String(b);
  }

  public static void main(String[] args) throws Exception {
      String source = "恭喜发财!";// 要加密的字符串
      System.out.println("准备用公钥加密的字符串为:" + source);

      String cryptograph = encrypt(source);// 生成的密文
      System.out.print("用公钥加密后的结果为:" + cryptograph);
      System.out.println();

      String target = decrypt(cryptograph);// 解密密文
      System.out.println("用私钥解密后的字符串为:" + target);
      System.out.println();
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值