本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7587891
建立时间:2006年5月29日




[J2SE]一个简单的RSA算法实现JAVA源代码
软件技术,  电脑与网络

lhwork 发表于 2006/6/27 15:42:18

filename:RSA.java /** Created on Mar 3, 2005** TODO To change the template for this generated file go to* Window - Preferences - Java - Code Style - Code Templates*/import java.math.BigInteger;import java.io.InputStream;import java.io.OutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.FileWriter;import java.io.FileReader;import java.io.BufferedReader;import java.util.StringTokenizer;/*** @author Steve** TODO To change the template for this generated type comment go to* Window - Preferences - Java - Code Style - Code Templates*/public class RSA {       /**     * BigInteger.ZERO     */    private static final BigInteger ZERO = BigInteger.ZERO;       /**     * BigInteger.ONE     */    private static final BigInteger ONE = BigInteger.ONE;       /**     * Pseudo BigInteger.TWO     */    private static final BigInteger TWO = new BigInteger("2");       private BigInteger myKey;       private BigInteger myMod;       private int blockSize;       public RSA (BigInteger key, BigInteger n, int b) {        myKey = key;        myMod = n;        blockSize = b;    }       public void encodeFile (String filename) {        byte[] bytes = new byte[blockSize / 8 + 1];        byte[] temp;        int tempLen;        InputStream is = null;        FileWriter writer = null;        try {             is = new FileInputStream(filename);             writer = new FileWriter(filename + ".enc");        }        catch (FileNotFoundException e1){            System.out.println("File not found: " + filename);        }        catch (IOException e1){            System.out.println("File not found: " + filename + ".enc");        }               /**         * Write encoded message to 'filename'.enc         */        try {            while ((tempLen = is.read(bytes, 1, blockSize / 8)) > 0) {                for (int i = tempLen + 1; i < bytes.length; ++i) {                    bytes[i] = 0;                }                 writer.write(encodeDecode(new BigInteger(bytes)) + " ");            }        }        catch (IOException e1) {            System.out.println("error writing to file");        }               /**         * Close input stream and file writer         */        try {            is.close();            writer.close();        }        catch (IOException e1) {            System.out.println("Error closing file.");        }    }       public void decodeFile (String filename) {               FileReader reader = null;        OutputStream os = null;        try {            reader = new FileReader(filename);            os = new FileOutputStream(filename.replaceAll(".enc", ".dec"));        }        catch (FileNotFoundException e1) {            if (reader == null)                System.out.println("File not found: " + filename);            else                System.out.println("File not found: " + filename.replaceAll(".enc", "dec"));        }               BufferedReader br = new BufferedReader(reader);        int offset;        byte[] temp, toFile;        StringTokenizer st = null;        try {            while (br.ready()) {                st = new StringTokenizer(br.readLine());                while (st.hasMoreTokens()){                    toFile = encodeDecode(new BigInteger(st.nextToken())).toByteArray();                    System.out.println(toFile.length + " x " + (blockSize / 8));                                       if (toFile[0] == 0 && toFile.length != (blockSize / 8)) {                        temp = new byte[blockSize / 8];                        offset = temp.length - toFile.length;                         for (int i = toFile.length - 1; (i <= 0) && ((i + offset) <= 0); --i) {                            temp[i + offset] = toFile[i];                        }                        toFile = temp;                    }                                       /*if (toFile.length != ((blockSize / 8) + 1)){                        temp = new byte[(blockSize / 8) + 1];                        System.out.println(toFile.length + " x " + temp.length);                        for (int i = 1; i < temp.length; i++) {                            temp[i] = toFile[i - 1];                        }                        toFile = temp;                    }                    else                        System.out.println(toFile.length + " " + ((blockSize / 8) + 1));*/                    os.write(toFile);                }            }        }        catch (IOException e1) {            System.out.println("Something went wrong");        }               /**         * close data streams         */        try {            os.close();            reader.close();        }        catch (IOException e1) {            System.out.println("Error closing file.");        }    }       /**     * Performs <tt>base</tt>^<sup><tt>pow</tt></sup> within the modular     * domain of <tt>mod</tt>.     *     * @param base the base to be raised     * @param pow the power to which the base will be raisded     * @param mod the modular domain over which to perform this operation     * @return <tt>base</tt>^<sup><tt>pow</tt></sup> within the modular     * domain of <tt>mod</tt>.     */    public BigInteger encodeDecode(BigInteger base) {        BigInteger a = ONE;        BigInteger s = base;        BigInteger n = myKey;               while (!n.equals(ZERO)) {            if(!n.mod(TWO).equals(ZERO))                a = a.multiply(s).mod(myMod);                       s = s.pow(2).mod(myMod);            n = n.divide(TWO);        }               return a;    }   } 在这里提供两个版本的RSA算法JAVA实现的代码下载: 1. 来自于 http://www.javafr.com/code.aspx?ID=27020 的RSA算法实现源代码包:       http://zeal.newmenbase.net/attachment/JavaFR_RSA_Source.rar  2. 来自于 http://www.ferrara.linux.it/Members/lucabariani/RSA/implementazioneRsa/ 的实现:       http://zeal.newmenbase.net/attachment/sorgentiJava.tar.gz  - 源代码包       http://zeal.newmenbase.net/attachment/algoritmoRSA.jar - 编译好的jar包 另外关于RSA算法的php实现请参见文章:       php下的RSA算法实现 关于使用VB实现RSA算法的源代码下载(此程序采用了psc1算法来实现快速的RSA加密):        http://zeal.newmenbase.net/attachment/vb_PSC1_RSA.rar RSA加密的JavaScript实现: http://www.ohdave.com/rsa/


阅读全文(4656) | 回复(1) | 编辑 | 精华
 


回复:一个简单的RSA算法实现JAVA源代码
软件技术,  电脑与网络

nono(游客)发表评论于2006/10/24 23:43:30

为什么这个程序编译成功后还是不能运行出现异常??


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.078 second(s), page refreshed 144767024 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号