【问题标题】:Is there any difference between implementation of Triple DES in C# and Java? Java gives error wrong IV size在 C# 和 Java 中实现 Triple DES 有什么区别吗? Java 给出错误的 IV 大小
【发布时间】:2013-01-16 06:22:15
【问题描述】:

在我们的应用程序中,我们使用三重 DES 来加密和解密数据。我们在 C# 中有 enc/dec 代码,它使用 24 字节密钥和 12 字节 IV,可以正常工作。现在我们想在 java 中实现相同的代码,但是当我使用 12 字节 IV 时,我在 java 中得到一个错误,说 IV 大小错误。当我四处搜索时,我知道 java 使用 8 字节 IV。现在我很困惑三重 DES 的 C# 和 JAVA 的实现差异。还是我错过了什么?

这类似于我们的加密代码

class cTripleDES
{
// define the triple des provider
private TripleDESCryptoServiceProvider m_des = new TripleDESCryptoServiceProvider();

// define the string handler
private UTF8Encoding m_utf8 = new UTF8Encoding();

// define the local property arrays
private byte[] m_key;
private byte[] m_iv;

public cTripleDES(byte[] key, byte[] iv)
{
    this.m_key = key;
    this.m_iv = iv;
}

public byte[] Encrypt(byte[] input)
{
    return Transform(input,
           m_des.CreateEncryptor(m_key, m_iv));
}

public byte[] Decrypt(byte[] input)
{
    return Transform(input,
           m_des.CreateDecryptor(m_key, m_iv));
}

public string Encrypt(string text)
{
    byte[] input = m_utf8.GetBytes(text);
    byte[] output = Transform(input,
                    m_des.CreateEncryptor(m_key, m_iv));
    return Convert.ToBase64String(output);
}

public string Decrypt(string text)
{
    byte[] input = Convert.FromBase64String(text);
    byte[] output = Transform(input,
                    m_des.CreateDecryptor(m_key, m_iv));
    return m_utf8.GetString(output);
}

private byte[] Transform(byte[] input,
               ICryptoTransform CryptoTransform)
{
    // create the necessary streams
    MemoryStream memStream = new MemoryStream();
    CryptoStream cryptStream = new CryptoStream(memStream,
                 CryptoTransform, CryptoStreamMode.Write);
    // transform the bytes as requested
    cryptStream.Write(input, 0, input.Length);
    cryptStream.FlushFinalBlock();
    // Read the memory stream and
    // convert it back into byte array
    memStream.Position = 0;
    byte[] result = memStream.ToArray();
    // close and release the streams
    memStream.Close();
    cryptStream.Close();
    // hand back the encrypted buffer
    return result;
}

}

这就是我们使用它的方式:

string IVasAString = "AkdrIFjaQrRQ";
byte[] iv = Convert.FromBase64String(IVasAString);
byte[] key = ASCIIEncoding.UTF8.GetBytes(KEY);

// instantiate the class with the arrays
cTripleDES des = new cTripleDES(key, iv);
string output = des.Encrypt("DATA TO BE ENCRYPTED");

【问题讨论】:

    标签: c# java encryption encryption-symmetric tripledes


    【解决方案1】:

    TripleDES 有一个64-bit block size。您需要在 C# 中使用 8 byte IV

    【讨论】:

    • 我已经添加了有问题的代码。 'Convert.FromBase64String' 是否将字符串转换为 8 字节数组?
    • 是的。刚刚检查过。 C# 中的 IV 为 8 个字节。我怎样才能在 JAVA 中达到同样的效果?
    • 抱歉,您的问题是什么?如果我的回答回答了您的问题,您可以投票并将其标记为已接受吗?
    • 实际上你的回答描述了这个问题。 :)
    【解决方案2】:

    得到了答案。 来自 apache 通用框架 (commons.codec.binary.Base64) 的 decodeBase64 方法是必要的。 感谢 mfanto 的提醒。!

    【讨论】:

      猜你喜欢
      • 2014-07-07
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多