00001 using System;
00002
00003 namespace Tamir.SharpSsh.jsch.jce
00004 {
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 public class SignatureRSA : Tamir.SharpSsh.jsch.SignatureRSA
00035 {
00036
00037
00038
00039 System.Security.Cryptography.RSAParameters RSAKeyInfo;
00040 System.Security.Cryptography.SHA1CryptoServiceProvider sha1;
00041 System.Security.Cryptography.CryptoStream cs;
00042
00043 public void init()
00044 {
00045
00046
00047 sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
00048 cs = new System.Security.Cryptography.CryptoStream(System.IO.Stream.Null, sha1, System.Security.Cryptography.CryptoStreamMode.Write);
00049 }
00050 public void setPubKey(byte[] e, byte[] n)
00051 {
00052
00053
00054
00055
00056
00057 RSAKeyInfo.Modulus = Util.stripLeadingZeros( n );
00058 RSAKeyInfo.Exponent = e;
00059
00060
00061 }
00062 public void setPrvKey(byte[] d, byte[] n)
00063 {
00064
00065
00066
00067
00068
00069
00070 RSAKeyInfo.D = d ;
00071 RSAKeyInfo.Modulus = n ;
00072 }
00073
00074 public void setPrvKey(byte[] e, byte[] n, byte[] d, byte[] p, byte[] q, byte[] dp, byte[] dq, byte[] c)
00075 {
00076 RSAKeyInfo.Exponent = e ;
00077 RSAKeyInfo.D = Util.stripLeadingZeros( d ) ;
00078 RSAKeyInfo.Modulus = Util.stripLeadingZeros( n ) ;
00079 RSAKeyInfo.P = Util.stripLeadingZeros(p);
00080 RSAKeyInfo.Q = Util.stripLeadingZeros(q);
00081 RSAKeyInfo.DP = Util.stripLeadingZeros(dp);
00082 RSAKeyInfo.DQ = Util.stripLeadingZeros(dq);
00083 RSAKeyInfo.InverseQ = Util.stripLeadingZeros(c);
00084 }
00085
00086 public void setPrvKey(System.Security.Cryptography.RSAParameters keyInfo)
00087 {
00088
00089
00090
00091
00092
00093 RSAKeyInfo = keyInfo;
00094 }
00095
00096 public byte[] sign()
00097 {
00098
00099
00100 cs.Close();
00101 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
00102 RSA.ImportParameters(RSAKeyInfo);
00103 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
00104 RSAFormatter.SetHashAlgorithm("SHA1");
00105
00106 byte[] sig = RSAFormatter.CreateSignature( sha1 );
00107 return sig;
00108
00109
00110 }
00111 public void update(byte[] foo)
00112 {
00113
00114 cs.Write( foo , 0, foo.Length);
00115 }
00116 public bool verify(byte[] sig)
00117 {
00118 cs.Close();
00119 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
00120 RSA.ImportParameters(RSAKeyInfo);
00121 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
00122 RSADeformatter.SetHashAlgorithm("SHA1");
00123
00124
00125 long i=0;
00126 long j=0;
00127 byte[] tmp;
00128
00129
00130
00131 if(sig[0]==0 && sig[1]==0 && sig[2]==0)
00132 {
00133 long i1 = (sig[i++]<<24)&0xff000000;
00134 long i2 = (sig[i++]<<16)&0x00ff0000;
00135 long i3 = (sig[i++]<<8)&0x0000ff00;
00136 long i4 = (sig[i++])&0x000000ff;
00137 j = i1 | i2 | i3 | i4;
00138
00139 i+=j;
00140
00141 i1 = (sig[i++]<<24)&0xff000000;
00142 i2 = (sig[i++]<<16)&0x00ff0000;
00143 i3 = (sig[i++]<<8)&0x0000ff00;
00144 i4 = (sig[i++])&0x000000ff;
00145 j = i1 | i2 | i3 | i4;
00146
00147 tmp=new byte[j];
00148 Array.Copy(sig, i, tmp, 0, j); sig=tmp;
00149 }
00150
00151
00152 bool verify = RSADeformatter.VerifySignature(sha1, sig);
00153 return verify;
00154 }
00155 }
00156
00157 }