public void init(int key_size) { // KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); // keyGen.initialize(key_size, new SecureRandom()); // KeyPair pair = keyGen.generateKeyPair(); // PublicKey pubKey=pair.getPublic(); // PrivateKey prvKey=pair.getPrivate(); System.Security.Cryptography.DSACryptoServiceProvider dsa = new System.Security.Cryptography.DSACryptoServiceProvider(key_size); System.Security.Cryptography.DSAParameters DSAKeyInfo = dsa.ExportParameters(true); // x=((DSAPrivateKey)prvKey).getX().toByteArray(); // y=((DSAPublicKey)pubKey).getY().toByteArray(); // // DSAParams _params=((DSAKey)prvKey).getParams(); // p=_params.getP().toByteArray(); // q=_params.getQ().toByteArray(); // g=_params.getG().toByteArray(); x = DSAKeyInfo.X; y = DSAKeyInfo.Y; p = DSAKeyInfo.P; q = DSAKeyInfo.Q; g = DSAKeyInfo.G; }
public void init(int key_size) { // KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); // keyGen.initialize(key_size, new SecureRandom()); // KeyPair pair = keyGen.generateKeyPair(); // PublicKey pubKey=pair.getPublic(); // PrivateKey prvKey=pair.getPrivate(); System.Security.Cryptography.DSACryptoServiceProvider dsa = new System.Security.Cryptography.DSACryptoServiceProvider(key_size); System.Security.Cryptography.DSAParameters DSAKeyInfo = dsa.ExportParameters(true); // x=((DSAPrivateKey)prvKey).getX().toByteArray(); // y=((DSAPublicKey)pubKey).getY().toByteArray(); // // DSAParams _params=((DSAKey)prvKey).getParams(); // p=_params.getP().toByteArray(); // q=_params.getQ().toByteArray(); // g=_params.getG().toByteArray(); x = DSAKeyInfo.X; y = DSAKeyInfo.Y; p = DSAKeyInfo.P; q = DSAKeyInfo.Q; g = DSAKeyInfo.G; }
public bool verify(byte[] sig) { cs.Close(); System.Security.Cryptography.DSACryptoServiceProvider DSA = new System.Security.Cryptography.DSACryptoServiceProvider(); DSA.ImportParameters(DSAKeyInfo); System.Security.Cryptography.DSASignatureDeformatter DSADeformatter = new System.Security.Cryptography.DSASignatureDeformatter(DSA); DSADeformatter.SetHashAlgorithm("SHA1"); long i=0; long j=0; byte[] tmp; //This makes sure sig is always 40 bytes? if(sig[0]==0 && sig[1]==0 && sig[2]==0) { long i1 = (sig[i++]<<24)&0xff000000; long i2 = (sig[i++]<<16)&0x00ff0000; long i3 = (sig[i++]<<8)&0x0000ff00; long i4 = (sig[i++])&0x000000ff; j = i1 | i2 | i3 | i4; i+=j; i1 = (sig[i++]<<24)&0xff000000; i2 = (sig[i++]<<16)&0x00ff0000; i3 = (sig[i++]<<8)&0x0000ff00; i4 = (sig[i++])&0x000000ff; j = i1 | i2 | i3 | i4; tmp=new byte[j]; Array.Copy(sig, i, tmp, 0, j); sig=tmp; } bool res = DSADeformatter.VerifySignature(sha1, sig); return res; }
public void init(int key_size) { System.Security.Cryptography.DSACryptoServiceProvider dsa = new System.Security.Cryptography.DSACryptoServiceProvider(key_size); System.Security.Cryptography.DSAParameters DSAKeyInfo = dsa.ExportParameters(true); x = DSAKeyInfo.X; y = DSAKeyInfo.Y; p = DSAKeyInfo.P; q = DSAKeyInfo.Q; g = DSAKeyInfo.G; }
public byte[] sign() { //byte[] sig=signature.sign(); cs.Close(); System.Security.Cryptography.DSACryptoServiceProvider DSA = new System.Security.Cryptography.DSACryptoServiceProvider(); DSA.ImportParameters(DSAKeyInfo); System.Security.Cryptography.DSASignatureFormatter DSAFormatter = new System.Security.Cryptography.DSASignatureFormatter(DSA); DSAFormatter.SetHashAlgorithm("SHA1"); byte[] sig = DSAFormatter.CreateSignature(sha1); return(sig); }
public byte[] sign() { //byte[] sig=signature.sign(); cs.Close(); System.Security.Cryptography.DSACryptoServiceProvider DSA = new System.Security.Cryptography.DSACryptoServiceProvider(); DSA.ImportParameters(DSAKeyInfo); System.Security.Cryptography.DSASignatureFormatter DSAFormatter = new System.Security.Cryptography.DSASignatureFormatter(DSA); DSAFormatter.SetHashAlgorithm("SHA1"); byte[] sig =DSAFormatter.CreateSignature( sha1 ); return sig; }
public virtual byte[] Sign(byte[] message) { if (certificate.PrivateKey is System.Security.Cryptography.RSACryptoServiceProvider) { System.Security.Cryptography.RSACryptoServiceProvider rsa = (System.Security.Cryptography.RSACryptoServiceProvider)certificate.PrivateKey; return(rsa.SignData(message, hashAlgorithm)); } else { System.Security.Cryptography.DSACryptoServiceProvider dsa = (System.Security.Cryptography.DSACryptoServiceProvider)certificate.PrivateKey; return(dsa.SignData(message)); } }
public bool verify(byte[] sig) { cs.Close(); System.Security.Cryptography.DSACryptoServiceProvider DSA = new System.Security.Cryptography.DSACryptoServiceProvider(); DSA.ImportParameters(DSAKeyInfo); System.Security.Cryptography.DSASignatureDeformatter DSADeformatter = new System.Security.Cryptography.DSASignatureDeformatter(DSA); DSADeformatter.SetHashAlgorithm("SHA1"); long i = 0; long j = 0; byte[] tmp; //This makes sure sig is always 40 bytes? if (sig[0] == 0 && sig[1] == 0 && sig[2] == 0) { long i1 = (sig[i++] << 24) & 0xff000000; long i2 = (sig[i++] << 16) & 0x00ff0000; long i3 = (sig[i++] << 8) & 0x0000ff00; long i4 = (sig[i++]) & 0x000000ff; j = i1 | i2 | i3 | i4; i += j; i1 = (sig[i++] << 24) & 0xff000000; i2 = (sig[i++] << 16) & 0x00ff0000; i3 = (sig[i++] << 8) & 0x0000ff00; i4 = (sig[i++]) & 0x000000ff; j = i1 | i2 | i3 | i4; tmp = new byte[j]; Array.Copy(sig, i, tmp, 0, j); sig = tmp; } bool res = DSADeformatter.VerifySignature(sha1, sig); return(res); }
//This method will probably won't work, we need to get rid of the ASN.1 format (Tamir) public byte[] sign() { //byte[] sig=signature.sign(); cs.Close(); System.Security.Cryptography.DSACryptoServiceProvider DSA = new System.Security.Cryptography.DSACryptoServiceProvider(); DSA.ImportParameters(DSAKeyInfo); System.Security.Cryptography.DSASignatureFormatter DSAFormatter = new System.Security.Cryptography.DSASignatureFormatter(DSA); DSAFormatter.SetHashAlgorithm("SHA1"); byte[] sig =DSAFormatter.CreateSignature( sha1 ); /* System.out.print("sign["+sig.length+"] "); for(int i=0; i<sig.length;i++){ System.out.print(Integer.toHexString(sig[i]&0xff)+":"); } System.out.println(""); */ // sig is in ASN.1 // SEQUENCE::={ r INTEGER, s INTEGER } int len=0; int index=3; len=sig[index++]&0xff; //System.out.println("! len="+len); byte[] r=new byte[len]; Array.Copy(sig, index, r, 0, r.Length); index=index+len+1; len=sig[index++]&0xff; //System.out.println("!! len="+len); byte[] s=new byte[len]; Array.Copy(sig, index, s, 0, s.Length); byte[] result=new byte[40]; // result must be 40 bytes, but length of r and s may not be 20 bytes Array.Copy(r, (r.Length>20)?1:0, result, (r.Length>20)?0:20-r.Length, (r.Length>20)?20:r.Length); Array.Copy(s, (s.Length>20)?1:0, result, (s.Length>20)?20:40-s.Length, (s.Length>20)?20:s.Length); // System.arraycopy(sig, (sig[3]==20?4:5), result, 0, 20); // System.arraycopy(sig, sig.length-20, result, 20, 20); return result; }
public bool verify(byte[] sig) { cs.Close(); System.Security.Cryptography.DSACryptoServiceProvider DSA = new System.Security.Cryptography.DSACryptoServiceProvider(); DSA.ImportParameters(DSAKeyInfo); System.Security.Cryptography.DSASignatureDeformatter DSADeformatter = new System.Security.Cryptography.DSASignatureDeformatter(DSA); DSADeformatter.SetHashAlgorithm("SHA1"); int i=0; int j=0; byte[] tmp; if(sig[0]==0 && sig[1]==0 && sig[2]==0) { j=(int)((sig[i++]<<24)&0xff000000)|(byte)((sig[i++]<<16)&0x00ff0000)|(byte) ((sig[i++]<<8)&0x0000ff00)|(byte)((sig[i++])&0x000000ff); i+=j; j=(int)((sig[i++]<<24)&0xff000000)|(byte)((sig[i++]<<16)&0x00ff0000)|(byte) ((sig[i++]<<8)&0x0000ff00)|(byte)((sig[i++])&0x000000ff); tmp=new byte[j]; Array.Copy(sig, i, tmp, 0, j); sig=tmp; } // // ASN.1 // int frst=((sig[0]&0x80)!=0?1:0); // int scnd=((sig[20]&0x80)!=0?1:0); // //System.out.println("frst: "+frst+", scnd: "+scnd); // // int length=sig.Length+6+frst+scnd; // tmp=new byte[length]; // tmp[0]=(byte)0x30; tmp[1]=(byte)0x2c; // tmp[1]+=(byte)frst; tmp[1]+=(byte)scnd; // tmp[2]=(byte)0x02; tmp[3]=(byte)0x14; // tmp[3]+=(byte)frst; // Array.Copy(sig, 0, tmp, 4+frst, 20); // tmp[4+tmp[3]]=(byte)0x02; tmp[5+tmp[3]]=(byte)0x14; // tmp[5+tmp[3]]+=(byte)scnd; // Array.Copy(sig, 20, tmp, 6+tmp[3]+scnd, 20); // sig=tmp; /* tmp=new byte[sig.length+6]; tmp[0]=(byte)0x30; tmp[1]=(byte)0x2c; tmp[2]=(byte)0x02; tmp[3]=(byte)0x14; System.arraycopy(sig, 0, tmp, 4, 20); tmp[24]=(byte)0x02; tmp[25]=(byte)0x14; System.arraycopy(sig, 20, tmp, 26, 20); sig=tmp; */ //return signature.verify(sig); //Console.WriteLine("Sig Length = "+sig.Length); //bool res = DSADeformatter.VerifySignature(sha1, Util.FixDsaSig2(sig)); bool res = DSADeformatter.VerifySignature(sha1, sig); return res; }