DES Wrong Key Size错误

对接方将3DES密钥用文本给出,我将其base64解码后的bytes来创建SecretKeySpec,报错

java.security.InvalidKeyException: Wrong key size

debug发现base64解码的bytes只有18个字节。。

后来我想是不是他将每个文本字符作为一个字节?统计了以下字符长度,确实是24。

然后直接将字符串的每个字符作为一个字节,将得到的二进制字节作为密钥,解密成功了。

通常来说,提供des密钥应该是提供二进制密钥的base64编码。这次遇到的真是个奇葩的对接方      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
// Java crypt example SJ import javax.crypto.*; import javax.crypto.spec.*; import java.io.*; import java.lang.*; import java.util.*; import java.security.*; public class EncryptionExample { protected String calg = "Blowfish"; // AES. DES, Blowfish protected int keyLen = 128; // 128 for AES, Blowfish, 64 for DES public static void main(String[] args) { EncryptionExample s = new EncryptionExample(); // to call nonstatic methods SecretKeySpec key = s.readkey(); String mess = new String("Hello, world!"); byte[] messb = mess.getBytes(); System.out.println("Plain|" + mess +"|=|" + s.bintohex(messb)); byte[] ct = s.encrypt(messb, key); System.out.println("Encry:" + s.bintohex(ct)); byte[] pt = s.decrypt(ct, key); String dmess = new String(pt); System.out.println("Decry|" + dmess +"|=|" + s.bintohex(pt)); } // main() // encrypt message t with key k public byte[] encrypt(byte[] t, SecretKeySpec k) { try { Cipher c = Cipher.getInstance(calg); c.init(Cipher.ENCRYPT_MODE, k); return c.doFinal(t); } catch (Exception e) { System.err.println("Encryption failed: " + e); } return null; } // decrypt message t with key k public byte[] decrypt(byte[] t, SecretKeySpec k) { try { Cipher c = Cipher.getInstance(calg); c.init(Cipher.DECRYPT_MODE, k); return c.doFinal(t); } catch (Exception e) { System.err.println("Decryption failed: " + e); } return null; } // reads key string from user, returns SecretKeySpec public SecretKeySpec readkey() { SecretKeySpec kp = null; String line; byte [] bin = null; try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Give the key or passphrase (" + keyLen/4 + " hex digits or long ascii string) \n : "); line = in.readLine(); // check if input is all hex or not boolean ishex = true; for (int i = 0; i < line.length(); i++) if (Character.digit(line.charAt(i), 16) < 0) { ishex = false; break; } // check hex key length if (ishex && line.length() != keyLen/4) System.err.println("Wrong hex ley lenght (" + line.length() + "/" + keyLen/4 + ")"); // make binary key if (ishex) bin = hextobin(line); else bin = asciitobin(line); // make key for crypto algorithm kp = new SecretKeySpec(bin, calg); System.out.println("Key = |" + bintohex(kp.getEncoded()) + "|"); } catch (Exception e) { System.err.println("Key generation failed" + e); } return kp; } // readkey() // make binary out of hex string public byte[] hextobin(String s) { int len = (s.length()+1)/2; byte[] A = new byte[len]; for (int i = 0; i < len; i++) A[i] = Integer.valueOf(s.substring(i*2, i*2+2), 16).byteValue(); return A; } // returns new 128 bit key using MD5 of the string s public byte[] asciitobin(String s) { byte[] A = null; try { MessageDigest md = MessageDigest.getInstance("MD5"); A = md.digest(s.getBytes()); } catch (Exception e) { System.err.println("Digest failed" + e); } return A; } // returns new hex string representation of A public String bintohex(byte[] A) { int len = A.length; StringBuffer sb = new StringBuffer(len*2); for (int i = 0; i < len; i++) { if ((A[i] & 0xFF) < 0x10) sb.append("0"); sb.append(Integer.toHexString(A[i] & 0xFF)); } return sb.toString(); } } // class

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值