Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java C#指定密钥是';三倍体';不能使用_Java_C#_Tripledes - Fatal编程技术网

Java C#指定密钥是';三倍体';不能使用

Java C#指定密钥是';三倍体';不能使用,java,c#,tripledes,Java,C#,Tripledes,我有一个用Java编写的身份验证过程,它将一个字符串加密到MD5中并生成一个字符串,只取该字符串的前8位。 在本例中,生成的字符串是“89a5c474”。 之后,我有下面这段Java代码,其中我使用了TripleDesEncryption public static byte[] encrypt(byte[] keybyte, byte[] src) throws NoSuchAlgorithmException, NoSuchPaddingException, Exception {

我有一个用Java编写的身份验证过程,它将一个字符串加密到MD5中并生成一个字符串,只取该字符串的前8位。 在本例中,生成的字符串是“89a5c474”。

之后,我有下面这段Java代码,其中我使用了TripleDesEncryption

public static byte[] encrypt(byte[] keybyte, byte[] src) throws NoSuchAlgorithmException, NoSuchPaddingException, Exception 
{ 
    System.out.println("Key Byte " + byte2hex(keybyte) + "Key Byte Array" + keybyte);
    System.out.println("Key String " + byte2hex(src));
    SecretKey deskey = new SecretKeySpec(keybyte, DESede); 
    Cipher c1 = Cipher.getInstance(Algorithm); 
    c1.init(Cipher.ENCRYPT_MODE, deskey); 
    return c1.doFinal(src);
} 

public static void main(String[] args) 
{
        final byte[] rawKey = "89a5c474".getBytes(); 
        final byte[] keyBytes = new byte[24]; 
        for (int i = 0; i <rawKey.length; i++) 
        { 
            keyBytes[i] = rawKey[i]; 
        }

        for (int i = rawKey.length; i <keyBytes.length; i++) 
        { 
            keyBytes[i] = (byte)0; 
        }


        String szSrc = "20126303$4A6D9BD0DDD094B76C111577A49EB87A$Guest$PC$193.92.123.5$$Reserved$CTC";         
        byte[] encoded = null;

        try 
        {            
            encoded = encrypt(keyBytes, szSrc.getBytes());          
        } 
        catch (Exception e) 
        { 
            e.printStackTrace(); 
        }                       
}
Java=D68D8423EB01421E8F23C118D3AEF6A6998D8F2A62CEB697377195AA979FE5E97141454716E6D6B41C56AF296BCD6AB2979C7D9233898BAEF5C9F38FA9FD286D8A6C2A2A4B6697D1EB7C


C#=ff9772125dc1e3a4c9b63dfd429fb3cda43732331025f9b73a092a9421f6869c372ae40b0db1991db0fd04ce5924eb213b8f303721c79f8f4cca384711b7e2adcc862e0003e18ef3cc0da2cd4b7488我已经通过Java中使用的函数和构造函数绕过并重新创建了C#上的3DES加密。对于所有遇到此问题的人,您可以使用Bouncy Castle的assembly,并按照以下链接了解更多信息:

我认为您的密钥可能都是零,这是不允许的。不完全是,在密钥的24个字节中,我只占用了8个字节,而其余的都是0。但是,我不能更改密钥,因为身份验证接口使用的是相同的密钥,但在Java接口中
    public static string AuthenticatePassword(string token, string hashPassword)
    {
                byte[] rawKey = UTF8Encoding.UTF8.GetBytes(hashPassword);
                byte[] keyBytes = new byte[24];



                for (var i = 0; i < rawKey.Length; ++i)
                {
                    keyBytes[i] = rawKey[i];
                }

                for (int i = rawKey.Length; i < keyBytes.Length; i++)
                {
                    keyBytes[i] = 0;
                }

                string keyString = "20126303$" + token + "$Guest$PC$193.92.123.5$$Reserved$CTC";

                return Encrypt(keyBytes, System.Text.Encoding.ASCII.GetBytes(keyString), rawKey, keyString);
    }
public static string Encrypt(byte[] keyBytes, byte[] keyString)
{

            try
            {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

            des.Key = keyBytes;
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.PKCS7;                
            ICryptoTransform ic = des.CreateEncryptor();
            byte[] enc = ic.TransformFinalBlock(keyString, 0, keyString.Length);
            }
            catch (Exception e)
            {
                Console.WriteLine("[Encryption Error] {0}", e.Message); 
            }
return string.Join(string.Empty, enc.Select(x => x.ToString("X2")));
}
        TripleDESCryptoServiceProvider sm = new TripleDESCryptoServiceProvider();
        MethodInfo mi = sm.GetType().GetMethod("_NewEncryptor", BindingFlags.NonPublic | BindingFlags.Instance);
        object[] Par = { EmptyKey, CipherMode.ECB, keyBytes, sm.FeedbackSize, 0 };
        ICryptoTransform trans = mi.Invoke(sm, Par) as ICryptoTransform;
        byte[] enc = trans.TransformFinalBlock(keyString, 0, keyString.Length);