can plz solve this
Iam doing voice encryption for a voice mail application.
Iam sending the voice bytes received from microphone and encrypting with AES (iam not able to include bouncycastle provider...so iam doing without any provider).
Iam getting exception ..FinalBlocknotPaddedProperly
Is there ll be any problem with the size of the bytes that i sent to decryption method(in receiver client)
Because the data to be sent is a stream ....so when 1 send 5000 bytes of ciphertext to another client can i operate on 1200 bytes at a time at other end for dcryption?
waiting for the reply....plz
hello ghstark .....
iam adding code here ...this is to encrypt and decrypt the voice mails
Iam reading the voice bytes and encrypting them and sending them
At the other side iam decrypting and sending to speaker...
Iam pasting the three classes here ..key generation(common key -AES),and Encryption and Decryption....
Its working fine when i did in single system...but not in cllient-server voice sys....i think there was a problem with no. of bytes i send and receive...will it be a problem?
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class Aesx{
public Key getKey() throws Exception
{
/** AES key size of 128 bits **/
byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
Key key = new SecretKeySpec(keyBytes, "AES");
return key;
}
// encryption pass
public byte[] encrypt(byte[] plaintext,Key key) throws Exception
{
Cipher cipher = Cipher.getInstance("AES");
try{
cipher.init(Cipher.ENCRYPT_MODE,key);
}
catch(Exception e)
{
e.printStackTrace();
}
byte[] cipherText = new byte[cipher.getOutputSize(plaintext.length)];
int ctLength = cipher.update(plaintext, 0, plaintext.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
return cipherText;
}
// decryption pass
public byte[] decrypt(byte[] cipherText,Key key) throws Exception
{
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] plainText = new byte[cipher.getOutputSize(cipherText.length)];
int ptLength = cipher.update(cipherText, 0, cipherText.length, plainText, 0);
ptLength += cipher.doFinal(plainText, ptLength);
return plainText;
}
}
And iam getting the EXCEPTION as like this....
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA6275)
at javax.crypto.Cipher.doFinal(DashoA12275)
at other.Aesx.decrypt(Aesx.java:59)
at audio.Speaker.run(Speaker.java:202)
Speaker.run(): javax.crypto.BadPaddingException: Given final block not properly
Eagerly waiting for ur reply.....b bye ..thanks in advance
Your decrypt method needs exactly the same bytes as that were returned by the encrypt method. In other words, you cannot encrypt 500 bytes using one call of encrypt (on the sending side) and then decrypt (on the receiving side) 150 bytes with one decrypt call and then 350 bytes with a second decrypt call. To be able to encrypt and decrypt in pieces you must not call doFinal().
What you want is byte-oriented streaming. Therefore, I think the most natural solution is to use CipherInputStream and CipherOutputStream with the RC4 algorithm, i.e. Cipher rc4 = Cipher.getInstance("RC4/ECB/NoPadding");
You must also generate a random salt and and hash this into the key. This salt must be transmitted in the clear at the beginning of the cipher. You should also MAC the data.
Basically, you need to provide all the functionality that is already provide by SSL/TLS. Of course, I would recommend that you simply use SSL/TLS as available in the Sun JSSE
Thanks Mr.ghstark....
I tried by removing the doFinal method in the decryption method.
Now its working fine.My remaining part is adding message authentication.
The problem is i have to show such that ...
Suppose A is sending voice to C...B should capture the voice from A and add/delete/modify it and send it to C.
C should find out that the message not as correct as send fromA.
The problem is how to capture the voice and send it back with modification?
Plz if you know anything related to it u can help me
Expecting ur help ...thank you once again
b bye have a nice day