newbie doubt in encryption

V

vaneric

I am a beginner to JCE. I have been trying out some tutorials on the
topic of encryption using the Cipher class.

I wrote the following code,

byte[] initvector=new byte[]{ 0x00, 0x00, 0x00, 0x01, 0x04, 0x05,
0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
KeyGenerator kgen = KeyGenerator.getInstance("AES", "BC");
generator.init(192);
Key encKey = kgen.generateKey();
cipher.init(Cipher.ENCRYPT_MODE, encKey,
new IvParameterSpec(initvector));

Why do I have to create a decryption key like
Key decKey = new SecretKeySpec(
encKey.getEncoded(), encKey.getAlgorithm());

Can't I use the encKey itself to do the decoding? Cipher only expects
an object implementing Key interface as the second argument.Why do I
have to create a SecretKeySpec object?

Any help most welcome.
Thanks in advance,
eric
 
R

RedGrittyBrick

vaneric said:
I am a beginner to JCE. I have been trying out some tutorials on the
topic of encryption using the Cipher class.

Why do I have to create a decryption key like

Can't I use the encKey itself to do the decoding?

I have a feeling you have not fully grasped the difference between
* secret key cryptography (symmetric)
* public key cryptography (asymmetric) e.g. AES

In secret key cryptography, the encryption key *can* be used to decrypt
ciphertext.

In public key cryptography, it is absolutely a fundamental feature that
the encryption key *cannot* be used as a decryption key.

To allow someone to send you a secret message, you can broadcast your
public key to the world, anyone can use your public key to encrypt a
message to you, no-one else can read that message even if they have the
public key.

http://en.wikipedia.org/wiki/Public_key
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top