问题描述
秘钥类:importjavax.crypto.SecretKey;importjavax.crypto.SecretKeyFactory;importjavax.crypto.spec.DESKeySpec;importsun.misc.BASE64Decoder;publicclassKey{privatestaticStringencryptKey="7EV/Zzutjzg=";publicstaticSecretKeyloadKey()throwsException{BASE64Decoderd=newBASE64Decoder();byte[]b=d.decodeBuffer(encryptKey);DESKeySpecdks=newDESKeySpec(b);SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance("DES");returnkeyFactory.generateSecret(dks);}}加密解密类:importjava.io.ByteArrayOutputStream;importjava.io.PrintStream;importjava.util.Properties;importjavax.crypto.Cipher;importjavax.crypto.SecretKey;importorg.springframework.beans.factory.FactoryBean;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;publicclassDBEncryptimplementsFactoryBean{privatePropertiesproperties;publicObjectgetObject()throwsException{returngetProperties();}publicClassgetObjectType(){returnProperties.class;}publicbooleanisSingleton(){returntrue;}publicPropertiesgetProperties(){returnthis.properties;}publicvoidsetProperties(PropertiesinProperties){this.properties=inProperties;StringoriginalUsername=this.properties.getProperty("user");StringoriginalPassword=this.properties.getProperty("password");if(originalUsername!=null){StringnewUsername=deEncryptUsername(originalUsername);this.properties.put("user",newUsername);}if(originalPassword!=null){StringnewPassword=deEncryptPassword(originalPassword);this.properties.put("password",newPassword);}}privateStringdeEncryptUsername(StringoriginalUsername){returndCode(originalUsername.getBytes());}privateStringdeEncryptPassword(StringoriginalPassword){returndCode(originalPassword.getBytes());}publicStringeCode(StringneedEncrypt){byte[]result=(byte[])null;try{CipherenCipher=Cipher.getInstance("DES");SecretKeykey=Key.loadKey();enCipher.init(1,key);result=enCipher.doFinal(needEncrypt.getBytes());BASE64Encoderb=newBASE64Encoder();ByteArrayOutputStreambos=newByteArrayOutputStream();b.encode(result,bos);result=bos.toByteArray();}catch(Exceptione){thrownewIllegalStateException("Systemdoesn'tsupportDESalgorithm.");}returnnewString(result);}publicStringdCode(byte[]result){Strings=null;try{CipherdeCipher=Cipher.getInstance("DES");deCipher.init(2,Key.loadKey());BASE64Decoderd=newBASE64Decoder();result=d.decodeBuffer(newString(result));byte[]strByte=deCipher.doFinal(result);s=newString(strByte);}catch(Exceptione){thrownewIllegalStateException("Systemdoesn'tsupportDESalgorithm.");}returns;}publicstaticvoidmain(String[]args){Strings="test";DBEncryptp=newDBEncrypt();StringafterE=p.eCode(s);System.out.println(afterE);System.out.println(p.dCode(afterE.getBytes()));}}
解决方案
解决方案二:
你自己去查c#的des加解密功能
解决方案三:
base64的加密可能问题不大。des的加密,不知道算法差异有多大了。也看不到java的算法。
解决方案四:
N多转换
解决方案五:
C#的c#的des秘钥只能是八位的,但是这个java的是12位的
解决方案六:
引用4楼xiaosa1009348905的回复:
C#的c#的des秘钥只能是八位的,但是这个java的是12位的
哪个人告诉你c#的des秘钥只能是八位的,微软的DES.Key属性写的清清楚楚“此算法支持长度为64位的密钥。”
解决方案七:
用http://www.tangiblesoftwaresolutions.com/里的工具转换出来不行啊!
解决方案八:
引用2楼mjp1234airen4385的回复:
base64的加密可能问题不大。des的加密,不知道算法差异有多大了。也看不到java的算法。
哪里看不到java的算法?有的啊,publicStringeCode(StringneedEncrypt){byte[]result=(byte[])null;try{CipherenCipher=Cipher.getInstance("DES");SecretKeykey=Key.loadKey();enCipher.init(1,key);result=enCipher.doFinal(needEncrypt.getBytes());BASE64Encoderb=newBASE64Encoder();ByteArrayOutputStreambos=newByteArrayOutputStream();b.encode(result,bos);result=bos.toByteArray();}catch(Exceptione){thrownewIllegalStateException("Systemdoesn'tsupportDESalgorithm.");}returnnewString(result);}publicStringdCode(byte[]result){Strings=null;try{CipherdeCipher=Cipher.getInstance("DES");deCipher.init(2,Key.loadKey());BASE64Decoderd=newBASE64Decoder();result=d.decodeBuffer(newString(result));byte[]strByte=deCipher.doFinal(result);s=newString(strByte);}catch(Exceptione){thrownewIllegalStateException("Systemdoesn'tsupportDESalgorithm.");}returns;}这两个就是加密和解密算法
解决方案九:
有人可以帮忙解决这个问题吗?感谢!
解决方案十:
CipherenCipher=Cipher.getInstance("DES");byte[]strByte=deCipher.doFinal(result);Cipher是java里的des加密算法,在C#里没有。你现在的代码里des加密是依靠这个类库实现的。你有Cipher的源码的话,可以考虑在C#里实现。