Android开发中我们经常会用到各种加密,一般针对一些密码加密,下面给说一下RSA加密与SHA256算法的使用方法:
附加RSA加密jar包:点击打开链接
public class RsaHelper {/**
     *
     * rsa加密
     * @param s
     * @param publicKey
     * @return
     * @throws InvalidCipherTextException
     * @throws IOException
     */
    public static String encrypt(String s, String publicKey) throws InvalidCipherTextException, IOException {AsymmetricBlockCipher engine = new PKCS1Encoding(new RSAEngine());
        AsymmetricKeyParameter parameters = GetPublicKeyParameter(publicKey);
        engine.init(true, parameters);
        byte[] byteData = s.getBytes();
        byte[] ResultData = engine.processBlock(byteData, 0, byteData.length);
        return Base64.encodeToString((ResultData), Base64.NO_WRAP);
    }/**
     *
     * rsa解密
     * @param s
     * @param privateKey
     * @return
     * @throws InvalidCipherTextException
     * @throws IOException
     */
    public static String decrypt(String s, String privateKey) throws InvalidCipherTextException, IOException {AsymmetricBlockCipher engine = new PKCS1Encoding(new RSAEngine());
        AsymmetricKeyParameter parameters = GetPrivateKeyParameter(privateKey);
        engine.init(false, parameters);
        byte[] byteData = Base64.decode(s,Base64.NO_WRAP);
        byte[] ResultData = engine.processBlock(byteData, 0, byteData.length);
        return new String(ResultData);
    }private static AsymmetricKeyParameter GetPrivateKeyParameter(String key) throws IOException {String keypriv = key.replace("\r", "").replace("\n", "").replace(" ", "");
        byte[] privateInfoByte = Base64.decode(keypriv,Base64.NO_WRAP);
        AsymmetricKeyParameter priKey = PrivateKeyFactory.createKey(privateInfoByte);
        return priKey;
    }private static AsymmetricKeyParameter GetPublicKeyParameter(String key) throws IOException {String keypub = key.replace("\r", "").replace("\n", "").replace(" ", "");
        byte[] publicInfoByte = Base64.decode(keypub,Base64.NO_WRAP);
        AsymmetricKeyParameter pubKey = PublicKeyFactory.createKey(publicInfoByte);
        return pubKey;
    }/**
     * SHA256加密
     * @param content
     * @param secret
     * @return
     */
    public static String encrytSHA256(String content, String secret) {try {Security.addProvider(new BouncyCastleProvider());
            SecretKey secretKey = new SecretKeySpec(secret.getBytes(),
                    "HmacSHA256");
            Mac mac = Mac.getInstance(secretKey.getAlgorithm());
            mac.init(secretKey);
            byte[] digest = mac.doFinal(content.getBytes());
            String sig = Base64.encodeToString((digest), Base64.NO_WRAP);
            return sig;
        } catch (Exception e) {throw new RuntimeCryptoException("加密异常");
        }}
}

Android RSA加密与SHA256算法工具类相关推荐

  1. Android RSA 加密

    没想到被Android里的RSA加密折腾了几个小时,主要还是自己对RSA加密的原理不了解,然后网上相关的资料也少. 使用AndroidUtilCode工具类中的EncryptUtils.encrypt ...

  2. Android 开发人员不得不收集的工具类集合

    RxTools 项目地址:vondear/RxTools  简介:Android 开发人员不得不收集的工具类集合 | 支付宝支付 | 微信支付(统一下单) | 微信分享 | Zip4j 压缩(支持分卷 ...

  3. Java实现Google的S2算法工具类

    WGS84坐标系 GCJ02坐标系 BD09坐标系的各种转换 WGS84坐标系 GCJ02坐标系 BD09坐标系的各种转换 Google S2 经纬度 转 CellId 经纬度 转 cellToken ...

  4. 国密算法java语言的实现:利用bcprov和hutool库分别实现国密SM4算法工具类,对称密钥

    SM4算法成为行业标准: SM4分组密码算法是2012年3月21日实施的一项行业标准: 2021年6月25日,我国SM4分组密码算法作为国际标准ISO/IEC 18033-3:2010/AMD1:20 ...

  5. 基于Python实现的RSA加密和解密算法

    目录 第 一部分 RSA 加密和解密算法 1 算法原理 1 1.1 RSA 原理概述 1 用中国剩余定理CRT对RSA运算进行加 2 代码实现 2 2.1 RSA 代码实现 2 2.2 CRT-RSA ...

  6. Android PermissionUtils:运行时权限工具类及申请权限的正确姿势

    Android PermissionUtils:运行时权限工具类及申请权限的正确姿势 ifadai 关注 2017.06.16 16:22* 字数 318 阅读 3637评论 1喜欢 6 Permis ...

  7. Android开发人员不得不收集的工具类集合

    一.Android开发人员不得不收集的工具类集合  ----收藏 https://github.com/RobertCow/RxTools 二.android 开发之listview工具集合框架 ht ...

  8. Android RSA加密解密的 工具类的使用

    RSA 比较特殊,我们首先要生成私钥和公钥,然后在加密的时候,使用私钥加密,在解密的时候使用公钥解密. //RSA 的初始化,获得私钥和密钥public void rsaInit(){try {Key ...

  9. android rsa加密工具类,GitHub - Lerist/encrypt: Android 加密解密工具包。

    Encrypt(加密工具) 字符串,byte[],文件等对象的加密和解密工具集合,包含了多种加密方案. 加密类型 摘要 相关方法 简单加密 换一种编码格式 Base64Util 单向加密 只能加密,不 ...

最新文章

  1. ImageView 常用属性的分析
  2. Zora P1测评报告-Tsrjrc
  3. c语言学习自我评价,学习自我评价4篇
  4. 华为云MVP程云:知识化转型,最终要赋能一线
  5. Android 系统(184)---Android APN 配置
  6. ARCGIS 分区统计的时候,出现无法解决的重大问题,程序崩溃
  7. Wifi Enable 启动过程
  8. RHEL 7 使用 CentOS 源安装 docker ce
  9. Win10(Win7)局域网设置共享文件夹,超全面步骤。
  10. 查看MXNet模型结构
  11. LabVIEW与Matlab混合编程进行图像处理(附带颜色栏Colorbar)
  12. 租服务器的 直连100m是啥,如何知道我的服务器带宽是独享10M或者100M?
  13. Kernel:里的某某某;xxx
  14. 新能源汽车车载智能终端t-box
  15. 【每日一题】字符串转换成整数
  16. DiI(细胞膜红色荧光探针)被广泛应用于神经等细胞或组织的示踪剂或长期示踪剂
  17. 用C#(入门) 双色球摇奖机 随机数实现
  18. 安全设计规范_山西火灾原因查明:安全检查不到位?设计规范问题为何要统归为安全问题?...
  19. Win11系统自带截图快捷键是什么 Win11系统自带截图工具怎么使用
  20. 元旦立下的Flag,春节后该如何实现?

热门文章

  1. 如何使用 win32com 操作excel 入坑
  2. Android安全学习笔记1——锁屏密码方式
  3. 2021全网最全Activiti7教程02(Activiti7入门使用-欢迎收藏)
  4. 华为的数通认证考试难不难?考试费用是多少?
  5. Acrobat 9 Pro安装教程
  6. IMX8QX 编译环境搭建(不用yocto)
  7. Google开除网志爆料员工
  8. 不锈钢旗杆的制作与安装
  9. 理解“护城河”和“压倒性优势”
  10. html5美食与茶页面,内容二 - html5茶艺食品网站模板