三重DES解密密钥无效,16字节

编程入门 行业动态 更新时间:2024-10-25 00:30:05
本文介绍了三重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个字节我发现这里一个论坛,并解释说,十六进制可能会导致问题

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字节

本文发布于:2023-11-13 01:43:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:密钥   字节   DES

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!