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)
请大家帮忙看一下!这是什么原应的错误
------解决方案--------------------
DES是非对称加密,加密和解密使用的密钥\初始化向量都要相同,否则当然解不了密
------解决方案--------------------
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();