Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old January 29th, 2012, 03:05 AM   #1 (permalink)
New Member
 
Join Date: Jan 2012
Posts: 5
 
Device(s):
Thanks: 0
Thanked 1 Time in 1 Post
Default How to generate and use variables in different switch statements

I'm trying to encrypt a text with DES algorithm using Bouncy Castle library. I have written the following code:

Code:
    	
    	try{
     		KeyGenerator kg=KeyGenerator.getInstance("DES");
     		SecretKey key=kg.generateKey();
     		SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "DES");
     		Cipher cipher=Cipher.getInstance("DES");
    	}catch(Exception e)
    	{
    		
    	}
    	
    	  	
    	switch(selection){
    	
    	case enc:
    		try{
    			
    			cipher.init(Cipher.ENCRYPT_MODE, keySpec);
    			String inText=inputText.toString();
    			byte[] cipherText=cipher.doFinal(inText.getBytes());
    			encText.setText(new String(cipherText));
    		}catch(Exception e)
			{
			encText.setText("Exception occured!");	
			}
    		break;
    		
    	case dec:
    		try{
    			cipher.init(Cipher.DECRYPT_MODE, keySpec);
    			String encT=encText.toString();
    			byte[] decBytes=cipher.doFinal(encT.getBytes());
    			decText.setText(new String(decBytes));
    		}catch(Exception e)
			{
			decText.setText("Exception occured!");	
			}
    		break;
    	}
    }
The code is supposed to work as follows: The following part of the code will generate "keySpec" and "Cipher" which is used in both encryption and decryption parts in the switch statement.

Code:
try{
     		KeyGenerator kg=KeyGenerator.getInstance("DES");
     		SecretKey key=kg.generateKey();
     		SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "DES");
     		Cipher cipher=Cipher.getInstance("DES");
    	}catch(Exception e)
    	{
Then depending on the user selection, inputText is encrypted or encText is decrypted in the switch statement. Both encryption and decryption blocks share the same "keySpec" and "Cipher" variables generated in the above code snippet. However, the compiler says that keySpec and Cipher cannot be resolved which I understand is that the keySpec and Cipher generated cannot be seen by the statements inside the switch blocks. How can I make it work?

Cheers,

YellowMoon is offline  
Reply With Quote
Sponsors
Old January 29th, 2012, 07:37 AM   #2 (permalink)
Member
 
Join Date: Aug 2010
Posts: 160
 
Device(s):
Thanks: 0
Thanked 7 Times in 6 Posts
Default

The variables in your try block are out of scope by the time
you get to the switch statements.

Move your switch statements into the try block OR make another block that surrounds everything and move the definition of your variable there.

I'd chose the first approach, try/catch blocks are good.

PS: This is not an Android question by the way.
BobPaul is offline  
Reply With Quote
Old January 29th, 2012, 10:15 AM   #3 (permalink)
Member
 
Sam Voss's Avatar
 
Join Date: Jun 2010
Location: Wisconsin
Posts: 365
 
Device(s): Droid X2
Thanks: 6
Thanked 44 Times in 43 Posts
Send a message via Skype™ to Sam Voss sam.voss@gmail.com
Default

Try putting:
Code:
Cipher cipher;
SecretKeySpec spec;
Above the onCreate method, allow its scope to greaten, then just initialize the code within the methods.
Sam Voss is offline  
Reply With Quote
Old January 29th, 2012, 10:17 AM   #4 (permalink)
Member
 
Sam Voss's Avatar
 
Join Date: Jun 2010
Location: Wisconsin
Posts: 365
 
Device(s): Droid X2
Thanks: 6
Thanked 44 Times in 43 Posts
Send a message via Skype™ to Sam Voss sam.voss@gmail.com
Default

Quote:
Originally Posted by BobPaul View Post
The variables in your try block are out of scope by the time
you get to the switch statements.

Move your switch statements into the try block OR make another block that surrounds everything and move the definition of your variable there.

I'd chose the first approach, try/catch blocks are good.

PS: This is not an Android question by the way.

Even though this is a Java question, its still an android question
Sam Voss is offline  
Reply With Quote
Old January 30th, 2012, 05:28 AM   #5 (permalink)
New Member
 
Join Date: Jan 2012
Posts: 5
 
Device(s):
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks for your replies, I'll try these and write the result here...
YellowMoon is offline  
Reply With Quote
Old January 31st, 2012, 07:50 AM   #6 (permalink)
New Member
 
Join Date: Jan 2012
Posts: 5
 
Device(s):
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks, it worked.
YellowMoon is offline  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 12:10 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo