我使用以下代码将密钥转换为字节
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
byte[] bkey=key.getEncoded();
Run Code Online (Sandbox Code Playgroud)
现在如何从中获取密钥bkey
?我试过了:
SecretKeySpec secretkey = new SecretKeySpec(bkey,"DES");
SecretKeyFactory sfkey = SecretKeyFactory.getInstance("DES");
SecretKey skey = sfkey.generateSecret(secretkey);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Error during Exception java.security.spec.InvalidKeySpecException: Inappropriate key specification
Run Code Online (Sandbox Code Playgroud) 由于使用RSAPublicKeyImpl,我收到一些警告:
警告:RSAPublicKeyImpl是内部专有API,可能会在以后的发行版中删除。import sun.security.rsa.RSAPublicKeyImpl;
我试图找到替代产品,但是没有运气。此类的开源替代品是什么?
这是我的KeyPair生成方法:
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
ECGenParameterSpec ecSpec = new ECGenParameterSpec("prime192v1");
// Initialize the key generator and generate a KeyPair
keyGen.initialize(ecSpec, random); // 256 bytes provides an
// acceptable security level
KeyPair keyPair = keyGen.generateKeyPair();
// Set the public and private keys from the keyPair
privateKey = keyPair.getPrivate();
publicKey = keyPair.getPublic();
System.out.println("Private and public keys:");
System.out.println("PRIVATE: " + StringUtil.getStringFromKey(this.privateKey));
System.out.println("PUBLIC: " + StringUtil.getStringFromKey(this.publicKey));
} catch (Exception e) {
throw new RuntimeException(e); …
Run Code Online (Sandbox Code Playgroud) java ×3
byte ×1
cryptography ×1
des ×1
encoding ×1
generate ×1
private-key ×1
public-key ×1
recover ×1
rsa ×1
security ×1