这是一个比较老的实现ELGamal签名算法的代码,运行时提示使用或覆盖了已过时的API,请高手帮我看看是怎么回事,谢谢各位了!!
这是一个比较老的实现ELGamal签名算法的代码,运行时提示使用或覆盖了已过时的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