读书人

,把所有分都给了

发布时间: 2012-01-10 21:26:51 作者: rapoo

请教高手,把所有分都给了!
这是一个ELGAMAL签名算法,这里有五个类,我想问下怎么能实现他们之间互相调用,还有就是在运行3和4时提示不是抽象的,并且未覆盖java.security.key中的抽象方法getencoded() public class ELGamalPublicKey/ELGamalPrivateKey.
在运行5的时候提示使用或覆盖了已过时的API,请高手帮我看看是怎么回事,谢谢各位了!!
1.
import java.math.BigInteger;
import java.security.*;

public class ElGamalKey
implements Key {
private BigInteger mP, mG;

protected ElGamalKey(BigInteger g, BigInteger p) {
mG = g;
mP = p;
}

protected BigInteger getG() { return mG; }
protected BigInteger getP() { return mP; }

public String getAlgorithm() { return "ElGamal "; }
public String getFormat() { return "NONE "; }
public byte[] getEncoded() { return null; }
}
2.
import java.math.BigInteger;
import java.security.*;

public class ElGamalKeyPairGenerator
extends KeyPairGeneratorSpi {
private int mStrength = 0;
private SecureRandom mSecureRandom = null;

// Strength is interpreted as the bit length of p.
public void initialize(int strength, SecureRandom random) {
mStrength = strength;
mSecureRandom = random;
}

public KeyPair generateKeyPair() {
if (mSecureRandom == null) {
mStrength = 1024;
mSecureRandom = new SecureRandom();
}
BigInteger p = new BigInteger(mStrength, 16, mSecureRandom);
BigInteger g = new BigInteger(mStrength - 1, mSecureRandom);
BigInteger x = new BigInteger(mStrength - 1, mSecureRandom);
BigInteger y = g.modPow(x, p);

ElGamalPublicKey publicKey = new ElGamalPublicKey(y, g, p);
ElGamalPrivateKey privateKey = new ElGamalPrivateKey(x, g, p);
return new KeyPair(publicKey, privateKey);
}
}
3.
import java.math.BigInteger;
import java.security.*;

public class ElGamalPrivateKey
extends ElGamalKey
implements PrivateKey {
private BigInteger mX;



protected ElGamalPrivateKey(BigInteger x, BigInteger g, BigInteger p) {
super(g, p);
mX = x;
}

protected BigInteger getX() { return mX; }
}
4.
import java.math.BigInteger;
import java.security.*;

public class ElGamalPublicKey
extends ElGamalKey
implements PublicKey {
private BigInteger mY;

protected ElGamalPublicKey(BigInteger y, BigInteger g, BigInteger p) {
super(g, p);
mY = y;
}

protected BigInteger getY() { return mY; }
}
5.
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.security.*;

public class ElGamalSignature
extends SignatureSpi {

protected ElGamalKey mKey;

protected ByteArrayOutputStream mOut;

protected static BigInteger kOne = BigInteger.valueOf(1);

protected void engineInitVerify(PublicKey key)
throws InvalidKeyException {
if (!(key instanceof ElGamalPublicKey))
throw new InvalidKeyException( "I didn 't get an ElGamalPublicKey. ");
mKey = (ElGamalKey)key;
mOut = new ByteArrayOutputStream();
}

protected void engineInitSign(PrivateKey key) throws InvalidKeyException {
if (!(key instanceof ElGamalPrivateKey))
throw new InvalidKeyException( "I didn 't get an ElGamalPrivateKey. ");
mKey = (ElGamalKey)key;
mOut = new ByteArrayOutputStream();
}

protected void engineUpdate(byte b) throws SignatureException {
mOut.write(b);
}

protected void engineUpdate(byte[] b, int off, int len)
throws SignatureException {
mOut.write(b, off, len);
}

protected byte[] engineSign() throws SignatureException {
BigInteger x = ((ElGamalPrivateKey)mKey).getX();
BigInteger g = mKey.getG();
BigInteger p = mKey.getP();
BigInteger pminusone = p.subtract(kOne);



BigInteger k;
do {
k = new BigInteger(p.bitLength() - 1, new SecureRandom());
} while (k.gcd(pminusone).equals(kOne) == false);

BigInteger m = new BigInteger(1, mOut.toByteArray());

BigInteger a = g.modPow(k, p);
BigInteger top = m.subtract(x.multiply(a)).mod(pminusone);
BigInteger b = top.multiply(
k.modPow(kOne.negate(), pminusone)).mod(pminusone);

int modulusLength = (p.bitLength() + 7) / 8;
byte[] signature = new byte[modulusLength * 2];
byte[] aBytes = getBytes(a);
int aLength = aBytes.length;
byte[] bBytes = getBytes(b);
int bLength = bBytes.length;
System.arraycopy(aBytes, 0,
signature, modulusLength - aLength, aLength);
System.arraycopy(bBytes, 0,
signature, modulusLength * 2 - bLength, bLength);
return signature;
}

protected boolean engineVerify(byte[] sigBytes)
throws SignatureException {
BigInteger y = ((ElGamalPublicKey)mKey).getY();
BigInteger g = mKey.getG();
BigInteger p = mKey.getP();

int modulusLength = (p.bitLength() + 7) / 8;
byte[] aBytes = new byte[modulusLength];
byte[] bBytes = new byte[modulusLength];
System.arraycopy(sigBytes, 0, aBytes, 0, modulusLength);
System.arraycopy(sigBytes, modulusLength, bBytes, 0, modulusLength);
BigInteger a = new BigInteger(1, aBytes);
BigInteger b = new BigInteger(1, bBytes);

BigInteger first = y.modPow(a, p).multiply(a.modPow(b, p)).mod(p);

BigInteger m = new BigInteger(1, mOut.toByteArray());
BigInteger second = g.modPow(m,p);

return first.equals(second);
}

protected byte[] getBytes(BigInteger big) {
byte[] bigBytes = big.toByteArray();


if ((big.bitLength() % 8) != 0) {
return bigBytes;
}
else {
byte[] smallerBytes = new byte[big.bitLength() / 8];
System.arraycopy(bigBytes, 1, smallerBytes, 0, smallerBytes.length);
return smallerBytes;
}
}

protected void engineSetParameter(String param, Object value)
throws InvalidParameterException {}
protected Object engineGetParameter(String param)
throws InvalidParameterException { return null; }
}


