java sm2 代码
时间: 2023-11-25 17:06:58 浏览: 134
以下是一个简单的Java代码示例,用于生成SM2公私钥对并进行加密解密操作:
```java
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.generators.AsymmetricCipherKeyPairGenerator;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.crypto.signers.SM2Signer;
import org.bouncycastle.math.ec.ECPoint;
import java.math.BigInteger;
import java.security.SecureRandom;
public class SM2Example {
public static void main(String[] args) throws InvalidCipherTextException {
// 生成SM2密钥对
AsymmetricCipherKeyPair keyPair = generateSM2KeyPair();
// 获取公私钥参数
ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters) keyPair.getPrivate();
ECPublicKeyParameters publicKey = (ECPublicKeyParameters) keyPair.getPublic();
// 要加密的明文
byte[] plaintext = "Hello, SM2!".getBytes();
// 使用公钥进行加密
byte[] ciphertext = encryptWithSM2(publicKey, plaintext);
System.out.println("Ciphertext: " + new String(ciphertext));
// 使用私钥进行解密
byte[] decryptedText = decryptWithSM2(privateKey, ciphertext);
System.out.println("Decrypted Text: " + new String(decryptedText));
// 签名和验签示例
byte[] message = "Message to be signed".getBytes();
byte[] signature = signWithSM2(privateKey, message);
boolean isVerified = verifyWithSM2(publicKey, message, signature);
System.out.println("Signature Verification: " + isVerified);
}
// 生成SM2公私钥对
public static AsymmetricCipherKeyPair generateSM2KeyPair() {
AsymmetricCipherKeyPairGenerator generator = new ECKeyPairGenerator();
generator.init(new ECKeyGenerationParameters(SM2Util.getDomainParameters(), new SecureRandom()));
return generator.generateKeyPair();
}
// 使用公钥进行加密
public static byte[] encryptWithSM2(ECPublicKeyParameters publicKey, byte[] plaintext) throws InvalidCipherTextException {
SM2Engine engine = new SM2Engine();
engine.init(true, new ParametersWithRandom(publicKey, new SecureRandom()));
return engine.processBlock(plaintext, 0, plaintext.length);
}
// 使用私钥进行解密
public static byte[] decryptWithSM2(ECPrivateKeyParameters privateKey, byte[] ciphertext) throws InvalidCipherTextException {
SM2Engine engine = new SM2Engine();
engine.init(false, privateKey);
return engine.processBlock(ciphertext, 0, ciphertext.length);
}
// 使用私钥进行签名
public static byte[] signWithSM2(ECPrivateKeyParameters privateKey, byte[] message) {
SM2Signer signer = new SM2Signer();
signer.init(true, privateKey);
signer.update(message, 0, message.length);
return signer.generateSignature();
}
// 使用公钥进行验签
public static boolean verifyWithSM2(ECPublicKeyParameters publicKey, byte[] message, byte[] signature) {
SM2Signer verifier = new SM2Signer();
verifier.init(false, publicKey);
verifier.update(message, 0, message.length);
return verifier.verifySignature(signature);
}
}
```
在上面的示例中,我们使用了Bouncy Castle密码库(Bouncy Castle Crypto Library)来实现SM2加密算法。你需要确保已经将Bouncy Castle库添加到项目的依赖中。
此示例演示了如何生成SM2公私钥对、使用公钥进行加密、使用私钥进行解密,以及使用私钥进行签名和使用公钥进行验签。请注意,示例中的加密和解密操作是基于SM2原生的非对称加密算法,而签名和验签操作是基于SM2的数字签名算法。
你可以根据自己的需求对代码进行进一步的扩展和优化。同时,建议在实际使用时,对密钥进行适当的存储和保护,确保安全性。
阅读全文