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
Contributor
fatslim
Posts: 12
Registered: ‎06-16-2011
My Device: 9700
My Carrier: China Mobile
Accepted Solution

Something About DesEncrypt

hi  i'm newbie for blackberry  development ,even for java。

 

just reading the blackberry api now。

 

the sample DESEncrypt code is here,i just wanna know if i could get the encrypted data  cipherText?

 

how to retrieve it? it's very kind of u guys may come up with any ideas。

private static int sampleDESEncryption( byte[] secretKey, byte[] plainText, byte[] cipherText )
        throws CryptoTokenException, CryptoUnsupportedOperationException
    {
        // Create a new DES key based on the 8 bytes in the secretKey array
        DESKey key = new DESKey( secretKey );
                
        // 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( plainText, 0, cipherText, 0 );
    
        // Return the block size of the engine
        return engine.getBlockLength();
    }    

 

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
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.1154, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Something About DesEncrypt

Welcome.

 

What do you mean? cipherText is a buffer that the encrypted text will be copied into.

---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
My Carrier: China Mobile

Re: Something About DesEncrypt

I mean, if I could get cipherText and output it?
I don't know how to get clipherText
Please use plain text.
Contributor
fatslim
Posts: 12
Registered: ‎06-16-2011
My Device: 9700
My Carrier: China Mobile

Re: Something About DesEncrypt

still waiting for someone could help

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
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.1154, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Something About DesEncrypt

I'm not sure what more I can tell you. You create an array for the encrypted text, pass in a key and text data along with the buffer and the buffer will contain the encrypted results.

---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
My Carrier: China Mobile

Re: Something About DesEncrypt

[ Edited ]

thanks very much for helping me

what i want is output the cipherText  as byte array

 

how can i do this?

 

System.out.println(“Encrypted Data : ” +cipherText );

 

i tried, seems not the correct result

 

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

Re: Something About DesEncrypt

fine, this is what i could run on pc。  DES Encrypt

 

i just wanna bb run as the way pc do。。。。

bb api sample desencryptengine just return the blocksize, not  the encrypted data。。。。

 

maybe  it's easy or silly question, but i do not know。。。。thanks for helping me

private final static String ALGORITHM = "DES";

private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
		
		SecureRandom sr = new SecureRandom();
		
		DESKeySpec dks = new DESKeySpec(key);
		
		
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
		SecretKey securekey = keyFactory.generateSecret(dks);
		
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		
		cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
		
		return cipher.doFinal(data);
	}

 

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
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.1154, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Something About DesEncrypt

[ Edited ]

EDIT: Don't use

 

It shouldn't be that tough to do:

private static byte[] sampleDESEncryption( byte[] secretKey, byte[] plainText )
    throws CryptoTokenException, CryptoUnsupportedOperationException
{
    // Create a new DES key based on the 8 bytes in the secretKey array
    DESKey key = new DESKey( secretKey );
    
    // 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 to get size
    engine.encrypt( plainText, 0, null, 0 );
    
    // Create the buffer to hold the encrypted text
    byte[] cipherText = new byte[engine.getBlockLength()];
    
    // Encrypt one block (8 bytes) of plainText to cipherText
    engine.encrypt( plainText, 0, cipherText, 0 );
    
    // Return the cipher text
    return cipherText;
}

 Note: I am not the greatest at encryption so this might not be the proper way to do it. I "think" you can pass in the data to find the size needed for the cipherText buffer but am not sure.

---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
My Carrier: China Mobile

Re: Something About DesEncrypt

thank u very much!! i'm doing the test now..

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

Re: Something About DesEncrypt

thanks very much. i did try your code .... doesnt work,  it seems   cipherText []  overflow.

 

i tried this kind way , but  the result isnt correct.....

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;
			  
		}

 

 

Please use plain text.