免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1382 | 回复: 0
打印 上一主题 下一主题

C Crypt Interoperate with Java Crypt [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-10-22 11:34 |只看该作者 |倒序浏览

There are many open source today.  this also works for the crypt projects.
Yet the  different crypt implementation have some compatibility issue between them.
1. Different implementations
C Crypt implementation
http://www.libtomcrypt.org/
Sun Crypt implematation:
http://java.sun.com
Bouncy Castle Crypt implematation:
http://www.bouncycastle.org/

2. Compatibilty issue.
[color="#800080"]libtomcrypt generate DSA key format can not be supported by java DSA implemation.
[color="#800080"]while bouncycastle do.
[color="#800080"]3. Implementation code.
import java.math.*;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.crypto.signers.DSASigner;
import org.bouncycastle.crypto.digests.SHA1Digest;
public class SignatureVerifier
{
   private DSASigner dsaSigner = null;

   /** Build Public Key object from the y/p/q/g data files */
   public SignatureVerifier()
      throws FileNotFoundException, IOException
   {
      // Load key parameters from data files
      BigInteger y = readBigInt("y.dat");
      BigInteger p = readBigInt("p.dat");
      BigInteger q = readBigInt("q.dat");
      BigInteger g = readBigInt("g.dat");
     System.out.println( "Y: " + y + "
P: " + p + "
Q: " + q + "
G: " + g);
      DSAParameters dsaParams = new DSAParameters( p, q, g);
      DSAPublicKeyParameters dsaPubParams = new DSAPublicKeyParameters( y, dsaParams );
      dsaSigner = new DSASigner();
      dsaSigner.init( false, dsaPubParams );
   }
   /** Verify the header data
       headerData should be
   */
   public boolean verify( byte[] headerData, byte[] signature )
   {
      boolean valid = false;
      // Build SHA1 digest from the passed in header data
      SHA1Digest sha1 = new SHA1Digest();
      sha1.update( headerData, 0, headerData.length );
            
      byte[] sha1hash = new byte[ sha1.getDigestSize() ];
      sha1.doFinal( sha1hash, 0 );
      // Decompose signature into the r and s values
      byte[] rBytes = new byte[20];
      byte[] sBytes = new byte[20];
      System.arraycopy( signature, 0, rBytes, 0, 20);
      System.arraycopy( signature, 20, sBytes, 0, 20);
      BigInteger r = new BigInteger( 1, rBytes );
      BigInteger s = new BigInteger( 1, sBytes );
      valid = dsaSigner.verifySignature( sha1hash, r, s );
      return(valid);
   }
   /** Reads a BigInteger from a binary file */
   static public BigInteger readBigInt( String filename )  
          throws FileNotFoundException, IOException
   {
       FileInputStream intFIS = new FileInputStream( filename );
       int numBytes = intFIS.available();
       byte[] fileBytes = new byte[numBytes];
       intFIS.read( fileBytes );
       intFIS.close();
       BigInteger bigInt = new BigInteger( 1, fileBytes );
       return(bigInt);
   }
   public static void main( String args[])
   throws Exception
   {
       SignatureVerifier sigVerify = new SignatureVerifier();
    try{
            // Read signature from test file
            byte[] signature =  new byte[40];
            FileInputStream fis = new FileInputStream( "test.txt.sig" );
            fis.read(signature );
            fis.close();
            // Read content file (simulated header)
            byte[] header = new byte[140];
            fis = new FileInputStream( "test.txt" );
            fis.read( header );
            fis.close();
         
                       boolean sigOkay = sigVerify.verify( header, signature );
            System.out.println("signatureOkay=" + sigOkay );
           }catch(Exception e){
               e.printStackTrace();
        }
   }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/9924/showart_52953.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP