三重DES解密密钥无效,16字节 [英] Triple DES decryption invalid key with 16 bytes

查看:1149
本文介绍了三重DES解密密钥无效,16字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android项目中,我得到一个三重DES加密的一段文字,从我的Web服务。我需要三重DES解密。

I have an android project in which am getting an Triple DES encrypted piece of text from my web services. I need to Triple DES decrypt.

不过,我收到无效键例外。我的键转换为十六进制格式,我得到了一个错误: W / System.err的:java.security.InvalidKeyException:DES密钥过长 - 应该是8个字节我发现< A HREF =htt​​ps://community.oracle.com/thread/1528631相对=nofollow>这里一个论坛,并解释说,十六进制可能会导致问题

However, I am getting invalid key exceptions. My key is converted to HEX format and I got an error: W/System.err﹕ java.security.InvalidKeyException: DES key too long - should be 8 bytes I found here a forum explaining that hex may cause issue

DES密钥是56位的8个字节通常包装,以便有机会,在16字节/字符,他们给你的十六进制EN codeD字节的关键。你可以得到一个十六进制德codeR

所以,我用我的转换十六进制字符串为一个字节数组

So I converted my hex string to a byte array using

 private static byte[] hexStringtoByteArray(String hex){
        int len = hex.length();

        byte [] data = new byte[len/2];
        for(int i=0; i<len;i+=2){
            data[i/2] = (byte)((Character.digit(hex.charAt(i), 16)<<4) + Character.digit(hex.charAt(i+1),16));
        }
        return data;
    }

和通过了到密码,我得到一个错误:

and passed that in to the cipher and I get an error:

W/System.err﹕ java.security.InvalidKeyException
W/System.err﹕ at javax.crypto.spec.DESedeKeySpec.

这是我的解密方法。我想AP preciate如果有人能照到的地方,我可能会错误的一些情况。

here is my decrypt method. I would appreciate if someone could shine some light on where I might be going wrong.

 public String DesDecryptPin(String pin, String encryptKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {

    String UNICODE_FORMAT = "UTF8";
    String decryptedPinText = null;

    byte[] hexConvert = hexStringtoByteArray(encryptKey);

    SecretKey desKey = null;
    KeySpec desKeySpec = new DESedeKeySpec(hexConvert); // Exception HERE
    Cipher desCipher;
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
    desCipher = Cipher.getInstance("DES/ECB/NoPadding");
    try {
        desKey = skf.generateSecret(desKeySpec);
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    }

    desCipher.init(Cipher.DECRYPT_MODE, desKey);
    byte[] decryptPin = desCipher.doFinal(pin.getBytes());
    decryptedPinText = new String(decryptPin, "UTF-8");

    return decryptedPinText;
}

我的关键是C9AF269DF8A78A06D1216BFFF8F0536A。

My key is C9AF269DF8A78A06D1216BFFF8F0536A.

我已经检查与客户和键是正确的,所以相同的密钥被用于加密。

加密code

Encryption Code

 public string TripleDESEncrypt(string strClearText, string strKey)
    {
        byte[] bytClearText;
        byte[] bytClearTextChunk = new byte[8];
        byte[] bytEncryptedChunk = new byte[8];
        int BytesCount = 0;
        int nArrayPosition = 0;
        string strEncryptedChar;
        string strEncryptedText = "";

        ArrayList Input = new ArrayList();
        ArrayList Output = new ArrayList();

        TripleDESCryptoServiceProvider tdes = (TripleDESCryptoServiceProvider)TripleDESCryptoServiceProvider.Create();

        tdes.Key = HexToByteArray(strKey);
        tdes.Mode = CipherMode.ECB;

        ICryptoTransform tdesEncrypt = tdes.CreateEncryptor();

        bytClearText = ASCIIEncoding.ASCII.GetBytes(strClearText);
        BytesCount = bytClearText.Length;

        for (int i = 0; i < BytesCount; i++)
        {
            if (nArrayPosition == 8)
            {
                Input.Add(bytClearTextChunk);
                bytClearTextChunk = new byte[8];
                nArrayPosition = 0;
            }
            bytClearTextChunk[nArrayPosition] = bytClearText[i];
            nArrayPosition++;
        }

        if (nArrayPosition != 0)
            Input.Add(bytClearTextChunk);


        foreach (byte[] Cbyte in Input)
        {
            tdesEncrypt.TransformBlock(Cbyte, 0, 8, bytEncryptedChunk, 0);
            Output.Add(bytEncryptedChunk);
            bytEncryptedChunk = null;
            bytEncryptedChunk = new byte[8];
        }


        foreach (byte[] Cbyte in Output)
        {
            foreach (byte BByte in Cbyte)
            {
                strEncryptedChar = BByte.ToString("X");
                strEncryptedChar = strEncryptedChar.PadLeft(2, Convert.ToChar("0"));
                strEncryptedText += strEncryptedChar;
            }
        }

        return strEncryptedText;
    }

下面是解密文本的14个字符的例子:12345678901234

Here is an example of the decrypted text with 14 chars: 12345678901234

推荐答案

您实例化你的密码为(辛格运河)DES密码有:

You instantiate your Cipher as a (singel) DES Cipher with:

desCipher = Cipher.getInstance("DES/ECB/NoPadding");

但你的关键是 16个字节 3DES键,thuse你的错误

But your key is a 16 byte 3Des key, and thuse you get the error

DES key too long - should be 8 bytes

尝试实例化你的密码作为3DES加密使用:

Try instantiating your cipher as a 3DES cipher with:

desCipher = Cipher.getInstance("DESede/ECB/NoPadding");

这篇关于三重DES解密密钥无效,16字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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