Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Learning how to encrypt and decrypt, need help!

800144Sep 24 2010 — edited Oct 12 2010
I am learning to encrypt and decrypt at the minute and I have put this small piece of code together from tutorials and info on the net:

---
String bytes = toHex("Some 16 bit Key");
Key skeySpec = new SecretKeySpec(toByte(bytes), "AES");
Cipher c = Cipher.getInstance("AES/CFB8/NoPadding");
	    				
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(keyFile));
oos.writeObject(skeySpec);
	    				
c.init(Cipher.ENCRYPT_MODE, skeySpec);
CipherOutputStream cos = new CipherOutputStream(new FileOutputStream(testFile), c);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(cos));
pw.println("Stand and unfold yourself");
pw.close();
oos.writeObject(c.getIV());
oos.close();
	    			    
c.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(toByte(bytes)));
CipherInputStream cis = new CipherInputStream(new FileInputStream(testFile), c);
BufferedReader br = new BufferedReader(new InputStreamReader(cis));
Log.d("XXXX", br.readLine());
---

The log outputs the following:

---

*09-24 17:05:30.929: DEBUG/(15018): �А�L���x$ yourself*

---

So it looks like it is encoding it all and then decoding only the last part?

Can any one explain why it would be doing that?

Edited by: Draffodx on 28-Sep-2010 04:21
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 9 2010
Added on Sep 24 2010
10 comments
1,443 views