ecc c语言实现 源代码,[分享]经过很久的努力我终于找到了一个ecc的源代码,和大家分享~...

[分享]经过很久的努力我终于找到了一个ecc的源代码,和大家分享~

2007-4-17 17:26

19562

[分享]经过很久的努力我终于找到了一个ecc的源代码,和大家分享~

2007-4-17 17:26

19562

package org.bouncycastle.crypto.test;

import org.bouncycastle.crypto.AsymmetricCipherKeyPair;

import org.bouncycastle.crypto.BufferedBlockCipher;

import org.bouncycastle.crypto.agreement.ECDHBasicAgreement;

import org.bouncycastle.crypto.digests.SHA1Digest;

import org.bouncycastle.crypto.engines.IESEngine;

import org.bouncycastle.crypto.engines.TwofishEngine;

import org.bouncycastle.crypto.generators.KDF2BytesGenerator;

import org.bouncycastle.crypto.macs.HMac;

import org.bouncycastle.crypto.modes.CBCBlockCipher;

import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;

import org.bouncycastle.crypto.params.ECDomainParameters;

import org.bouncycastle.crypto.params.ECPrivateKeyParameters;

import org.bouncycastle.crypto.params.ECPublicKeyParameters;

import org.bouncycastle.crypto.params.IESParameters;

import org.bouncycastle.crypto.params.IESWithCipherParameters;

import org.bouncycastle.math.ec.ECCurve;

import org.bouncycastle.util.encoders.Hex;

import org.bouncycastle.util.test.SimpleTest;

import java.math.BigInteger;

/**

* test for ECIES - Elliptic Curve Integrated Encryption Scheme

*/

public class ECIESTest

extends SimpleTest

{

ECIESTest()

{

}

public String getName()

{

return "ECIES";

}

public void performTest()

throws Exception

{

ECCurve.Fp curve = new ECCurve.Fp(

new BigInteger("6277101735386680763835789423207666416083908700390324961279"), // q

new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), // a

new BigInteger("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", 16)); // b

ECDomainParameters params = new ECDomainParameters(

curve,

curve.decodePoint(Hex.decode("03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012")), // G

new BigInteger("6277101735386680763835789423176059013767194773182842284081")); // n

ECPrivateKeyParameters priKey = new ECPrivateKeyParameters(

new BigInteger("651056770906015076056810763456358567190100156695615665659"), // d

params);

ECPublicKeyParameters pubKey = new ECPublicKeyParameters(

curve.decodePoint(Hex.decode("0262b12d60690cdcf330babab6e69763b471f994dd702d16a5")), // Q

params);

AsymmetricCipherKeyPair  p1 = new AsymmetricCipherKeyPair(pubKey, priKey);

AsymmetricCipherKeyPair  p2 = new AsymmetricCipherKeyPair(pubKey, priKey);

//

// stream test

//

IESEngine      i1 = new IESEngine(

new ECDHBasicAgreement(),

new KDF2BytesGenerator(new SHA1Digest()),

new HMac(new SHA1Digest()));

IESEngine      i2 = new IESEngine(

new ECDHBasicAgreement(),

new KDF2BytesGenerator(new SHA1Digest()),

new HMac(new SHA1Digest()));

byte[]         d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

byte[]         e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };

IESParameters  p = new IESParameters(d, e, 64);

i1.init(true, p1.getPrivate(), p2.getPublic(), p);

i2.init(false, p2.getPrivate(), p1.getPublic(), p);

byte[] message = Hex.decode("1234567890abcdef");

byte[]   out1 = i1.processBlock(message, 0, message.length);

if (!areEqual(out1, Hex.decode("2442ae1fbf90dd9c06b0dcc3b27e69bd11c9aee4ad4cfc9e50eceb44")))

{

fail("stream cipher test failed on enc");

}

byte[]   out2 = i2.processBlock(out1, 0, out1.length);

if (!areEqual(out2, message))

{

fail("stream cipher test failed");

}

//

// twofish with CBC

//

BufferedBlockCipher c1 = new PaddedBufferedBlockCipher(

new CBCBlockCipher(new TwofishEngine()));

BufferedBlockCipher c2 = new PaddedBufferedBlockCipher(

new CBCBlockCipher(new TwofishEngine()));

i1 = new IESEngine(

new ECDHBasicAgreement(),

new KDF2BytesGenerator(new SHA1Digest()),

new HMac(new SHA1Digest()),

c1);

i2 = new IESEngine(

new ECDHBasicAgreement(),

new KDF2BytesGenerator(new SHA1Digest()),

new HMac(new SHA1Digest()),

c2);

d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };

p = new IESWithCipherParameters(d, e, 64, 128);

i1.init(true, p1.getPrivate(), p2.getPublic(), p);

i2.init(false, p2.getPrivate(), p1.getPublic(), p);

message = Hex.decode("1234567890abcdef");

out1 = i1.processBlock(message, 0, message.length);

if (!areEqual(out1, Hex.decode("2ea288651e21576215f2424bbb3f68816e282e3931b44bd1c429ebdb5f1b290cf1b13309")))

{

fail("twofish cipher test failed on enc");

}

out2 = i2.processBlock(out1, 0, out1.length);

if (!areEqual(out2, message))

{

fail("twofish cipher test failed");

}

}

public static void main(

String[]    args)

{

runTest(new ECIESTest());

}

}

我已经成功编译运行了,由于是初学,还没能力加注释,先和广大菜鸟们分享一下吧~

d780e0040cd3bf6260b5cadbf8ec84a7.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值