问题描述
我在windows上做的加密到linux上解密总是失败,报java.lang.Exception:padblockcorrupted
解决方案
解决方案二:
如果代码不保密的话,请贴出相关DES加密和解密的代码,以及测试用的原文数据、加密密钥,以及你在Windows上用该密钥和原文进行DES加密后的密文。
解决方案三:
引用1楼bao110908的回复:
如果代码不保密的话,请贴出相关DES加密和解密的代码,以及测试用的原文数据、加密密钥,以及你在Windows上用该密钥和原文进行DES加密后的密文。
顶一下
解决方案四:
importjava.security.Key;importjava.security.SecureRandom;importjavax.crypto.Cipher;importjavax.crypto.KeyGenerator;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;publicclassDesEncrypt{privatestaticKeykey;privatefinalstaticStringKEY_STRING="cssc2010cssc2010zcec";privatestaticDesEncryptinstance=null;publicstaticDesEncryptgetInstance(){if(instance==null){instance=newDesEncrypt();}returninstance;}publicDesEncrypt(Stringstr){//StringkeyString="ACDAFAAD";getKey(str);//生成密匙}privateDesEncrypt(){getKey(KEY_STRING);//生成密匙}/***根据参数生成KEY*/publicvoidgetKey(StringstrKey){try{KeyGenerator_generator=KeyGenerator.getInstance("DES");_generator.init(newSecureRandom(strKey.getBytes()));key=_generator.generateKey();_generator=null;}catch(Exceptione){thrownewRuntimeException("ErrorinitializingSqlMapclass.Cause:"+e);}}/***加密String明文输入,String密文输出*/publicStringgetEncString(StringstrMing){byte[]byteMi=null;byte[]byteMing=null;StringstrMi="";BASE64Encoderbase64en=newBASE64Encoder();try{byteMing=strMing.getBytes("UTF8");byteMi=this.getEncCode(byteMing);strMi=base64en.encode(byteMi);}catch(Exceptione){thrownewRuntimeException("ErrorinitializingSqlMapclass.Cause:"+e);}finally{base64en=null;byteMing=null;byteMi=null;}returnstrMi;}/***解密以String密文输入,String明文输出**@paramstrMi*@return*/publicStringgetDesString(StringstrMi){BASE64Decoderbase64De=newBASE64Decoder();byte[]byteMing=null;byte[]byteMi=null;StringstrMing="";try{byteMi=base64De.decodeBuffer(strMi);byteMing=this.getDesCode(byteMi);strMing=newString(byteMing,"UTF8");}catch(Exceptione){thrownewRuntimeException("ErrorinitializingSqlMapclass.Cause:"+e);}finally{base64De=null;byteMing=null;byteMi=null;}returnstrMing;}/***加密以byte[]明文输入,byte[]密文输出**@parambyteS*@return*/publicstaticbyte[]getEncCode(byte[]byteS){byte[]byteFina=null;Ciphercipher;try{cipher=Cipher.getInstance("DES");cipher.init(Cipher.ENCRYPT_MODE,key);byteFina=cipher.doFinal(byteS);}catch(Exceptione){thrownewRuntimeException("ErrorinitializingSqlMapclass.Cause:"+e);}finally{cipher=null;}returnbyteFina;}/***解密以byte[]密文输入,以byte[]明文输出**@parambyteD*@return*/publicstaticbyte[]getDesCode(byte[]byteD){Ciphercipher;byte[]byteFina=null;try{cipher=Cipher.getInstance("DES");cipher.init(Cipher.DECRYPT_MODE,key);byteFina=cipher.doFinal(byteD);}catch(Exceptione){thrownewRuntimeException("ErrorinitializingSqlMapclass.Cause:"+e);}finally{cipher=null;}returnbyteFina;}publicstaticvoidmain(Stringargs[]){DesEncryptdes=DesEncrypt.getInstance();Stringstr1="ASDDasdadadadada你的明aaasda啊大大大大的dadadq使用手册欠妥人ASDADA";//DES加密Stringstr2=des.getEncString(str1);str2="sPtE/3vQPyiRYXfyUIVFPx0T9Ya3Uhzg0bNQ+4f4ZvTeOII6aE7+aVchiC3s67T0C8cklLjIEfxiXE3O3gXrwg==";System.out.println("根据密钥解密后的明文:"+des.getDesString(str2));}}
解决方案五:
你这个方法有问题!/***根据参数生成KEY*/publicvoidgetKey(StringstrKey){try{KeyGenerator_generator=KeyGenerator.getInstance("DES");_generator.init(newSecureRandom(strKey.getBytes()));key=_generator.generateKey();_generator=null;}catch(Exceptione){thrownewRuntimeException("ErrorinitializingSqlMapclass.Cause:"+e);}}
使用KeyGenerator生成的密钥是随机密钥,并不是你传入的密钥,更改一下:publicKeytoKey(byte[]key)throwsException{KeySpecdks=newDESKeySpec(key);SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance("DES");returnkeyFactory.generateSecret(dks);}
DES属于对称加密算法的块加密,密钥应该是8个字节。privatefinalstaticStringKEY_STRING="cssc2010cssc2010zcec";这样的密钥在DES中是不合法的。