使用带填充的DES使用提供的密钥进行加密 [英] encryption using provided key using DES with padding

查看:109
本文介绍了使用带填充的DES使用提供的密钥进行加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用给定密钥使用DES加密帧.

I want to encrypt a frame using DES using a given key.

我使用的填充样式是PKCS5Padding.如果要添加2个字节,则用02 02填充字符串;如果要添加3个字节,则用03 03 03填充字符串,以乘以8. 但是我的要求是用我的特定字节填充字符串.例如,如果要添加2个字节,则添加30 30,然后添加3个字节,则添加30 30 30(十六进制0的值为30).另外,我必须知道已经添加了多少填充字节. 我应该遵循哪种填充技术以及如何使用它?

The padding style I am using is PKCS5Padding. This pads the string with 02 02 if 2 bytes are to be added and with 03 03 03 if 3 bytes are to be added to make multiple of 8. But my requirement is to pad a string with my particular bytes. e.g if 2 bytes are to be added then 30 30 and 3 bytes are to be added then 30 30 30 (in hex 0's value is 30). Also, I must know how many padded bytes have been added. Which padding technique should I follow and how can I use it?

下面是我的加密代码:

byte[] keyValue = new byte[]{(byte) 0x30, (byte) 0x30, (byte) 0x30, (byte) 0x30, (byte) 0x30, (byte) 0x16,(byte) 0x05, (byte) 0x12};
myKeySpec = new DESKeySpec(keyValue);
mySecretKeyFactory = SecretKeyFactory.getInstance("DES");
key = mySecretKeyFactory.generateSecret(myKeySpec);


Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
IvParameterSpec iv2 = new IvParameterSpec(new byte[8]);
cipher.init(Cipher.ENCRYPT_MODE, key, iv2);
byte[] plainText = function.HexStringToByteArray(payloadRecv);
byte[] encryptedText = cipher.doFinal(plainText);

推荐答案

请勿在密码规范中选择PKCS5Padding.选择NoPadding.在加密之前,您必须自己将填充添加到数据上.解密后(同样不使用填充),您必须检查最后一个字节,以了解要删除和自己删除填充的多少字节.

Do not select PKCS5Padding in the cipher specification. Select NoPadding. You'll have to add the padding onto the data yourself prior to encrypting it. After decrypting it (also using no padding), you'll have to inspect the last byte to know how many bytes of padding to remove and remove it yourself.

基本上,只需完全按照您的描述进行编码即可.

Basically, just code exactly what you described.

这篇关于使用带填充的DES使用提供的密钥进行加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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