java.security.InvalidKeyException: Parameters missing
at com.sun.crypto.provider.SunJCE_h.a(DashoA12275)
at com.sun.crypto.provider.DESCipher.engineInit(DashoA12275)
at javax.crypto.Cipher.a(DashoA12275)
at javax.crypto.Cipher.a(DashoA12275)
at javax.crypto.Cipher.init(DashoA12275)
at javax.crypto.Cipher.init(DashoA12275)
at com.commom.encrypt.impl.DESMessageEncryptor.decryptMessage(DESMessageEncryptor.java:48)
at com.commom.encrypt.impl.DESMessageEncryptor.main(DESMessageEncryptor.java:96)请大家帮忙看一下!这是什么原应的错误

解决方案 »

  1.   

    import java.security.SecureRandom;
    import java.security.Security;import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;import com.commom.constants.Constants;
    import com.commom.encrypt.IMessageEncryptor;public class DESMessageEncryptor implements IMessageEncryptor { public String decryptMessage(String encryptedText) {
    // TODO Auto-generated method stub
    String passwordString = encryptedText.substring(0, 12);
    String cipherString = encryptedText.substring(12, encryptedText
    .length());
    BASE64Decoder decoder = new BASE64Decoder();
    try {
    byte[] password = decoder.decodeBuffer(passwordString);
    System.out.println(new BASE64Encoder().encode(password));
    byte[] cipherText = decoder.decodeBuffer(cipherString);
    System.out.println(new BASE64Encoder().encode(cipherText));
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
    DESKeySpec keySpec = new DESKeySpec(password);
    SecretKeyFactory keyFactory = SecretKeyFactory
    .getInstance(Constants.DES_ALGORITHM);
    SecretKey key = keyFactory.generateSecret(keySpec);
    Cipher cipher = Cipher
    .getInstance(Constants.DES_OFB32_PKCS5PADDING);
    cipher.init(Cipher.DECRYPT_MODE, key);
    return new String(cipher.doFinal(cipherText));
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    } public String encryptMessage(String plainText) {
    // TODO Auto-generated method stub
    try {
    byte[] password = new byte[8];
    SecureRandom random = new SecureRandom();
    random.nextBytes(password);
    System.out.println(new BASE64Encoder().encode(password));
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
    DESKeySpec keySpec = new DESKeySpec(password);
    SecretKeyFactory keyFactory = SecretKeyFactory
    .getInstance(Constants.DES_ALGORITHM);
    SecretKey key = keyFactory.generateSecret(keySpec);
    Cipher cipher = Cipher
    .getInstance(Constants.DES_OFB32_PKCS5PADDING);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] cipherText = cipher.doFinal(plainText.getBytes());
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(password).concat(encoder.encode(cipherText));
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    } public static void main(String[] args) {
    DESMessageEncryptor test1 = new DESMessageEncryptor();
    String cipher = test1.encryptMessage("programmer");
    System.out.println(cipher);
    System.out.println(test1.decryptMessage(cipher));
    }
    }
      

  2.   

    at com.sun.crypto.provider.SunJCE_h.a(DashoA12275)
    at com.sun.crypto.provider.DESCipher.engineInit(DashoA12275)
    at javax.crypto.Cipher.a(DashoA12275)
    at javax.crypto.Cipher.a(DashoA12275)
    at javax.crypto.Cipher.init(DashoA12275)
    at javax.crypto.Cipher.init(DashoA12275)
    --------------------------------------------------------------------------------
    自己看看这些包里的函数参数个数和类型跟你调用的对不对。
      

  3.   

    DES是非对称加密,加密和解密使用的密钥\初始化向量都要相同,否则当然解不了密
      

  4.   

    非要设置它的Initializatin Vector(IV)吗?
      

  5.   

    我没有设置Initializatin Vector(IV)为什么可以加密阿!
      

  6.   

    http://www.cnjbb.org/thread.jsp?boardid=3&threadid=42335如果使用函数,要把密钥保存下来.或者是生成密钥,供加密和解密时使用
      

  7.   

    http://www.cnjbb.org/thread.jsp?boardid=3&threadid=42335
    这里面和我这没区别阿,我这里只是哪了个密码生成密钥!
    它那里就是直接拿个密钥进行加密解密,一样的阿! 我这为什么什么会出
    parameters  missing
      

  8.   

    package com.uitv.mboss.payment.test;import java.security.SecureRandom;
    import java.security.Security;import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;public class DESMessageEncryptor { public String decryptMessage(String encryptedText) {
    // TODO Auto-generated method stub
    String passwordString = encryptedText.substring(0, 12);
    String cipherString = encryptedText.substring(12, encryptedText
    .length());
    BASE64Decoder decoder = new BASE64Decoder();
    try {
    byte[] password = decoder.decodeBuffer(passwordString);
    System.out.println(new BASE64Encoder().encode(password));
    byte[] cipherText = decoder.decodeBuffer(cipherString);
    System.out.println(new BASE64Encoder().encode(cipherText));
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
    DESKeySpec keySpec = new DESKeySpec(password);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey key = keyFactory.generateSecret(keySpec);
    byte[] iv = new sun.misc.BASE64Decoder().decodeBuffer("t4JPbY+rXgk="); javax.crypto.spec.IvParameterSpec ips = new javax.crypto.spec.IvParameterSpec(
    iv);
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key,ips);
    System.out.println("IV1:");
    System.out.println(new BASE64Encoder().encode(cipher.getIV()));
    return new String(cipher.doFinal(cipherText));
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    } public String encryptMessage(String plainText) {
    // TODO Auto-generated method stub
    try {
    byte[] password = new byte[8];
    SecureRandom random = new SecureRandom();
    random.nextBytes(password);
    System.out.println(new BASE64Encoder().encode(password));
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
    DESKeySpec keySpec = new DESKeySpec(password);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey key = keyFactory.generateSecret(keySpec);
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    byte[] iv = new sun.misc.BASE64Decoder().decodeBuffer("t4JPbY+rXgk="); javax.crypto.spec.IvParameterSpec ips = new javax.crypto.spec.IvParameterSpec(
    iv);
    cipher.init(Cipher.ENCRYPT_MODE, key,ips);
    System.out.println("IV0:");
    System.out.println(new BASE64Encoder().encode(cipher.getIV()));
    byte[] cipherText = cipher.doFinal(plainText.getBytes());
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(password).concat(encoder.encode(cipherText));
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    } public static void main(String[] args) {
    DESMessageEncryptor test1 = new DESMessageEncryptor();
    String cipher = test1.encryptMessage("programmer");
    System.out.println(cipher);
    System.out.println(test1.decryptMessage(cipher));
    }
    }
      

  9.   

    输出
    fu8cCytO/PM=
    IV0:
    t4JPbY+rXgk=
    fu8cCytO/PM=BKagejANzX2bpOL2WQAaqw==
    fu8cCytO/PM=
    BKagejANzX2bpOL2WQAaqw==
    IV1:
    t4JPbY+rXgk=
    programmer