Java等同于C#DESCrypto

时间:2016-01-09 11:12:01

标签: java c# cryptography des

我在C#和Java中有DESCrypto,如下所示。使用C#时,我得到了正确的结果。使用Java时遇到问题。如何解决这个问题?

//这是在main函数中,它将调用安全类(C#)中的crypto函数。

private void button1_Click(object sender, EventArgs e)
{    
    string plainText = "0123456789";
    Debug.WriteLine("plainText:" + plainText ); 
    // plainText:0123456789

    byte[] encrypted = Security.Encrypt(Encoding.ASCII.GetBytes(plainText));
    Debug.WriteLine("encrypted:" + Security.GetString(encrypted)); 
    // encrypted:4F792B474936462B6A4F62635A6142464D54782F4E413D3D

    byte[] decrypted = Security.Decrypt(encrypted);
    Debug.WriteLine("decrypted:" + Encoding.ASCII.GetString(decrypted)); //                               
    // decrypted:0123456789
}

//这是一个安全类(C#)

public class Security
{
    private static byte[] IV_64 = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
    private static byte[] KEY_64 = new byte[] { 7, 1, 7, 7, 5, 5, 4, 7 };

    public static byte[] GetBytes(string value)
    {
        SoapHexBinary shb = SoapHexBinary.Parse(value);
        return shb.Value;
    }

    public static string GetString(byte[] value) 
    {
        SoapHexBinary shb = new SoapHexBinary(value);
        return shb.ToString();
    }

    public static byte[] Decrypt(byte[] value)
    {
        MemoryStream mstream = new MemoryStream(Convert.FromBase64String(Encoding.ASCII.GetString(value)));
        CryptoStream cstream = new CryptoStream(mstream, new DESCryptoServiceProvider().CreateDecryptor(KEY_64, IV_64), CryptoStreamMode.Read);
        StreamReader reader = new StreamReader(cstream);
        return Encoding.ASCII.GetBytes(reader.ReadToEnd());
   }

    public static byte[] Encrypt(byte[] value)
    {
        MemoryStream mstream = new MemoryStream();
        CryptoStream cstream = new CryptoStream(mstream, new DESCryptoServiceProvider().CreateEncryptor(KEY_64, IV_64), CryptoStreamMode.Write);
        StreamWriter writer = new StreamWriter(cstream);
        writer.Write(Encoding.UTF8.GetString(value));
        writer.Flush();
        cstream.FlushFinalBlock();
        mstream.Flush();
        return Encoding ASCII.GetBytes(Convert.ToBase64String(mstream.GetBuffer(), 0, Convert.ToInt32(mstream.Length)));
    }
}

//这是在main函数中,它将调用安全类(Java)中的crypto函数。

String plainText = "0123456789";
Log.d("test", String.format("plainText:%s\n", plainText)); 
// plainText:0123456789

byte[] encrypted = Security.Encrypt(plainText.getBytes());
Log.d("test", String.format("encrypted:%s\n", Security.GetString(encrypted)));  
// encrypted:3B2F8623A17E8CE6DC65A045313C7F34

byte[] decrypted = Security.Decrypt(encrypted);
Log.d("test", String.format("decrypted: %s\n", String.valueOf(decrypted)));     
// decrypted:[B@6801b34

//这是一个安全类(JAVA)

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Security {
    private static byte[] IV_64 = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
    private static byte[] KEY_64 = new byte[] { 7, 1, 7, 7, 5, 5, 4, 7 };

    private static String KEY_TYPE = "DES";
    private static String ALGORITHM = "DES/CBC/PKCS5Padding";

    public static String GetString(byte[] value) {
        StringBuilder builder = new StringBuilder();
        for (byte i : value) {
            builder.append(String.format("%02X", i & 0xff));
        }
        return builder.toString();
    }

    public static byte[] GetByte(String value) {
        StringBuilder builder = new StringBuilder();
        for (char i : value.toCharArray()) {
            builder.append(String.format("%02X", i & 0xff));
        }
        return String.valueOf(builder).getBytes();
    }

    public static byte[] Encrypt(byte[] value) {
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY_64, KEY_TYPE), new IvParameterSpec(IV_64));
            return cipher.doFinal(value);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] Decrypt(byte[] value) {
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY_64, KEY_TYPE), new IvParameterSpec(IV_64));
            return cipher.doFinal(value);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }
}

在java中,当plainText = "0123456789"时,加密的预期输出是4F792B474936462B6A4F62635A6142464D54782F4E413D3D(就像在C#中一样),但我得到了3B2F8623A17E8CE6DC65A045313C7F34。

1 个答案:

答案 0 :(得分:2)

在C#中,您使用Security类内的Base64对密文进行编码,然后使用其外部的Hex再次对其进行编码。在Java中,您只进行十六进制编码。你应该坚持使用一种编码而不是两种编码。

其他考虑因素:

  • 现在真的不应该使用DES了。蛮力很容易。
  • 如果您使用CBC模式,则每次都需要使用新的且不可预测的IV(随机生成)。它不必是秘密的,因此您可以将其添加到密文并在解密之前将其切掉。
  • 你真的应该考虑在密文中添加身份验证。否则,可能会在您的系统中运行padding oracle攻击。使用GCM或EAX等经过身份验证的模式,或者使用强大的MAC encrypt-then-MAC方案,如HMAC-SHA256。