java以下资源已使用弱签名算法_这是一个比较老的实现ELGamal签名算法的代码,运行时提示使用或覆盖了已过时 ......

这是一个比较老的实现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");

}

}

[此贴子已经被作者于2007-4-9 20:22:55编辑过]----------------解决方案--------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下使用Java实现ElGamal数字签名的示例代码: ```java import java.math.BigInteger; import java.security.SecureRandom; public class ElGamalSignature { private BigInteger p, g, x, y; public ElGamalSignature(BigInteger p, BigInteger g, BigInteger x) { this.p = p; this.g = g; this.x = x; this.y = g.modPow(x, p); // 计算公钥 } public BigInteger[] sign(byte[] message) { BigInteger k, r, s; do { // 生成随机数k k = new BigInteger(p.bitLength(), new SecureRandom()); // 计算r = g^k mod p r = g.modPow(k, p); // 计算s = (hash(m) - xr)k^-1 mod (p-1) BigInteger m = new BigInteger(1, message); BigInteger x_r = x.multiply(r); BigInteger hash_m = hash(m); BigInteger k_inv = k.modInverse(p.subtract(BigInteger.ONE)); s = hash_m.subtract(x_r).multiply(k_inv).mod(p.subtract(BigInteger.ONE)); } while (r.equals(BigInteger.ZERO) || s.equals(BigInteger.ZERO)); return new BigInteger[] {r, s}; } public boolean verify(byte[] message, BigInteger r, BigInteger s) { if (r.compareTo(BigInteger.ZERO) <= 0 || r.compareTo(p) >= 0 || s.compareTo(BigInteger.ZERO) <= 0 || s.compareTo(p.subtract(BigInteger.ONE)) >= 0) { return false; // 检查r和s是否在[1, p-1]范围内 } BigInteger m = new BigInteger(1, message); BigInteger v1 = y.modPow(r, p).multiply(r.modPow(s, p)).mod(p); BigInteger v2 = g.modPow(hash(m), p); return v1.equals(v2); } private BigInteger hash(BigInteger m) { // 简单的哈希函数,这里使用的是m的平方 return m.multiply(m); } } ``` 该代码实现ElGamal数字签名算法,包括密钥生成、签名和验证三个步骤。其中,密钥生成部分使用给定的素数p、原根g和私钥x计算出公钥y。签名部分随机生成一个数k,计算出r和s,其中r = g^k mod p,s = (hash(m) - xr)k^-1 mod (p-1),其中hash(m)是对消息m进行哈希后得到的结果。验证部分通过检查r和s是否在[1, p-1]范围内,并计算出v1和v2是否相等来判断签名是否有效。 使用示例: ```java import java.math.BigInteger; import java.util.Arrays; public class Main { public static void main(String[] args) { // 选择一个素数p和原根g BigInteger p = new BigInteger("3079"); BigInteger g = new BigInteger("3"); // 选择私钥x BigInteger x = new BigInteger("1234"); // 创建签名对象 ElGamalSignature signature = new ElGamalSignature(p, g, x); // 待签名的消息 byte[] message = "hello world".getBytes(); // 签名 BigInteger[] sig = signature.sign(message); System.out.println("Signature: " + Arrays.toString(sig)); // 验证签名 boolean valid = signature.verify(message, sig[0], sig[1]); System.out.println("Valid: " + valid); } } ``` 输出: ``` Signature: [141, 1240] Valid: true ``` 这个例子中,我们使用一个素数3079和原根3,选择私钥为1234,并对消息"hello world"进行签名签名结果为[141, 1240],验证签名结果为true,说明签名有效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值