博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RSA
阅读量:6232 次
发布时间:2019-06-21

本文共 6293 字,大约阅读时间需要 20 分钟。

非对称加密算法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

你可能感兴趣的文章
USACO2.3 Money Systems(money)
查看>>
css3各浏览器圆角
查看>>
android 事件分发机制
查看>>
将语法从词法解析器中分离出来
查看>>
SpringBoot基础篇AOP之基本使用姿势小结
查看>>
Android中AsyncTask的简单用法
查看>>
Android 获取dimen值
查看>>
Camel In Action 读书笔记 (9)
查看>>
Gallery实现首页图片滑动源码
查看>>
ehlib 修改 使指示区背景色 和 数据区 背景色一致
查看>>
关于MySQL的init-file选项的用法实例
查看>>
对memcached使用的总结和使用场景
查看>>
Python 当前时间增加或减少一个月
查看>>
CollectionUtil
查看>>
平衡二叉树
查看>>
Android应用开发新路线
查看>>
smartHost 北京服务器
查看>>
制作自己的网络字体
查看>>
Xcode的包管理器:Alcatraz
查看>>
WinForms Adorner UI Manager v16.1支持高亮特定控件
查看>>