File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Security and the fly likes Java AES/CBC/PKCS7Padding missing one part of decrypt code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "Java AES/CBC/PKCS7Padding missing one part of decrypt code" Watch "Java AES/CBC/PKCS7Padding missing one part of decrypt code" New topic
Author

Java AES/CBC/PKCS7Padding missing one part of decrypt code

Antonino Spitale
Greenhorn

Joined: Aug 04, 2012
Posts: 2
Im new of Cryptography and BouncyCastle and i want to encrypt/decrypt with AES/CBC/PKCS7Padding ; maybe Encrypt is ok , but when ill decrypt the first part of sms3 is missing . somebody can help me? Thanks

String sms3 = "allora i Saiyan atterrarono sulla Terra e Napa disse";
byte iv2 [] = new byte [16] ;
random.nextBytes(iv2);
IvParameterSpec spec2 = new IvParameterSpec (iv2) ;
cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, key,spec2 ) ;
boolean finish = false ;
int i = 0 ;
byte [] cText3 = new byte [cipher.getOutputSize(sms3.getBytes().length)];
//int cLenght2 = cipher.update(iv, 0, iv.length, cText3,0);
int cLenght2 = 0 ;
System.out.println("size of sms3" + " " + cText3.length);
while (finish == false)
{
if ((sms3.getBytes().length)- i < 16 ) // last block
{
cLenght2 += cipher.doFinal(cText3, cLenght2);
finish = true ;
}
else
{
cLenght2 = cipher.update(sms3.getBytes(), 0, sms3.getBytes().length, cText3, 0);
}
}

// DECRYPT MODE
cipher.init(Cipher.DECRYPT_MODE, key,spec2 ) ;
byte [] plain4 = new byte [cipher.getOutputSize(cLenght2)];
int buf = cipher.update(cText3, 0,cLenght2,plain4,0);
buf += cipher.doFinal(plain4, buf);
byte [] fin = new byte [buf - spec2.getIV().length];
System.arraycopy (plain4 ,spec2.getIV().length ,fin ,0, fin.length);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java AES/CBC/PKCS7Padding missing one part of decrypt code