.NET TripleDESCryptoServiceProvider相当于在Java中 [英] .NET TripleDESCryptoServiceProvider equivalent in Java

查看:284
本文介绍了.NET TripleDESCryptoServiceProvider相当于在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请,只是不要问我为什么。我只是有这个代码在.NET中的加密/解密的数据串。我现在需要做恰好在java中相同的funcionality。我曾尝试的DESede隐窝几个例子,但他们没有给出相同的结果,因为这类.NET。



我即使在背后做一个SSL .NET webserbvice服务于.NET writen这两种方法,但它仅仅是太傻没有用尽所有的posibilities做的。



也许你们中的一些人的Java这是在更多的相关区域会对你的头顶部如何做到这一点。



谢谢!

 公共类加密
{
私人静态的byte [] sharedkey = {...};
私人静态的byte [] sharedvector = {...};

公共静态字符串解密(字符串VAL)
{
TripleDESCryptoServiceProvider TDES =新TripleDESCryptoServiceProvider();
字节[] = toDecrypt Convert.FromBase64String(VAL);
的MemoryStream毫秒​​=新的MemoryStream();
CryptoStream的CS =新的CryptoStream(MS,tdes.CreateDecryptor(sharedkey,sharedvector),CryptoStreamMode.Write);

cs.Write(toDecrypt,0,toDecrypt.Length);
cs.FlushFinalBlock();
返回Encoding.UTF8.GetString(ms.ToArray());
}

公共静态加密字符串(字符串VAL)
{
TripleDESCryptoServiceProvider TDES =新TripleDESCryptoServiceProvider();
字节[] = toEncrypt Encoding.UTF8.GetBytes(VAL);
的MemoryStream毫秒​​=新的MemoryStream();
CryptoStream的CS =新的CryptoStream(MS,tdes.CreateEncryptor(sharedkey,sharedvector),CryptoStreamMode.Write);
cs.Write(toEncrypt,0,toEncrypt.Length);
cs.FlushFinalBlock();
返回Convert.ToBase64String(ms.ToArray());
}
}






Samle输入/输出



 字符串平原=userNameHere:passwordHere 
Console.WriteLine(普通:+纯);


字符串加密=加密(纯);
Console.WriteLine(加密:+加密);
//zQPZgQHpjxR + 41Bc6 + 2Bvqo7 + pQAxBBVN + 0V1tRXcOc =

字符串decripted =解密(加密);
Console.WriteLine(decripted:+ decripted);
//userNameHere:passwordHere


解决方案

代码如下,但首先几个音符。




  1. 一个不同的初始化向量必须选择每个消息。硬编码的初始化向量就没有意义了。该IV应该随着密文的消息接收者(这不是秘密)发送。

  2. 我用我自己的实用工具类基础-64编码。您可以使用 sun.misc.BASE64Encoder sun.misc.BASE64Decoder 而是使用第三方库像BouncyCastle的,或写你自己的。

  3. 您使用的是2键三重DES,其中第一个键,第三个键是一样的。我修改 sharedkey 来反映这一点,因为在Java的DESede密码总是需要一个192位的密钥;它是由密钥生成处理的密钥选项。

  4. 系统CBC IV只有64位。我用只有 sharedvector 第64位。



这班与C#版本应该互操作

 进口javax.crypto.Cipher中; 
进口javax.crypto.spec.IvParameterSpec;
进口javax.crypto.spec.SecretKeySpec;

公共类加密
{

私人静态的byte [] sharedkey = {
0×01,0×02,×03,0×05,0×07,0x0B中,符进行, 0×11,
0×12,0×11,符进行,0x0B中,0×07,0×02,0×04,0×08,
0×01,0×02,×03,0×05,0×07,0x0B中,符进行,为0x11
};

私人静态的byte [] sharedvector = {
0×01,0×02,×03,0×05,0×07,0x0B中,符进行,为0x11
};

公共静态无效的主要(字符串...的argv)
抛出异常
{
字符串明文=userNameHere:passwordHere
字符串密文=加密(明文);
的System.out.println(密文);
的System.out.println(解密(密文));
}

公共静态加密字符串(字符串明文)
抛出异常
{
密码C = Cipher.getInstance(的DESede / CBC / PKCS5Padding );
c.init(Cipher.ENCRYPT_MODE,新SecretKeySpec(sharedkey的DESede),新IvParameterSpec(sharedvector));
字节[] =加密c.doFinal(plaintext.getBytes(UTF-8));
返回Base64.encode(加密的);
}

公共静态字符串解密(字符串密文)
抛出异常
{
密码C = Cipher.getInstance(的DESede / CBC / PKCS5Padding );
c.init(Cipher.DECRYPT_MODE,新SecretKeySpec(sharedkey的DESede),新IvParameterSpec(sharedvector));
字节[]解密= c.doFinal(Base64.decode(密文));
返回新的String(解密,UTF-8);
}

}



