RSAアルゴリズムの紹介とJAVA実現
:
Posted by zeal on 2005-05-17 16:38 , 13469 characters | + 0 - 1 English
:
http://www.zeali.net/entry/58
MaDe1nZEAL
( JAVA/C++ ): rsa , , , download
RSA , , , 。
< >
RSA , :
p q
n=p*q
t=(p-1)*(q-1)
e, e<t e t ( 1)
d*e%t==1
: n d e
M (M <n)
c=(M**d)%n c
m=(c**e)%n m == M, c 。
:** , d e 。
:
n d , ;
n e ,e , 。
e , d , 。
d , e 。
rsa n,
n d e; n e
d。
< >
, :
:
p=47
q=59
n=p*q=2773
t=(p-1)*(q-1)=2668
e=63, e<t e t
perl e*d%t ==1 d:
C:\Temp>perl -e "foreach $i (1..9999){ print($i),last if $i*63%2668==1 }"
847
d=847
n=2773
d=847
e=63
M=244
:
c=M**d%n = 244**847%2773
perl :
C:\Temp>perl -Mbigint -e "print 244**847%2773"
465
d M c=465
:
e c , M:
m=c**e%n=465**63%2773 :
C:\Temp>perl -Mbigint -e "print 465**63%2773"
244
e c m=244 , M 。
< >
。
ascii M , 16
, 3 , 01F
:
#!/usr/bin/perl -w
#RSA
#watercloud 2003-8-12
#
use strict;
use Math::BigInt;
my %RSA_CORE = (n=>2773,e=>63,d=>847); #p=47,q=59
my $N=new Math::BigInt($RSA_CORE{n});
my $E=new Math::BigInt($RSA_CORE{e});
my $D=new Math::BigInt($RSA_CORE{d});
print "N=$N D=$D E=$E
";
sub RSA_ENCRYPT
{
my $r_mess = shift @_;
my ($c,$i,$M,$C,$cmess);
for($i=0;$i < length($$r_mess);$i++)
{
$c=ord(substr($$r_mess,$i,1));
$M=Math::BigInt->new($c);
$C=$M->copy(); $C->bmodpow($D,$N);
$c=sprintf "%03X",$C;
$cmess.=$c;
}
return \$cmess;
}
sub RSA_DECRYPT
{
my $r_mess = shift @_;
my ($c,$i,$M,$C,$dmess);
for($i=0;$i < length($$r_mess);$i+=3)
{
$c=substr($$r_mess,$i,3);
$c=hex($c);
$M=Math::BigInt->new($c);
$C=$M->copy(); $C->bmodpow($E,$N);
$c=chr($C);
$dmess.=$c;
}
return \$dmess;
}
my $mess="RSA ~~~";
$mess=$ARGV[0] if @ARGV >= 1;
print " :",$mess,"
";
my $r_cmess = RSA_ENCRYPT(\$mess);
print " :",$$r_cmess,"
";
my $r_dmess = RSA_DECRYPT($r_cmess);
print " :",$$r_dmess,"
";
#EOF
:
C:\Temp>perl rsa-test.pl
N=2773 D=847 E=63
:RSA ~~~
:5CB6CD6BC58A7709470AA74A0AA74A0AA74A6C70A46C70A46C70A4
:RSA ~~~
C:\Temp>perl rsa-test.pl (xfocus)
N=2773 D=847 E=63
: (xfocus)
:3393EC12F0A466E0AA9510D025D7BA0712DC3379F47D51C325D67B
: (xfocus)
< >
,rsa n , n , ,
RSAKit、RSATool N D E。
, 1024 N D E :
n=0x328C74784DF31119C526D18098EBEBB943B0032B599CEE13CC2BCE7B5FCD15F90B66EC3A85F5005D
BDCDED9BDFCB3C4C265AF164AD55884D8278F791C7A6BFDAD55EDBC4F017F9CCF1538D4C2013433B383B
47D80EC74B51276CA05B5D6346B9EE5AD2D7BE7ABFB36E37108DD60438941D2ED173CCA50E114705D7E2
BC511951
d=0x10001
e=0xE760A3804ACDE1E8E3D7DC0197F9CEF6282EF552E8CEBBB7434B01CB19A9D87A3106DD28C523C2995
4C5D86B36E943080E4919CA8CE08718C3B0930867A98F635EB9EA9200B25906D91B80A47B77324E66AFF2
C4D70D8B1C69C50A9D8B4B7A3C9EE05FFF3A16AFC023731D80634763DA1DCABE9861A4789BD782A592D2B
1965
M=0x11111111111122222222222233333333333
, perl :
A) d M :
c=M**d%n :
C:\Temp>perl -Mbigint -e " $x=Math::BigInt->bmodpow(0x11111111111122222222222233
333333333, 0x10001, 0x328C74784DF31119C526D18098EBEBB943B0032B599CEE13CC2BCE7B5F
CD15F90B66EC3A85F5005DBDCDED9BDFCB3C4C265AF164AD55884D8278F791C7A6BFDAD55EDBC4F0
17F9CCF1538D4C2013433B383B47D80EC74B51276CA05B5D6346B9EE5AD2D7BE7ABFB36E37108DD6
0438941D2ED173CCA50E114705D7E2BC511951);print $x->as_hex"
0x17b287be418c69ecd7c39227ab681ac422fcc84bb35d8a632543b304de288a8d4434b73d2576bd
45692b007f3a2f7c5f5aa1d99ef3866af26a8e876712ed1d4cc4b293e26bc0a1dc67e247715caa6b
3028f9461a3b1533ec0cb476441465f10d8ad47452a12db0601c5e8beda686dd96d2acd59ea89b91
f1834580c3f6d90898
d M :
c=0x17b287be418c69ecd7c39227ab681ac422fcc84bb35d8a632543b304de288a8d4434b73d2576bd
45692b007f3a2f7c5f5aa1d99ef3866af26a8e876712ed1d4cc4b293e26bc0a1dc67e247715caa6b
3028f9461a3b1533ec0cb476441465f10d8ad47452a12db0601c5e8beda686dd96d2acd59ea89b91
f1834580c3f6d90898
B) e c :
m=c**e%n :
C:\Temp>perl -Mbigint -e " $x=Math::BigInt->bmodpow(0x17b287be418c69ecd7c39227ab
681ac422fcc84bb35d8a632543b304de288a8d4434b73d2576bd45692b007f3a2f7c5f5aa1d99ef3
866af26a8e876712ed1d4cc4b293e26bc0a1dc67e247715caa6b3028f9461a3b1533ec0cb4764414
65f10d8ad47452a12db0601c5e8beda686dd96d2acd59ea89b91f1834580c3f6d90898, 0xE760A
3804ACDE1E8E3D7DC0197F9CEF6282EF552E8CEBBB7434B01CB19A9D87A3106DD28C523C29954C5D
86B36E943080E4919CA8CE08718C3B0930867A98F635EB9EA9200B25906D91B80A47B77324E66AFF
2C4D70D8B1C69C50A9D8B4B7A3C9EE05FFF3A16AFC023731D80634763DA1DCABE9861A4789BD782A
592D2B1965, 0x328C74784DF31119C526D18098EBEBB943B0032B599CEE13CC2BCE7B5FCD15F90
B66EC3A85F5005DBDCDED9BDFCB3C4C265AF164AD55884D8278F791C7A6BFDAD55EDBC4F017F9CCF
1538D4C2013433B383B47D80EC74B51276CA05B5D6346B9EE5AD2D7BE7ABFB36E37108DD60438941
D2ED173CCA50E114705D7E2BC511951);print $x->as_hex"
0x11111111111122222222222233333333333
( P4 1.6G 5 )
e m=0x11111111111122222222222233333333333 == M
C) RSA
RSA , , RSA ,
, ,
RSA 。
, 1024 N
1024 RSA, 2048 。
----------------------------------------------------------
RSA JAVA :
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 :
JavaFR_RSA_Source.rar
2. http://www.ferrara.linux.it/Members/lucabariani/RSA/implementazioneRsa/ :
sorgentiJava.tar.gz -
algoritmoRSA.jar - jar
RSA php : php RSA
VB RSA ( psc1 RSA ):
vb_PSC1_RSA.rar
RSA JavaScript : http://www.ohdave.com/rsa/