上面的代码打印一下加密结果和解密结果
时间: 2024-04-09 08:30:18 浏览: 10
好的,我会修改代码,让它打印出加密结果和解密结果。
以下是修改后的代码:
```java
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECGenParameterSpec;
public class ECCExample {
public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
// 生成密钥对
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("secp256k1"); // 指定椭圆曲线参数
keyPairGenerator.initialize(ecGenParameterSpec);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 获取公钥和私钥
ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
// 加密
String plaintext = "Hello, ECC!";
Cipher encryptCipher = Cipher.getInstance("ECIES", "BC");
encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] ciphertext = encryptCipher.doFinal(plaintext.getBytes());
// 解密
Cipher decryptCipher = Cipher.getInstance("ECIES", "BC");
decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedText = decryptCipher.doFinal(ciphertext);
System.out.println("Plaintext: " + plaintext);
System.out.println("Ciphertext: " + new String(ciphertext));
System.out.println("Decrypted Text: " + new String(decryptedText));
}
}
```
在修改后的代码中,我添加了两行新的打印语句,分别输出加密结果和解密结果。你可以运行这段代码,看到加密结果和解密结果的输出。
希望这次能满足你的要求!如果还有其他问题,请随时提问。