Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
rcmaniac25
Posts: 1,805
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.634 Dev OS, Z30 (STA100-5)-10.3.1.634 Dev OS, Passport (SQW100-1)-10.3.0.1418, PlayBook (16GB)-2.1.0.1917

Re: Something About DesEncrypt

What didn't work? Did you get an exception or something?

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Contributor
fatslim
Posts: 12
Registered: ‎06-16-2011
My Device: 9700

Re: Something About DesEncrypt

the plaintext is something i input and the deskey  is the specific words。

 

i input the word and clicked the run button, there's nothing came out。  it is supposed to appear the encrypted data in hex format

 

in my code, i input plaintext and clicked the button, there came out the hex format string, but it is correct oneas expected。。。。

 

sorry for my poor english,hope u could understand me

Please use plain text.
Developer
rcmaniac25
Posts: 1,805
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.634 Dev OS, Z30 (STA100-5)-10.3.1.634 Dev OS, Passport (SQW100-1)-10.3.0.1418, PlayBook (16GB)-2.1.0.1917

Re: Something About DesEncrypt

Could you maybe post the sample code you are using to get a better idea?
---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Contributor
fatslim
Posts: 12
Registered: ‎06-16-2011
My Device: 9700

Re: Something About DesEncrypt

U mean the code I'm using in blackberry eclips plugin?
Please use plain text.
Developer
rcmaniac25
Posts: 1,805
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.634 Dev OS, Z30 (STA100-5)-10.3.1.634 Dev OS, Passport (SQW100-1)-10.3.0.1418, PlayBook (16GB)-2.1.0.1917

Re: Something About DesEncrypt

Ya, basically.

 

Other option is:

  • How are you converting the byte array to a hex String?
  • Are the values in the array themselves wrong or is it just the hex printout?
  • When you say nothing came out, do you mean like an empty String or array? Or did an exception occur so you didn't get anything?

The reason for asking to see the code is to see what you are doing, otherwise I ask more questions to try to get what could be the same information.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Contributor
fatslim
Posts: 12
Registered: ‎06-16-2011
My Device: 9700

Re: Something About DesEncrypt

How are you converting the byte array to a hex String?

i'm doing it like this:

public static String byte2hex(byte[] b) {
			String hs = "";
			String stmp = "";

			for (int n = 0; n < b.length; n++) {
				stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
				if (stmp.length() == 1)
					hs = hs + "0" + stmp;
				else
					hs = hs + stmp;
			}
			return hs;
		}

 

Are the values in the array themselves wrong or is it just the hex printout?

 now, the hex printout  isnt what i'm expecting

 

When you say nothing came out, do you mean like an empty String or array? Or did an exception occur so you didn't get anything?

i tried your way of code, i think there's an exception occur so i didn't get anything?

 

in my way code, hex  printout isnt correct as i expecting。

Please use plain text.
Contributor
fatslim
Posts: 12
Registered: ‎06-16-2011
My Device: 9700

Re: Something About DesEncrypt

[ Edited ]
public final static String encode(String data) throws Exception {
                String key=“fatslimfatslim”;
		return byte2hex(encrypt(data.getBytes(), key.getBytes()));
	}
		
		private static byte[] encrypt(byte[] Dkey, byte[] data) throws Exception {
			// Create a new DES key based on the 8 bytes in the secretKey array
			
	        DESKey key = new DESKey( Dkey );
	                
	        // Create a new instance of the DES encryptor engine, passing in the newly 
	        // created key
	        DESEncryptorEngine engine = new DESEncryptorEngine( key );
	        // Encrypt one block (8 bytes) of plainText into cipherText
	        //engine.encrypt( data, 0, null, 0 );
	        byte[] cipherText=new byte[50];
	        engine.encrypt( data, 0, cipherText, 0 );
	        return cipherText;
			  
		}

		public static String byte2hex(byte[] b) {
			String hs = "";
			String stmp = "";

			for (int n = 0; n < b.length; n++) {
				stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
				if (stmp.length() == 1)
					hs = hs + "0" + stmp;
				else
					hs = hs + stmp;
			}
			return hs;
		}

 

those r my codes。。。。。data is the plaintext i will input。。。。。

 

Please use plain text.
Developer
rcmaniac25
Posts: 1,805
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.634 Dev OS, Z30 (STA100-5)-10.3.1.634 Dev OS, Passport (SQW100-1)-10.3.0.1418, PlayBook (16GB)-2.1.0.1917

Re: Something About DesEncrypt

Ok, first thing I notice is that in your encrypt function you mix up the key and data. Your passing the data bytes into the key, and the key bytes into the data.

 

Second, (I'm learning this at the same time) the data must be at least 8 bytes and it will only process 8 bytes. You can pass data in multiple times to continue processing but it will only work with 8 bytes.

 

Third, (again I'm learning) I was wrong with the whole "pass the null" in code. I didn't understand the "one block=8 bytes" thing.

 

Finally, when I compared the J2SE (desktop) results with the BlackBerry results I got good news and bad news. The good news is the data matches, the bad news is it's half the length:

  • Desktop: 7cce31a8c460407dda56377bf46de282
  • BlackBe: 7cce31a8c460407d

Those are the results I got for using "HellWorld" (it's 8 bytes) in both the J2SE and BlackBerry DES encryptors. I have a feeling the "values" for the extra 8 bytes on the J2SE one have to do with the SecureRandom you pass in. Why it has 8 more bytes (or why BlackBerry only does 8 bytes) I don't know.

 

Hope that helps.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Contributor
fatslim
Posts: 12
Registered: ‎06-16-2011
My Device: 9700

Re: Something About DesEncrypt

thanks for your help and patients。。。。

finally,the biggest mistake  is like u said, i mixed the data and key。。。。

 

after this issue, i think i gonna be careful with every word i typed!

 

i'm still need to learn more,the result i wanna comes and like u said,on bb, there only 16 digits。。。thats ok。

 

again,thanks for your kindly help

Please use plain text.