6.还有我想问问这段代码是什么意思啊,他只是个提示信息吗?
import java.security.*;

public class Provider
extends java.security.Provider {
public Provider() {
super ( "Jonathan ",
1.2,
"Jonathan 's Cryptography Provider ");

put( "KeyPairGenerator.ElGamal ",
"oreilly.jonathan.crypto.ElGamalKeyPairGenerator ");
put( "Cipher.ElGamal ", "oreilly.jonathan.crypto.ElGamalCipher ");
put( "Signature.ElGamal ", "oreilly.jonathan.crypto.ElGamalSignature ");

put( "Cipher.DES/CBC/PKCS5Padding ",
"oreilly.jonathan.crypto.CBCWrapper ");
put( "Cipher.DES/CFB/NoPadding ", "oreilly.jonathan.crypto.CFBWrapper ");

put( "Alg.Alias.Cipher.DES/CFB8/NoPadding ", "DES/CFB/NoPadding ");
}
}




[解决办法]
up
[解决办法]
没有做过,帮你顶

[解决办法]
mark
[解决办法]
没有做过,大概意思如下
import java.security.*;

public class Provider
extends java.security.Provider {//继承Provider
public Provider() {
super ( "Jonathan ",
1.2,
"Jonathan 's Cryptography Provider ");//调用父类构造器

put( "KeyPairGenerator.ElGamal ",
"oreilly.jonathan.crypto.ElGamalKeyPairGenerator ");//调用父类方法
put( "Cipher.ElGamal ", "oreilly.jonathan.crypto.ElGamalCipher ");
put( "Signature.ElGamal ", "oreilly.jonathan.crypto.ElGamalSignature ");

put( "Cipher.DES/CBC/PKCS5Padding ",


"oreilly.jonathan.crypto.CBCWrapper ");
put( "Cipher.DES/CFB/NoPadding ", "oreilly.jonathan.crypto.CFBWrapper ");

put( "Alg.Alias.Cipher.DES/CFB8/NoPadding ", "DES/CFB/NoPadding ");
}
}


此类表示 Java 安全 API "provider ",这里 provider 实现了 Java 安全性的一部分或者全部。provider 可能实现的服务包括:

算法(如 DSA、RSA、MD5 或 SHA-1)。
密钥的生成、转换和管理设施(如用于特定于算法的密钥)。
每个 provider 有一个名称和一个版本号,并且在每个它装入运行时中进行配置。

有关特定类型的 provider、加密服务 provider 如何工作和安装的信息,请参阅 "Java Cryptography Architecture API Specification & Reference " 中的 The Provider Class。但是,请注意 provider 能够被用来实现 Java 中的任何安全服务,这些安全服务使用带有适合下层的实现选择的可插入架构。

某些 provider 的实现在操作时可能遇到不可恢复的内部错误,例如与安全性令牌通信的错误。应该使用 ProviderException 指示此类错误。

put(Object key, Object value)
设置 key 属性,使其具有指定的 value。

[解决办法]
路过。
[解决办法]
帮顶,建议最好是仔细看看API
[解决办法]
好长。。。。可能是方法废除了,改成新方法就好啦,查下api

[解决办法]
在你的电脑属性里面,可以找到系统属性设置,更改你的class访问路径就可以了!

读书人网 >J2SE开发

热点推荐