C#和Java DES加密值不相同 [英] C# and Java DES Encryption value are not identical

查看:225
本文介绍了C#和Java DES加密值不相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加密C#和Java中的相同数据。如果数据超过7个字节,则Java和C#加密值不相同。




  • 输入1: a

    java输出: FrOzOp / 2Io8 =

    C#输出: FrOzOp / 2Io8 =


  • 输入2: strong> abc

    j: H9A / ahl8K7I =

    c#: H9A / ahl8K7I =


  • 输入3: aaaaaaaa 问题

    j: Gxl7e0aWPd7j6l7uIEuMxA ==

    c#: Gxl7e0aWPd7sf1xR6hK4VQ ==




是C#和Java方法的实现。

C#代码:

  public String saltTxt =12345678 ; 
public String Encrypt(String txt)
{
byte [] data = Encrypt(Encoding.UTF8.GetBytes(txt));

DESCryptoServiceProvider alg = new DESCryptoServiceProvider();

alg.Key = Encoding.UTF8.GetBytes(saltTxt.ToCharArray(),0,cprovider.KeySize / 8);
alg.IV = new byte [8];

MemoryStream ms = new MemoryStream();
CryptoStream stem = new CryptoStream(ms,cprovider.CreateEncryptor(),CryptoStreamMode.Write);

stem.Write(txt,0,txt.Length);
stem.FlushFinalBlock();

data = ms.ToArray();

return Convert.ToBase64String(data);
}

Java代码:

  public String saltTxt =12345678; 
public String Encrypt(String str){
try {
KeySpec myKey = new DESKeySpec(saltTxt.getBytes(UTF8));
SecretKey key = SecretKeyFactory.getInstance(DES)。generateSecret(myKey);
密码ecipher = Cipher.getInstance(DES);
ecipher.init(Cipher.ENCRYPT_MODE,key);

byte [] data = str.getBytes(UTF8);

byte [] crypt = ecipher.doFinal(data);

返回新的BASE64Encoder()。encode(crypt);
} catch(Exception ex){
}

返回null;
}

任何想法为什么它不按预期工作?

解决方案

问题出在模式中。


SunJCE提供商使用 ECB 作为默认模式,和 PKCS5Padding 作为DES,DES-EDE和Blowfish密码的默认填充方案。 ( JCA Doc



.Net ,对称算法的默认操作模式是 CipherMode.CBC ,默认填充为 PaddingMode.PKCS7 即可。 ( msdn..SymmetricAlgorithm


以下更改解决了问题。

  //在C#
DESCryptoServiceProvider alg = new DESCryptoServiceProvider();
alg.Mode = CipherMode.ECB; //指定

  // in java 
chiper = Cipher.getInstance(DES / CBC / PKCS5Padding);

不要在两边更改。


I am trying to encrypt same data in C# and Java. If data is more than 7 bytes then Java and C# encrypted value are not identical.

  • Input 1: a
    java output: FrOzOp/2Io8=
    C# output: FrOzOp/2Io8=

  • Input 2: abc
    j : H9A/ahl8K7I=
    c#: H9A/ahl8K7I=

  • Input 3: aaaaaaaa (Problem)
    j : Gxl7e0aWPd7j6l7uIEuMxA==
    c#: Gxl7e0aWPd7sf1xR6hK4VQ==

Here is the implementation of C# and Java methods.
C# code:

public String saltTxt = "12345678";
public String Encrypt(String txt)
{
        byte[] data = Encrypt(Encoding.UTF8.GetBytes(txt));

        DESCryptoServiceProvider alg = new DESCryptoServiceProvider();

        alg.Key = Encoding.UTF8.GetBytes(saltTxt.ToCharArray(), 0, cprovider.KeySize / 8);
        alg.IV = new byte[8];

        MemoryStream ms = new MemoryStream();
        CryptoStream stem = new CryptoStream( ms, cprovider.CreateEncryptor(),CryptoStreamMode.Write);

        stem.Write(txt, 0, txt.Length);
        stem.FlushFinalBlock();

        data = ms.ToArray();

        return Convert.ToBase64String(data);
 }

Java Code:

public String saltTxt = "12345678";
public String Encrypt(String str) {
    try {
        KeySpec myKey = new DESKeySpec(saltTxt.getBytes("UTF8"));
        SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(myKey);
        Cipher ecipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] data = str.getBytes("UTF8");

        byte[] crypt = ecipher.doFinal(data);

        return new BASE64Encoder().encode(crypt);
    } catch (Exception ex) {
    }

    return null;
} 

Any idea why it's not working as expected?

解决方案

the problem was in mode of encryption.

SunJCE provider uses ECB as the default mode, and PKCS5Padding as the default padding scheme for DES, DES-EDE and Blowfish ciphers. (JCA Doc)

and

In .Net, The default operation mode for the symmetric algorithm is CipherMode.CBC and default padding is PaddingMode.PKCS7. (msdn..SymmetricAlgorithm)

following changes resolve the problem.

// in C# 
DESCryptoServiceProvider alg = new DESCryptoServiceProvider();
alg.Mode = CipherMode.ECB;  // specified 

or

// in java
chiper = Cipher.getInstance("DES/CBC/PKCS5Padding");

don't change in both side.

这篇关于C#和Java DES加密值不相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