我有一个RSA BigInteger类,通过这个类,您可以生成密钥,加密,解密,签名和验证数据。
class RSA {
public static RSAKeyPair generateKeyPair(int size) {
Random rnd = new SecureRandom();
BigInteger p = new BigInteger(size / 2, 100, rnd);
BigInteger q = new BigInteger(size / 2, 100, rnd);
BigInteger n = p.multiply(q);
BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
BigInteger e;
do {
e = new BigInteger(phi.bitLength(), rnd);
} while (e.compareTo(BigInteger.ONE) <= 0 || e.compareTo(phi) >= 0 || !e.gcd(phi).equals(BigInteger.ONE));
BigInteger d = e.modInverse(phi);
return new RSAKeyPair(new RSAPublicKey(n, e), new RSAPrivateKey(n, d), n);
}
public static BigInteger encrypt(BigInteger m, RSAPublicKey key) {
return m.modPow(key.getPublicExponent(), key.getModulus());
}
public static BigInteger decrypt(BigInteger c, RSAPrivateKey key) {
return c.modPow(key.getPrivateExponent(), key.getModulus());
}
public static BigInteger sign(BigInteger m, RSAPrivateKey key) {
return m.modPow(key.getPrivateExponent(), key.getModulus());
}
public static boolean verify(BigInteger m, BigInteger s, RSAPublicKey key) {
return s.modPow(key.getPublicExponent(), key.getModulus()).equals(m);
}
}
class RSAKeyPair {
private RSAPublicKey pub;
private RSAPrivateKey priv;
private BigInteger n;
public RSAKeyPair(RSAPublicKey pub, RSAPrivateKey priv, BigInteger n) {
this.pub = pub;
this.priv = priv;
this.n = n;
}
public RSAPublicKey getPublicKey() {
return pub;
}
public RSAPrivateKey getPrivateKey() {
return priv;
}
public BigInteger getModulus() {
return n;
}
}
class RSAPublicKey {
private BigInteger n;
private BigInteger e;
public RSAPublicKey(BigInteger n, BigInteger e) {
this.n = n;
this.e = e;
}
public BigInteger getModulus() {
return n;
}
public BigInteger getPublicExponent() {
return e;
}
}
class RSAPrivateKey {
private BigInteger n;
private BigInteger d;
public RSAPrivateKey(BigInteger n, BigInteger d) {
this.n = n;
this.d = d;
}
public BigInteger getModulus() {
return n;
}
public BigInteger getPrivateExponent() {
return d;
}
}
这个课有效,但我有一个问题: 当我使用java api对字符串进行签名并使用RSA类对其进行验证时,返回false。
代码:
byte[] data = "poop".getBytes();
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(1024);
KeyPair pair = gen.generateKeyPair();
Signature sig = Signature.getInstance("NONEwithRSA");
sig.initSign(pair.getPrivate());
sig.update(data);
byte[] sign = sig.sign();
java.security.interfaces.RSAPublicKey pub = (java.security.interfaces.RSAPublicKey) pair.getPublic();
BigInteger m = new BigInteger(data);
BigInteger c = new BigInteger(sign);
RSAPublicKey key = new RSAPublicKey(pub.getModulus(), pub.getPublicExponent()); // not the java.security.interfaces.RSAPublicKey rather the RSAPublicKey from my class see above
boolean verify = RSA.verify(m, c, key);
System.out.println(verify);
为什么它返回false,它的字节和密钥对完全相同。