C#和Java DES加密值不相同 [英] C# and Java DES Encryption value are not identical
问题描述
-
输入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, andPKCS5Padding
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 isCipherMode.CBC
and default padding isPaddingMode.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屋!