数字签名算法:Rainbow
Java代码
import base64.Base64;
import org.junit.BeforeClass;
import org.spongycastle.crypto.AsymmetricCipherKeyPair;
import org.spongycastle.crypto.params.AsymmetricKeyParameter;
import org.spongycastle.pqc.crypto.rainbow.*;
import java.security.SecureRandom;
public class RainbowTest {
private static AsymmetricKeyParameter privateKey = null;
private static AsymmetricKeyParameter publicKey = null;
@BeforeClass
public static void init() {
RainbowKeyPairGenerator keyPairGenerator = new RainbowKeyPairGenerator();
RainbowKeyGenerationParameters rbKGParams = new RainbowKeyGenerationParameters(new SecureRandom(), new RainbowParameters());
keyPairGenerator.initialize(rbKGParams);
AsymmetricCipherKeyPair keyPair = keyPairGenerator.genKeyPair();
privateKey = keyPair.getPrivate();
RainbowPrivateKeyParameters privKey = (RainbowPrivateKeyParameters) privateKey;
publicKey = keyPair.getPublic();
RainbowPublicKeyParameters pubKey = (RainbowPublicKeyParameters) publicKey;
}
@org.junit.Test
public void test() {
String message = “13120983870”;
RainbowSigner rainbow = new RainbowSigner();
rainbow.init(true, privateKey);
byte[] signature = rainbow.generateSignature(message.getBytes());
System.out.println(“message=” + message + “;signature=base64:” + Base64.byteArrayToBase64(signature));
rainbow.init(false, publicKey);
boolean result = rainbow.verifySignature(message.getBytes(), signature);
System.out.println(result);
result = rainbow.verifySignature(“13120983871”.getBytes(), signature);
System.out.println(result);
}
}
运行结果代码
message=13120983870;signature=base64:SuIF/kR6Wuo6dxe/Uqi3he0ad9G/ezsAim/9OsytQUHV
true
false
Java 标准api方式:
1、java的KeyPairGenerator,PrivateKey,PublicKey接口
2、java的Provider,KeyFactory,Signature接口
生成公钥和私钥:BCRainbowPrivateKey,BCRainbowPublicKey,他们分别实现了java的PrivateKey,PublicKey接口,并且私钥ASN.1编码(规范),编码按照PKCS#8标准,公钥ASN.1编码(规范),编码按照X.509标准。见#getEncoded方法实现。