0

我正在尝试通过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:没有安装的提供程序支持此密钥:(空)

4

1 回答 1

1

您正在将密钥转换为 base-64,但不会再次将其转换回来。

于 2013-11-12T09:07:33.603 回答