InvalidKeyException 使用 ECPublicKey
InvalidKeyException using ECPublicKey
当我尝试使用 EC public 密钥加密字节数组时出现以下异常:
java.security.InvalidKeyException: No installed provider supports this key:
sun.security.ec.ECPublicKeyImpl
当我调用 Cipher.init() 时会生成此异常。下面几行显示了我在我的程序中做了什么:
ECPublicKey publicKey ;
ECPrivateKey privateKey;
//Generating key paire (public and private keys)
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", "SunEC");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(571, random);
KeyPair pair = keyGen.generateKeyPair();
privateKey = (ECPrivateKey) pair.getPrivate();
publicKey = (ECPublicKey) pair.getPublic();
// get an AES cipher object with CTR encription mode
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
// encrypt the sharedSecret using the public key
cipher.init(Cipher.ENCRYPT_MODE, publicKey);**
byte[] result = cipher.doFinal(data);
我必须添加提供商才能支持此 public 密钥吗??
终于找到了这个异常的根源。问题是密码的初始化:
//This is the wrong initialization
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
//This is the right initialization
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding","SunJCE");
但是现在,我有另一个例外(它不如前一个重要):
java.security.InvalidKeyException: Invalid AES key length: 170 bytes
那么我现在必须使用什么作为 ECDSA public 密钥的加密算法?
当我尝试使用 EC public 密钥加密字节数组时出现以下异常:
java.security.InvalidKeyException: No installed provider supports this key:
sun.security.ec.ECPublicKeyImpl
当我调用 Cipher.init() 时会生成此异常。下面几行显示了我在我的程序中做了什么:
ECPublicKey publicKey ;
ECPrivateKey privateKey;
//Generating key paire (public and private keys)
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", "SunEC");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(571, random);
KeyPair pair = keyGen.generateKeyPair();
privateKey = (ECPrivateKey) pair.getPrivate();
publicKey = (ECPublicKey) pair.getPublic();
// get an AES cipher object with CTR encription mode
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
// encrypt the sharedSecret using the public key
cipher.init(Cipher.ENCRYPT_MODE, publicKey);**
byte[] result = cipher.doFinal(data);
我必须添加提供商才能支持此 public 密钥吗??
终于找到了这个异常的根源。问题是密码的初始化:
//This is the wrong initialization
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
//This is the right initialization
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding","SunJCE");
但是现在,我有另一个例外(它不如前一个重要):
java.security.InvalidKeyException: Invalid AES key length: 170 bytes
那么我现在必须使用什么作为 ECDSA public 密钥的加密算法?