当前位置: 首页 > news >正文

RSA

非对称加密算法RSA介绍

RSA:到2008年为止,世界上还没有任何可靠的攻击RSA算法的方式

秘钥对:公钥和私钥,秘钥对不能手动指定,必须有系统生成

加密速度慢:必须分段加密,不能加密大文件

公钥加密私钥解密;私钥加密公钥解密

公钥互换:连个商家合作需要交互公钥,但是私钥不能别人

非对称加密RSA生成秘钥对

不能手动指定,必须由系统生成:公钥和私钥

    public void click13(View view) {
        try {
            KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
            //generator.initialize(1024);
            KeyPair keyPair = generator.generateKeyPair();
            PrivateKey aPrivate = keyPair.getPrivate();
            PublicKey aPublic = keyPair.getPublic();
            aprivateKey = Base64.encode(aPrivate.getEncoded());//私钥字符串
            apublicKey = Base64.encode(aPublic.getEncoded());//公钥字符串
            Log.e(TAG, "click13: " + aprivateKey);
            Log.e(TAG, "click13: " + apublicKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

非对称加密RSA加密

公钥加密和私钥加密

    /**
     * 私钥加密
     *
     * @param str
     * @param privateKey
     * @return
     */
    public static String encryptByPrivateKey(String str, PrivateKey privateKey) {
        try {
            byte[] bytes = str.getBytes();
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
            int offset = 0;//偏移量
            byte[] buffer = new byte[1024];
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            while (bytes.length - offset > 0) {
                if (bytes.length - offset >= MAX_ENCRYPT_SIZE) {
                    buffer = cipher.doFinal(bytes, offset, MAX_ENCRYPT_SIZE);
                    offset += MAX_ENCRYPT_SIZE;
                } else {
                    buffer = cipher.doFinal(bytes, offset, bytes.length - offset);
                    offset = bytes.length;
                }
                bao.write(bytes);
            }
            String encode = Base64.encode(bao.toByteArray());
            return encode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 公钥加密
     *
     * @param str
     * @param publicKey
     * @return
     */
    public static String encryptByPublicKey(String str, PublicKey publicKey) {
        try {
            byte[] bytes = str.getBytes();
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            int offset = 0;//偏移量
            byte[] buffer = new byte[1024];
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            while (bytes.length - offset > 0) {
                if (bytes.length - offset >= MAX_ENCRYPT_SIZE) {
                    buffer = cipher.doFinal(bytes, offset, MAX_ENCRYPT_SIZE);
                    offset += MAX_ENCRYPT_SIZE;
                } else {
                    buffer = cipher.doFinal(bytes, offset, bytes.length - offset);
                    offset = bytes.length;
                }
                bao.write(bytes);
            }
            String encode = Base64.encode(bao.toByteArray());
            return encode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

非对称加密RSA分段加密

RSA每次最大只能加密117个字节

超过117字节,分段加密

非对称加密RSA分段解密

    /**
     * 公钥解密
     *
     * @param str
     * @param publicKey
     * @return
     */
    public static String decrypByPublicKey(String str, PublicKey publicKey) {
        try {
            byte[] bytes = Base64.decode(str);
            Cipher cipher = Cipher.getInstance(TAD);
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
            int offset = 0;
            byte[] buffer = new byte[1024];
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            while (bytes.length - offset > 0) {
                if (bytes.length - offset >= MAX_DECRYP_SIZE) {
                    buffer = cipher.doFinal(bytes, offset, MAX_DECRYP_SIZE);
                    offset += MAX_DECRYP_SIZE;
                } else {
                    buffer = cipher.doFinal(bytes, offset, bytes.length - offset);
                    offset = bytes.length;
                }
                bao.write(bytes);
            }
            String encode = bao.toString();
            return encode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 私钥解密
     *
     * @param str
     * @param privateKey
     * @return
     */
    public static String decrypByPrivateKey(String str, PrivateKey privateKey) {
        try {
            byte[] bytes = Base64.decode(str);
            Cipher cipher = Cipher.getInstance(TAD);
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            int offset = 0;
            byte[] buffer = new byte[1024];
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            while (bytes.length - offset > 0) {
                if (bytes.length - offset >= MAX_DECRYP_SIZE) {
                    buffer = cipher.doFinal(bytes, offset, MAX_DECRYP_SIZE);
                    offset += MAX_DECRYP_SIZE;
                } else {
                    buffer = cipher.doFinal(bytes, offset, bytes.length - offset);
                    offset = bytes.length;
                }
                bao.write(bytes);
            }
            String encode = bao.toString();
            return encode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

非对称加密保存秘钥对

每次都生成秘钥对:安卓加密有肯能IOS不能解密

第一次生成存储起来

    public static PrivateKey getPrivateKey(String aprivateKey) {
        if (aprivateKey != null) {
            PRIVATE_KEY = aprivateKey;
        }
        PrivateKey privateKey = null;
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(TAE);
            privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.decode(PRIVATE_KEY)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return privateKey;
    }

    public static PublicKey getPublicKey(String apublicKey) {
        if (apublicKey != null) {
            PUBLIC_KEY = apublicKey;
        }
        PublicKey publicKey = null;
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(TAE);
            publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(Base64.decode(PUBLIC_KEY)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return publicKey;
    }

 

转载于:https://www.cnblogs.com/nangongyibin/p/10391721.html

相关文章:

  • bzoj 3622 已经没有什么好害怕的了——二项式反演
  • Kendo DropDownListFor值传不回去的小坑
  • Java消息队列三道面试题详解!
  • 高性能两级缓存J2Cache
  • Webpack 4 学习01(基础配置)
  • 我的zsh配置, 2019最新方案
  • Java基础篇
  • 数据库基础SQL知识面试题二
  • 取代Python多进程!伯克利开源分布式框架Ray
  • mysql常用命令汇总
  • 前端知识点整理(待续)
  • Angular 响应式表单 基础例子
  • 2019年2月22日 807. Max Increase to Keep City Skyline
  • 微信小程序:实现悬浮返回和分享按钮
  • 巨杉中标渤海银行,股份制银行再下一城
  • 2017-09-12 前端日报
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • CODING 缺陷管理功能正式开始公测
  • github从入门到放弃(1)
  • JavaScript-Array类型
  • JavaScript对象详解
  • js
  • Otto开发初探——微服务依赖管理新利器
  • PaddlePaddle-GitHub的正确打开姿势
  • Vue实战(四)登录/注册页的实现
  • Webpack 4x 之路 ( 四 )
  • webpack4 一点通
  • webpack入门学习手记(二)
  • 分类模型——Logistics Regression
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 京东美团研发面经
  • 数组的操作
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • # 数据结构
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • $().each和$.each的区别
  • ${factoryList }后面有空格不影响
  • (3)llvm ir转换过程
  • (Git) gitignore基础使用
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (八)Flask之app.route装饰器函数的参数
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • .net CHARTING图表控件下载地址
  • .net core webapi 大文件上传到wwwroot文件夹
  • .NET Core 项目指定SDK版本
  • .NET/C# 避免调试器不小心提前计算本应延迟计算的值
  • .netcore如何运行环境安装到Linux服务器
  • .NET简谈互操作(五:基础知识之Dynamic平台调用)
  • .net开源工作流引擎ccflow表单数据返回值Pop分组模式和表格模式对比
  • @transactional 方法执行完再commit_当@Transactional遇到@CacheEvict,你的代码是不是有bug!...
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——
  • [AX]AX2012 AIF(四):文档服务应用实例