java和.net中的DES加密结果不同

时间:2015-12-10 02:18:56

标签: java .net

伙计们,这是我第一次触摸DES加密,我希望我的.net代码和java代码可以通过使用我自己的密钥创建相同的加密结果。我尝试了很多代码,但仍然混淆,任何人都可以帮助我?是加密模式问题?

这是我的.net代码

        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes("12345678");
        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes("12345678");


        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
   //     int i = cryptoProvider.KeySize;
        MemoryStream ms = new MemoryStream();
        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
        StreamWriter sw = new StreamWriter(cst);
        sw.Write(data);
        sw.Flush();
        cst.FlushFinalBlock();
        sw.Flush();
        return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);

这是我的java代码

`

String  epsKey ="12345678";
    String  str ="hcs";
DESKeySpec desKeySpec = new DESKeySpec(epsKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(desKeySpec);
Cipher ecipher = Cipher.getInstance("DES");
//ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
ecipher.init(Cipher.ENCRYPT_MODE, key);
byte[] enc = ecipher.doFinal(str.getBytes("UTF8"));
String encryptedString = new BASE64Encoder().encode(enc);

System.out.println(encryptedString);

`

0 个答案:

没有答案