GithubHelp home page GithubHelp logo

shicuf / rsa-algorithm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from liufan321/rsa-algorithm

0.0 2.0 0.0 176 KB

RSA算法演示程序,仅供了解RSA算法实现原理

License: Apache License 2.0

Objective-C 100.00%

rsa-algorithm's Introduction

RSA-Algorithm

RSA算法演示程序,仅供了解RSA算法实现原理

RSA算法原理

  • 找出两个"很大"的质数:P & Q
  • N = P * Q
  • M = (P - 1) * (Q - 1)
  • 找出整数E,E与M互质,即除了1之外,没有其他公约数
  • 找出整数D,使得E*D除以M余1,即 (E * D) % M = 1

经过上述准备工作之后,可以得到:

  • E是公钥,负责加密
  • D是私钥,负责解密
  • N负责公钥和私钥之间的联系
  • 加密算法,假定对X进行加密
    • (X ^ E) % N = Y
  • 根据费尔马小定义,根据以下公式可以完成解密操作
    • (Y ^ D) % N = X

RSA本身算法的核心**还是比较简单的,加密、解密算法的区别也只是在乘方取模部分使用的数字有所区别而已

当然,实际运用要比示例代码复杂得多,由于RSA算法的公钥私钥的长度(模长度)要到1024位甚至2048位才能保证安全, 因此,P、Q、E的选取,公钥、私钥的生成,加密、解密模指数运算都有一定的计算程序,需要依托计算机高速运算来完成。

公开密钥的好处

  • 简单 就是一些乘除而已
  • 可靠 可以保证产生的密文是统计独立,并且分布均匀的,也就是说:
    • 不论给出多少份明文和对应的密文,也无法根据已知的明文和密文的对应关系,破译出下一份密文
    • N和E可以公开给任何人加密使用,但是只有掌握密钥D的人才可以解密,即使加密者自己也无法解密
  • 灵活 可以产生很多的公钥E和私钥D的组合给不同的加密者

测试数据说明

P = 11;
Q = 13;
N = 143;
M = 120;

E = 89;
D = 209;

提示:本示例程序仅用于演示,N的数值只有143,能够加密的字符范围有限。

致谢

本示例程序的**,参照吴军博士的《数学之美》一书,在此表示感谢!

rsa-algorithm's People

Contributors

liufan321 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.