问题描述
java加密,已经写好的不能修改。publicstaticStringencrypt(StringstrIn,Stringkey)throwsException{returnbyteArr2HexStr(encrypt(strIn.getBytes(),key));}publicstaticStringbyteArr2HexStr(byte[]arrB)throwsException{intiLen=arrB.length;//每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍StringBuffersb=newStringBuffer(iLen*2);for(inti=0;i<iLen;i++){intintTmp=arrB[i];//把负数转换为正数while(intTmp<0){intTmp=intTmp+256;}//小于0F的数需要在前面补0if(intTmp<16){sb.append("0");}sb.append(Integer.toString(intTmp,16));}returnsb.toString();}publicstaticbyte[]encrypt(byte[]arrB,Stringkey)throwsException{SecretKeydeskey=newjavax.crypto.spec.SecretKeySpec(key.getBytes(),"DES");CipherencryptCipher=Cipher.getInstance("DES");encryptCipher.init(Cipher.ENCRYPT_MODE,deskey);returnencryptCipher.doFinal(arrB);}
C#代码publicstaticstringEncrypt(stringpToEncrypt,stringsKey){DESCryptoServiceProviderprovider=newDESCryptoServiceProvider();provider.Mode=CipherMode.ECB;byte[]bytes=Encoding.UTF8.GetBytes(pToEncrypt);provider.Key=Encoding.ASCII.GetBytes(sKey);provider.IV=Encoding.ASCII.GetBytes(sKey);MemoryStreamstream=newMemoryStream();CryptoStreamstream2=newCryptoStream(stream,provider.CreateEncryptor(),CryptoStreamMode.Write);stream2.Write(bytes,0,bytes.Length);stream2.FlushFinalBlock();StringBuilderbuilder=newStringBuilder();foreach(bytenuminstream.ToArray()){builder.AppendFormat("{0:X2}",num);}returnbuilder.ToString();}C#代码是我写的,大神帮忙看看,哪里有不对的
解决方案
本帖最后由 liujun198773 于 2015-05-08 14:46:58 编辑