What is wrong with my DESKey (BlackBerry API)?
I have an application developed on BlackBerry JE 4.6.1 that decrypts an information from WebServer using DES algorythm. If I send encrypted information to the server, it is decrypted well. But in case if the server sends an encrypted d开发者_高级运维ata, I do not get the correct value after decryption. Key is supposed to be the same and crypted information is sent base64 encoded. During debugging I have found out, that after DESKey is created it's inner data differs from the byte array passed to the constructor. For example if I create the DESKey the next way
String keyStr = "2100000A";
DESKey desKey = new DESKey(keyStr.getBytes()); // pass the byte array {'2','1','0','0','0','0','0','A'}
the method desKey.getData() returns the byte array {'2','1','1','1','1','1','1','@'} that differs from the initial key bytes.
So is it possible for such behavior of the DESKey to be the reason why I can not decrypt data from server?
Thank you.
The desKey.getData() behaviour is expected.
The doc states:
DES operates on 64 bit blocks and has an effective key length of 56 bits. In reality, the key is 64 bits but there are 8 bits of parity used which means that the effective key length is only 56 bits. Every eighth bit is used for parity and it is the least significant bit that is used for parity.
Parity bit definition:
A parity bit, or check bit, is a bit that is added to ensure that the number of bits with the value one in a set of bits is even or odd. Parity bits are used as the simplest form of error detecting code.
So, this is how it happens:
'2' => 0x32 => 00110010 => 0011001 + (parity bit 0) => 00110010 => 0x32 => '2'
'1' => 0x31 => 00110001 => 0011000 + (parity bit 1) => 00110001 => 0x31 => '1'
'0' => 0x30 => 00110000 => 0011000 + (parity bit 1) => 00110001 => 0x31 => '1'
'0' => 0x30 => 00110000 => 0011000 + (parity bit 1) => 00110001 => 0x31 => '1'
'0' => 0x30 => 00110000 => 0011000 + (parity bit 1) => 00110001 => 0x31 => '1'
'0' => 0x30 => 00110000 => 0011000 + (parity bit 1) => 00110001 => 0x31 => '1'
'0' => 0x30 => 00110000 => 0011000 + (parity bit 1) => 00110001 => 0x31 => '1'
'A' => 0x41 => 01000001 => 0100000 + (parity bit 0) => 01000000 => 0x40 => '@'
精彩评论