96 lines
4.2 KiB
Java
96 lines
4.2 KiB
Java
package com.ruoyi.system.demo;
|
|
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.KeyGenerator;
|
|
import javax.crypto.SecretKey;
|
|
import javax.crypto.spec.GCMParameterSpec;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.security.KeyPair;
|
|
import java.security.KeyPairGenerator;
|
|
import java.security.Signature;
|
|
import java.security.SecureRandom;
|
|
import java.security.Security;
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
import java.util.Base64;
|
|
|
|
/**
|
|
* Ed25519 使用示例。
|
|
*
|
|
* Ed25519 只能签名和验签,不能直接加密/解密;这里用 AES-256-GCM 演示加密解密。
|
|
* 真正的公钥加密应使用 X25519 协商共享密钥,再使用 AES-GCM 加密业务数据。
|
|
* 本示例显式使用 Bouncy Castle provider,因此可兼容 JDK 8。
|
|
*/
|
|
public final class Ed25519CryptoDemo {
|
|
private static final SecureRandom RANDOM = new SecureRandom();
|
|
|
|
private Ed25519CryptoDemo() { }
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
// JDK 8 does not provide Ed25519; register Bouncy Castle explicitly.
|
|
if (Security.getProvider("BC") == null) Security.addProvider(new BouncyCastleProvider());
|
|
String message = "TRON payment payload";
|
|
|
|
KeyPairGenerator edGenerator = KeyPairGenerator.getInstance("Ed25519", "BC");
|
|
KeyPair signingKeys = edGenerator.generateKeyPair();
|
|
byte[] signature = sign(message.getBytes(StandardCharsets.UTF_8), signingKeys);
|
|
boolean verified = verify(message.getBytes(StandardCharsets.UTF_8), signature, signingKeys);
|
|
|
|
System.out.println("Ed25519 public key: " + Base64.getEncoder().encodeToString(signingKeys.getPublic().getEncoded()));
|
|
System.out.println("Ed25519 private key: " + Base64.getEncoder().encodeToString(signingKeys.getPrivate().getEncoded()));
|
|
System.out.println("Signature: " + Base64.getEncoder().encodeToString(signature));
|
|
System.out.println("Signature valid: " + verified);
|
|
|
|
AesEnvelope encrypted = encrypt(message.getBytes(StandardCharsets.UTF_8));
|
|
byte[] decrypted = decrypt(encrypted);
|
|
System.out.println("AES-GCM ciphertext: " + Base64.getEncoder().encodeToString(encrypted.ciphertext));
|
|
System.out.println("AES-GCM plaintext: " + new String(decrypted, StandardCharsets.UTF_8));
|
|
}
|
|
|
|
public static byte[] sign(byte[] message, KeyPair keyPair) throws Exception {
|
|
Signature signer = Signature.getInstance("Ed25519", "BC");
|
|
signer.initSign(keyPair.getPrivate());
|
|
signer.update(message);
|
|
return signer.sign();
|
|
}
|
|
|
|
public static boolean verify(byte[] message, byte[] signature, KeyPair keyPair) throws Exception {
|
|
Signature verifier = Signature.getInstance("Ed25519", "BC");
|
|
verifier.initVerify(keyPair.getPublic());
|
|
verifier.update(message);
|
|
return verifier.verify(signature);
|
|
}
|
|
|
|
public static AesEnvelope encrypt(byte[] plaintext) throws Exception {
|
|
KeyGenerator generator = KeyGenerator.getInstance("AES");
|
|
generator.init(256);
|
|
SecretKey key = generator.generateKey();
|
|
byte[] nonce = new byte[12];
|
|
RANDOM.nextBytes(nonce);
|
|
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
|
|
cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(128, nonce));
|
|
return new AesEnvelope(key, nonce, cipher.doFinal(plaintext));
|
|
}
|
|
|
|
public static byte[] decrypt(AesEnvelope envelope) throws Exception {
|
|
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
|
|
cipher.init(Cipher.DECRYPT_MODE, envelope.key, new GCMParameterSpec(128, envelope.nonce));
|
|
return cipher.doFinal(envelope.ciphertext);
|
|
}
|
|
|
|
public static final class AesEnvelope {
|
|
private final SecretKey key;
|
|
private final byte[] nonce;
|
|
private final byte[] ciphertext;
|
|
|
|
private AesEnvelope(SecretKey key, byte[] nonce, byte[] ciphertext) {
|
|
this.key = key;
|
|
this.nonce = nonce;
|
|
this.ciphertext = ciphertext;
|
|
}
|
|
|
|
public byte[] getEncodedKey() { return key.getEncoded().clone(); }
|
|
public byte[] getNonce() { return nonce.clone(); }
|
|
public byte[] getCiphertext() { return ciphertext.clone(); }
|
|
}
|
|
}
|