如何在java中使用TripleDESCryptoServiceProvider没有任何问题

时间:2017-12-16 11:30:14

标签: java encryption c#-2.0

我有一个项目,我需要将c#代码转换为java。我在java中采用TripleDESCryptoServiceProvider功能时遇到了问题。好消息是我找到了一个解决方案,但不幸的是它没有完美运作

c#c​​ode

using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace HelloWorldApplication {

class HelloWorld {

private TripleDESCryptoServiceProvider TripleDes;
      private byte[] key;
      private byte[] iv;
  static void Main(string[] args) {

     new HelloWorld();
  }
  public HelloWorld(){

     this.TripleDes = new TripleDESCryptoServiceProvider();
           this.key = new byte[]
           {
                14, 
                4, 
                5, 
                12, 
                13, 
                6, 
                18, 
                19, 
                20, 
                1, 
                15, 
                16, 
                17, 
                2, 
                3, 
                23, 
                24, 
                7, 
                8, 
                9, 
                10, 
                11, 
                21, 
                22
           };
           this.iv = new byte[]
           {
                6, 
                5, 
                8, 
                3, 
                2, 
                7, 
                4, 
                1
           };
           string data="this is string data";
           string enc=EncryptData(data);
           Console.WriteLine(enc);
  }

  public string EncryptData(string plaintext)
      {
           byte[] bytes = Encoding.Unicode.GetBytes(plaintext);
           MemoryStream memoryStream = new MemoryStream();
           CryptoStream cryptoStream = new CryptoStream(memoryStream, 
           this.TripleDes.CreateEncryptor(this.key, this.iv), 
           CryptoStreamMode.Write);
           cryptoStream.Write(bytes, 0, bytes.Length);
           cryptoStream.FlushFinalBlock();
           return Convert.ToBase64String(memoryStream.ToArray());
      }

  public string DecryptData(string encryptedtext)
      {
           byte[] arr_ = Convert.FromBase64String(encryptedtext);
           MemoryStream memoryStream = new MemoryStream();
           CryptoStream cryptoStream = new CryptoStream(memoryStream, 
           this.TripleDes.CreateDecryptor(this.key, this.iv), 
           CryptoStreamMode.Write);
           cryptoStream.Write(arr_, 0, arr_.Length);
           cryptoStream.FlushFinalBlock();
           return Encoding.Unicode.GetString(memoryStream.ToArray());
      }

   }
}

输出:" I6rXj2YRY6WN9iOoHbrHJcbPkAPDEeFWfBJr0dYxWNXwYAhcjJZX7w =="

以下是相应的java代码:

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

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import sun.misc.BASE64Encoder;

public class HelloWorld
 {

  private static byte[] sharedkey = new byte[]
      {
   14, 
   4, 
   5, 
   12, 
   13, 
   6, 
   18, 
   19, 
   20, 
   1, 
   15, 
   16, 
   17, 
   2, 
   3, 
   23, 
   24, 
   7, 
   8, 
   9, 
   10, 
   11, 
   21, 
   22
 };

  private static byte[] sharedvector = new byte[]
      {
  6, 
  5, 
  8, 
  3, 
  2, 
  7, 
  4, 
  1
};

public static void main(String[] argv)
throws Exception
{
String enctext ="I6rXj2YRY6WN9iOoHbrHJcbPkAPDEeFWfBJr0dYxWNXwYAhcjJZX7w==";
String text = decrypt(enctext);
System.out.println(text);
}

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");
}

}

输出:

enter image description here

如何摆脱这个问题?

2 个答案:

答案 0 :(得分:0)

您的解决方案是正确的,您需要在eclipse运行配置中启用UTF-8。我能够在解密后从相同的代码中获取纯文本。enter image description here

答案 1 :(得分:0)

您应该设置Project-> Properties-> Resource-> Text Fie编码:UTF-8

public static void main(String[] argv) throws Exception {
    String enctext = "I6rXj2YRY6WN9iOoHbrHJcbPkAPDEeFWfBJr0dYxWNXwYAhcjJZX7w==";
    String text = decrypt(enctext);
    System.out.println("decrypt:" + text);
    String text2 = encrypt(text);
    System.out.println("entcrypt:" + text2);
}

输出:

decrypt:t
entcrypt:I6rXj2YRY6WN9iOoHbrHJcbPkAPDEeFWfBJr0dYxWNXwYAhcjJZX7w==

您的代码有效!