• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can a global variable get data from a method ?  RSS feed

 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can a global variable (key) get data from a method ?

Class A

JCE

CipherFactoryImpl

Error
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PatienceIsAVirtue

I see no global variable.

You also haven't answered my queries in your last three topics.
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:PatienceIsAVirtue
I see no global variable.
Is private Key key, inside JCE class sir.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Joe wrote:
Stephan van Hulst wrote:
I see no global variable.
Is private Key key, inside JCE class sir.


How can a private variable be global? It can't be accessed outside of the class where it is defined.

Henry
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me how to fix that ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Joe wrote:Can you tell me how to fix that ?


Fix what? I don't even understand your question.  What do you mean by a variable getting anything from a method? What are you trying to do?

Henry
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is in your constructor:

However, key is not initialised at this point, and so is null.
'key' is only given a value in the encryptCBC method, which will have no effect on the (null) value passed into the CipherFactory.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, how in the world are you going to decrypt the encrypted message if you don't know the cipher the message was encrypted with, and if you didn't append the IV to the ciphertext?

Please don't tell me you've hardcoded a fixed IV. You might as well not use any encryption at all.
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:'key' is only given a value in the encryptCBC method, which will have no effect on the (null) value passed into the CipherFactory.

Is there any method I can get the value from the method to CipherFactory?
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:John, how in the world are you going to decrypt the encrypted message if you don't know the cipher the message was encrypted with, and if you didn't append the IV to the ciphertext?
Please don't tell me you've hardcoded a fixed IV. You might as well not use any encryption at all.

I tried to minimize my code so I not posting them. I just want to get rid of the null pointer error only.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not my point. Your application is completely insecure. Do you know how block ciphers work, and why they need an IV? If not, I strongly suggest you read some articles about encryption before you continue coding.
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noted,with thanks. But is there a way I can pass the variable from method to constructor?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Joe wrote:Noted,with thanks. But is there a way I can pass the variable from method to constructor?


No, because the constructor (by its very nature) has to have run before any method can be called on the object.

All you could do is pass the key into the constructor...but please take note of what Stephan is saying.
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to do that ? Yup, I will take note of it.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
No, because the constructor (by its very nature) has to have run before any method can be called on the object.

All you could do is pass the key into the constructor.


As a possible alternative, the constructor can delay instantiation of the factory... meaning if the key isn't available, then don't instantiate the factory.

However, with this alternative, it means that the methods will need to check if the key and/or factory exists. If the key doesn't exist, then error out. And if the factory doesn't exist, but the key does, then instantiate the factory.

Henry
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I fix that ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Joe wrote:How can I fix that ?


Dave and I gave you two alternate solutions... and Stephan gave you some suggestions as well. So, which are you talking/asking about? And also, what is it about the solutions/suggests that you don't understand?

Henry
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to pass the key from method to constructor....
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind, got it. Thanks
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!