我正在尝试通过DES制作加密和解密程序。在这段代码中,我正在创建一个secretKey
Cipher desCipher;
KeyGenerator keygenerator;
SecretKey myDesKey;
try{
keygenerator = KeyGenerator.getInstance("DES");
myDesKey = keygenerator.generateKey();
byte[] encoded = myDesKey.getEncoded();
// convert secret key to string
String stringKey =Base64.encodeToString(myDesKey.getEncoded());
// converting back from string to secret key.
//its returning javax.crypto.spec.SecretKey object but i need com.sun.crypto.provider.DESKey object
SecretKey originalKey = new SecretKeySpec(stringKey.getBytes(), 0, stringKey.length(), "DES");
String text="hello how are you "
desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte[] textEncrypted = desCipher.doFinal(text.getBytes());
System.out.println("text encrypted successfully");
}
catch(Exception ex)
{
ex.printStackTrace();
}
is throwing the exception
java.security.InvalidKeyException:没有安装的提供程序支持此密钥:(null)
答案 0 :(得分:1)
您正在将密钥转换为base-64,但不会再将其转换回来。