Android RSA 加解密

一、RSA简介

RSA是一种常用的非对称加密算法,所谓非对称加密是指使用一对密钥(公钥和私钥)进行加密和解密,公钥人人都可以获得,用于加密数据,私钥保存在服务器中,用于解密数据。加密解密过程如下:

在这里插入图片描述
使用RSA进行加密解密,其优点是非常不容易破解,缺点是和对称加密(如AES)相比,加密速度较慢。因此,实际使用中,常常将对称加密和非对称加密结合使用,即使用非对称加密协商对称加密的密钥,使用对称加密密钥加密传输内容。

二、RSA 原理介绍

RSA是目前最有影响力的公钥加密算法,该算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但想要对其乘积进行因式分解却极其困 难,因此可以将乘积公开作为加密密钥,即公钥,而两个大素数组合成私钥。公钥是可发布的供任何人使用,私钥则为自己所有

三、RSA 秘钥对生成

1. 密钥对生成

 private static KeyPair genKeyPair() {
        try {
            KeyPairGenerator keyPairGen = null;
            keyPairGen = KeyPairGenerator.getInstance("RSA");
            keyPairGen.initialize(2048, new SecureRandom());
            return keyPairGen.generateKeyPair();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

2. 获取公钥

 public static PublicKey getPublicKey(KeyPair keyPair){
        return  keyPair.getPublic();
    }

3. 获取私钥

 public static PrivateKey getPrivateKey(KeyPair keyPair){
        return  keyPair.getPrivate();
    }

四、PublicKey 和PrivateKey 的保存

1. 获取公钥十六进制字符串

  public static  String getHexStrPublicKey(PublicKey publicKey){
        byte[] publicKeyEncoded = publicKey.getEncoded();
        return  ConvectionUtils.byte2HexStr(publicKeyEncoded);
    }

1. 获取私钥十六进制字符串

public static  String getHexStrPrivateKey(PrivateKey privateKey){
    byte[] privateKeyEncoded = privateKey.getEncoded();
    return  ConvectionUtils.byte2HexStr(privateKeyEncoded);
}

五、PublicKey 和 PrivateKey 加载

1. 加载公钥

  public static PublicKey loadPublicKey(String publicKeyStr) throws Exception {
        try {
            byte[] buffer = ConvectionUtils.hexStr2Bytes(publicKeyStr);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
            return keyFactory.generatePublic(keySpec);
        } catch (Exception e) {
           e.printStackTrace();
        }

        return null;
    }

2. 加载私钥

    public static PrivateKey loadPrivateKey(String privateKeyStr) throws Exception {
        try {
            byte[] buffer =ConvectionUtils.hexStr2Bytes(privateKeyStr);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
           return keyFactory.generatePrivate(keySpec);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

六、 RSA加解密

1. RSA 支持三种加密方式

  • RSA/ECB/PKCS1Padding (1024, 2048)
  • RSA/ECB/OAEPWithSHA-1AndMGF1Padding (1024, 2048)
  • RSA/ECB/OAEPWithSHA-256AndMGF1Padding (1024, 2048)

2.RSA加密

 public static byte[] encrypt(PublicKey publicKey, byte[] plainTextData)  {
        if (publicKey == null || plainTextData == null) {
            return null;
        }
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            return cipher.doFinal(plainTextData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

3. RSA解密

public static byte[] decrypt(RSAPrivateKey privateKey, byte[] cipherData)  {
        if (privateKey == null || cipherData == null) {
            return null;
        }
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            return cipher.doFinal(cipherData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    } 

RSA在线加密解密

七、实例代码

AndroidEncryption

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幸福在路上wellbeing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值