Я пытаюсь использовать Java для шифрования/дешифрования текста с помощью «secp256r1»
Но, к сожалению, я не всегда могу выдать ошибку «Невозможно обработать предоставленную спецификацию параметра: необходимо передать параметры IES»
Вот полный пример кода
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import java.security.Security;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.spec.ECGenParameterSpec;
import java.util.Base64;
public class Generator {
public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
// Create KeyPairGenerator for ECC
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
keyPairGenerator.initialize(ecSpec);
// Generate Key Pair
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
// Output the keys
System.out.println("Private Key: " + privateKey);
System.out.println("Public Key: " + publicKey);
// Message to encrypt
String originalMessage = "This is a secret message!";
// Convert to bytes
byte[] messageBytes = originalMessage.getBytes();
// Encrypt the message
Cipher cipher = Cipher.getInstance("ECIES", "BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedMessage = cipher.doFinal(messageBytes);
System.out.println("Encrypted Message: " + Base64.getEncoder().encodeToString(encryptedMessage));
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedMessage);
// Decrypt the message
Cipher cipher2 = Cipher.getInstance("ECIES", "BC");
cipher2.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedMessage = cipher2.doFinal(encryptedBytes);
System.out.println("Decrypted Message: " + new String(decryptedMessage));
}
}
Я пытался удалить BC, но безрезультатно
полное исключение:
Exception in thread "main" java.lang.IllegalArgumentException: cannot handle supplied parameter spec: must be passed IES parameters
at org.bouncycastle.jcajce.provider.asymmetric.ec.IESCipher.engineInit(Unknown Source)
at java.base/javax.crypto.Cipher.init(Cipher.java:1296)
at java.base/javax.crypto.Cipher.init(Cipher.java:1236)
at com.example.Main.main(Main.java:42)
Подробнее здесь: https://stackoverflow.com/questions/790 ... eters-java
Невозможно обработать предоставленную спецификацию параметра: необходимо передать параметры IES Java ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Необходимо создать спецификацию, которая будет соответствовать данным из Postgres varchar[]
Anonymous » » в форуме JAVA - 0 Ответы
- 8 Просмотры
-
Последнее сообщение Anonymous
-