输出:




zQPZgQHpjxR + 41Bc6 + 2Bvqo7 + pQAxBBVN + 0V1tRXcOc =



userNameHere:passwordHere



Please, just don't ask me why. I just have this code in .NET that encrypt/decrypt strings of data. I need now to make 'exactly' the same funcionality in java. I have tried several examples for DESede crypt, but none of them gives the same results as this class in .net.

I even though on making a .net webserbvice behind ssl to serve this two methods writen in .net but it is just too stupid to do without exhausting all the posibilities.

Maybe some of you java people which are more related in the area will have on top of your heads how to make it.

Thanks !!!

public class Encryption
{
  private static byte[] sharedkey = {...};
  private static byte[] sharedvector = {...};

  public static String Decrypt(String val)
  {
    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    byte[] toDecrypt = Convert.FromBase64String(val);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, tdes.CreateDecryptor( sharedkey, sharedvector ), CryptoStreamMode.Write);

    cs.Write(toDecrypt, 0, toDecrypt.Length);
    cs.FlushFinalBlock();
    return Encoding.UTF8.GetString(ms.ToArray());
  }

  public static String Encrypt(String val)
  {
    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    byte[] toEncrypt = Encoding.UTF8.GetBytes(val);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, tdes.CreateEncryptor( sharedkey, sharedvector ), CryptoStreamMode.Write);
    cs.Write(toEncrypt, 0, toEncrypt.Length);
    cs.FlushFinalBlock();
    return Convert.ToBase64String(ms.ToArray());
  }
}


Samle input/output

String plain = "userNameHere:passwordHere";
Console.WriteLine("plain: " + plain);


String encrypted = Encrypt(plain);
Console.WriteLine("encrypted: " + encrypted);
// "zQPZgQHpjxR+41Bc6+2Bvqo7+pQAxBBVN+0V1tRXcOc="

String decripted = Decrypt(encrypted);
Console.WriteLine("decripted: " + decripted); 
// "userNameHere:passwordHere"

解决方案

Code follows, but first a few notes.

  1. A different initialization vector must be chosen for every message. Hard-coding the initialization vector does not make sense. The IV should be sent along with the cipher text to the message recipient (it's not secret).
  2. I used my own utility class for base-64 encoding. You can use sun.misc.BASE64Encoder and sun.misc.BASE64Decoder instead, use a third-party library like BouncyCastle, or write your own.
  3. You are using 2-key triple DES, where the first key and the third key is the same. I modified sharedkey to reflect this, since the Java DESede cipher always requires a 192-bit key; it's up to the key generator to handle the keying option.
  4. A CBC IV is only 64 bits. I've used only the first 64 bits of sharedvector.

This class should inter-operate with the C# version.

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Encryption
{

  private static byte[] sharedkey = {
    0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11, 
    0x12, 0x11, 0x0D, 0x0B, 0x07, 0x02, 0x04, 0x08, 
    0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11
  };

  private static byte[] sharedvector = {
    0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11
  };

  public static void main(String... argv)
    throws Exception
  {
    String plaintext = "userNameHere:passwordHere";
    String ciphertext = encrypt(plaintext);
    System.out.println(ciphertext);
    System.out.println(decrypt(ciphertext));
  }

  public static String encrypt(String plaintext)
    throws Exception
  {
    Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sharedkey, "DESede"), new IvParameterSpec(sharedvector));
    byte[] encrypted = c.doFinal(plaintext.getBytes("UTF-8"));
    return Base64.encode(encrypted);
  }

  public static String decrypt(String ciphertext)
    throws Exception
  {
    Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sharedkey, "DESede"), new IvParameterSpec(sharedvector));
    byte[] decrypted = c.doFinal(Base64.decode(ciphertext));
    return new String(decrypted, "UTF-8");
  }

}

Output:

zQPZgQHpjxR+41Bc6+2Bvqo7+pQAxBBVN+0V1tRXcOc=

userNameHere:passwordHere

这篇关于.NET TripleDESCryptoServiceProvider相当于在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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