We had to built a encrypted/decrypted voice chat through UDP. Chat is working without encryption but when I add AES code to encrypt, i hear very loud noise which is continuous periodic beep signal but at the same time I also hear decrypted conversations which is fine. I need to eliminate this noise.
We will be so grateful for your help. Thank you
Sending
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.sound.sampled.*;
public class MicPlayer {
private static final String IP_TO_STREAM_TO = "localhost" ;
private static final int PORT_TO_STREAM_TO = 1234 ;
/** Creates a new instance of MicPlayer */
public MicPlayer() {
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Mixer.Info minfo[] = AudioSystem.getMixerInfo() ;
for( int i = 0 ; i < minfo.length ; i++ )
{
System.out.println( minfo[i] ) ;
}
if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) {
try {
DataLine.Info dataLineInfo = new DataLine.Info( TargetDataLine.class , getAudioFormat() ) ;
TargetDataLine targetDataLine = (TargetDataLine)AudioSystem.getLine( dataLineInfo ) ;
targetDataLine.open( getAudioFormat() );
targetDataLine.start();
byte tempBuffer[] = new byte[8192] ;
while( true )
{
targetDataLine.read( tempBuffer , 0 , tempBuffer.length );
byte[] encrypt = AES.encrypt(tempBuffer);
sendThruUDP(encrypt) ;
}
}
catch(Exception e )
{
System.out.println(" not correct " ) ;
System.exit(0) ;
}
}
}
public static AudioFormat getAudioFormat(){
float sampleRate = 8000.0F;
//8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
//8,16
int channels = 1;
//1,2
boolean signed = true;
//true,false
boolean bigEndian = false;
//true,false
return new AudioFormat( sampleRate, sampleSizeInBits, channels, signed, bigEndian );
}
public static void sendThruUDP( byte soundpacket[] )
{
try
{
DatagramSocket sock = new DatagramSocket() ;
sock.send( new DatagramPacket( soundpacket , soundpacket.length , InetAddress.getByName( IP_TO_STREAM_TO ) , PORT_TO_STREAM_TO ) ) ;
sock.close() ;
}
catch( Exception e )
{
e.printStackTrace() ;
System.out.println(" Unable to send soundpacket using UDP " ) ;
}
}
}
Receiving
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class RadioReceiver extends Thread {
private static final String IP_TO_STREAM_TO = "localhost" ;
private static final int PORT_TO_STREAM_TO = 1234;
/** Creates a new instance of RadioReceiver */
public RadioReceiver() {
}
public void run()
{
byte b[] = null ;
while( true )
{
b = receiveThruUDP() ;
toSpeaker( b ) ;
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
RadioReceiver r = new RadioReceiver() ;
r.start() ;
}
public static byte[] receiveThruUDP()
{
try
{
DatagramSocket sock = new DatagramSocket(PORT_TO_STREAM_TO) ;
byte soundpacket[] = new byte[8192] ;
DatagramPacket datagram = new DatagramPacket( soundpacket , soundpacket.length , InetAddress.getByName( IP_TO_STREAM_TO ) , PORT_TO_STREAM_TO ) ;
sock.receive( datagram ) ;
sock.close() ;
return AES.decrypt(datagram.getData()); // soundpacket ;
}
catch( Exception e )
{
System.out.println(" Unable to send soundpacket using UDP " ) ;
return null ;
}
}
public static void toSpeaker( byte soundbytes[] )
{
try{
DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class , getAudioFormat() ) ;
SourceDataLine sourceDataLine = (SourceDataLine)AudioSystem.getLine( dataLineInfo );
sourceDataLine.open( getAudioFormat() ) ;
sourceDataLine.start();
sourceDataLine.write( soundbytes , 0, soundbytes.length );
sourceDataLine.drain() ;
sourceDataLine.close() ;
}
catch(Exception e )
{
System.out.println("not working in speakers " ) ;
}
}
public static AudioFormat getAudioFormat()
{
float sampleRate = 44100.0F;
//8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
//8,16
int channels = 1;
//1,2
boolean signed = true;
//true,false
boolean bigEndian = false;
//true,false
return new AudioFormat( sampleRate, sampleSizeInBits, channels, signed, bigEndian );
}
}
AES
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AES {
static String IV = "AAAAAAAAAAAAAAAA";
static String encryptionKey = "0123456789abcdef";
public static byte[] encrypt(byte[] inputcum) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
return cipher.doFinal(inputcum);
}
public static byte[] decrypt(byte[] cipherSound) throws Exception{
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
return cipher.doFinal(cipherSound);
}
}
The problem is not related to UDP or encryption. Each call to TargetDataLine.read(byte[]) fills only a part of array with remaining part being filled with leftovers from the previous calls, but you are encrypting and sending the whole array each time.
A call to TargetDataLine.read(byte[]) behaves similar to InputStream.read(byte[]) - it returns the actual number of bytes transferred into the byte array. This value must not be ignored.
For the minimum working process the code should be modified along the following guidelines:
When sending:
while( true ) {
int read = targetDataLine.read( tempBuffer , 0 , tempBuffer.length );
byte[] encrypt = AES.encrypt(tempBuffer, 0, read);
sendThruUDP(encrypt) ;
}
When encrypting (notice that the padding is changed to PKCS5Padding to allow for input length which is not a multiple of the AES block size):
public static byte[] encrypt(byte[] plainData, int offset, int length) throws Exception
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
return cipher.doFinal(plainData, offset, length);
}
The decrypt() method should be modified to use the same padding.
Other most obvious improvements:
Generate new random IV for each data block and send a packet that contains both the IV and the encrypted data. It will require some copying between a number of byte arrays both on the sending+encrypting and receiving+decrypting side, but reusing the same IV for multiple cipher operations on the same key is absolutely unsafe from the cryptography point of view.
Use the proper key derivation function (search for PBKDF2) instead of simply converting a password string to bytes.
Get an instance of Cipher once and then just reinitialize it with the key and a new IV. That will save a bit of CPU and memory.
#Oleg Estekhin said "The problem is not related to UDP or encryption. You are ignoring the return value of targetDataLine.read( tempBuffer , 0 , tempBuffer.length ). I am pretty sure that most of the time it reads less than the buffer size, with remaining data being garbage from previous calls"
I agree and use a CTR or GCM as a mode of operation not CBC because CBC is slower.
Related
So this was part of an assignment I was working (though I already had to turn it in unsolved) based on this challenge at http://www.loyalty.org/~schoen/rsa/.
In summary, the challenge provides a list of files contain cipher and an associated public key. Using the fact that RSA public keys often repeat primes, we are expected to find as many of the private keys from the 100 as possible and decrypt the associated message. Although I think forming the private key was straightforward (and believe I am doing it correctly), but I get error "javax.crypto.BadPaddingException: Message is larger than modulus". Can someone tell me what I am doing wrong? I'm including my code along with 2 of the bin and cipher files as an example.
My Code:
package decryptrsa;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.io.File;
import java.util.Scanner;
import java.security.spec.X509EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.KeyFactory;
import java.security.interfaces.*;
import java.math.BigInteger;
import java.util.ArrayList;
import javax.crypto.Cipher;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
*
* #author qscot
*/
public class DecryptRSA {
static ArrayList<BigInteger> publicKeys = new ArrayList<BigInteger>();
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
ReadPublicKeys();
crackPrivateKeys();
}
static void ReadPublicKeys() throws Exception {
String publicKey;
for (int i = 1; i <= 100; i++) {
publicKey = "";
Scanner scanner = new Scanner(new File("C:\\Users\\qscot\\Downloads\\challenge\\" + i + ".pem"));
scanner.nextLine();
String lineString = scanner.nextLine();
do {
publicKey += lineString;
lineString = scanner.nextLine();
} while (lineString.contains("END PUBLIC KEY") == false);
scanner.close();
byte[] decoded = Base64.decode(publicKey.getBytes());
X509EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKey pk = (RSAPublicKey) kf.generatePublic(spec);
BigInteger modulus = pk.getModulus();
publicKeys.add(modulus);
}
}
static void crackPrivateKeys() throws Exception {
BigInteger gcd;
BigInteger q1;
BigInteger q2;
for (int i = 0; i < 100; i++) {
for (int z = 0; z < 100; z++) {
if (i != z) {
gcd = (gcd(publicKeys.get(i), publicKeys.get(z)));
if (!gcd.equals(BigInteger.ONE)) {
q1 = publicKeys.get(i).divide(gcd);
q2 = publicKeys.get(z).divide(gcd);
RSAPrivateKey key1 = getKey(gcd, q1);
RSAPrivateKey key2 = getKey(gcd, q2);
writeDecryptedFile(i, key1);
writeDecryptedFile(z, key2);
}
}
}
}
}
static void writeDecryptedFile(int fileNo, RSAPrivateKey privKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
File file = new File("C:\\Users\\qscot\\Downloads\\challenge\\" + fileNo + ".bin");
FileInputStream fis = new FileInputStream(file);
byte[] fbytes = new byte[(int) file.length()];
fis.read(fbytes);
fis.close();
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] oBytes = cipher.doFinal(fbytes);
FileOutputStream fos = new FileOutputStream("C:\\Users\\qscot\\Downloads\\challenge\\" + fileNo + "-OUTPUT.txt");
fos.write(oBytes);
fos.flush();
fos.close();
}
static BigInteger gcd(BigInteger num1, BigInteger num2) {
while (!num1.equals(BigInteger.ONE) && !num1.equals(BigInteger.ZERO) && !num2.equals(BigInteger.ONE) && !num2.equals(BigInteger.ZERO)) {
if (num1.compareTo(num2) == 1) {
num1 = num1.mod(num2);
} else {
num2 = num2.mod(num1);
}
}
if (num1.equals(BigInteger.ONE) || num2.equals(BigInteger.ONE)) {
return BigInteger.valueOf(1);
} else {
if (num1.equals(BigInteger.ZERO)) {
return num2;
} else {
return num1;
}
}
}
static RSAPrivateKey getKey(BigInteger p, BigInteger q) throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
BigInteger t, d, e;
e = BigInteger.valueOf(65537);
t = (p.subtract(BigInteger.ONE)).multiply((q.subtract(BigInteger.ONE)));
d = e.modInverse(t);
RSAPrivateKeySpec keyspec = new RSAPrivateKeySpec(d, e);
return (RSAPrivateKey) kf.generatePrivate(keyspec);
}
}
6.pem file:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDn5BqEUDrlhvwXQ68MqZ001B74
fGD1w2Le++wILzEX7Ba8LeJVeuwpOxxdxDQ7280yc0SKPiChWpb3bE1/G/hV5e++
95qfhbe+SP7MRL39TxEotADaqyHY6SfloDk5A9NiIzgebWmtFriamBfhrxzx8G3K
6NWAAjDAIMx+xjLn6QIDAQAB
-----END PUBLIC KEY-----
6.bin File:
xƒâTD§¼çÄ ØˆßPã…Ôä3x4b2Ð#•—æ¨
U«õ`Êzÿúw"Ü°™è0ÄÕ~³•—˜§FºqŠ„hÏŒÞõ&د³Ô<*pàbGÃGìMÿö¶3Ùù¸²Z•a¯®éDNïæÝjn¢¯tå!WÐ
6-OUTPUT.txt:
8.pem File:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1fGe+mMJYZ+BDm76Ag25bXcBB
pABkGZpnQpnSTocEuCQyp5/lNNVFdF0QliNRULnxoK+pD8VEBqxR+zkYsYf9iGzU
PzOELWvbFgIJdUPixlaD3/1Y6+eSDffCOsCoJ1A/8DELMbjQdbFoxfqj9AVRU3cd
R0AauL4O9hPz0N9OVQIDAQAB
-----END PUBLIC KEY-----
8.bin File:
¤”»BÃ."
îÂT.<(bø×]¥”£Ó¯!›==±Ñ·;ª%7¿ðU#xÀÉ5ç£
‡*h\w¸¸#¦aܳj Ù~t´õêæSü®Î ŒQU¼L-â-äK}\-žù‹ý«>DÕ£ñ”Õe6Œ"
G®lI
The line RSAPrivateKeySpec keyspec = new RSAPrivateKeySpec(d, e); contains a bug. The arguments to RSAPrivateKeySpec should be, in order, the modulus and the decrypt exponent. According the documentation in the link you provided the padding used is PKCS1PADDING. Therefore you should obtain a Cipher instance using Cipher.getInstance("RSA/ECB/PKCS1PADDING");
There is also an off-by-1 bug in your writeDecryptedFile method. The files are named 1.bin ... 100.bin, but your keys are indexed from 0 through 99. Thus you need to be accessing (fileNo + 1) + ".bin" rather than fileNo + ".bin"
There is no reason to assume the encrypt exponent is always 65537 since this information is available in the RSAPublicKey. Also, do not use the Base64 codec you are using, instead use the one from java.util.Base64.
Your method of reading in the bytes from the cipher *.bin files is unreliable because it assumes a read always returns the number of bytes requested. It will probably work out that way with current implementations but that isn't guaranteed by the javadocs. You should investigate the very simple Files.readAllBytes() method and related methods for reading in small files. They greatly simplify your code.
Hi I have a client using Triple DES. I know I know if AES I would not have a problem it is a older solution integration. I have code below that Takes in a file and writes to another. On the decrypt method I don't understand the 2nd parameter for length of the file. Please help. The error I get is the following:
Exception in thread "main" org.bouncycastle.crypto.DataLengthException: last block incomplete in decryption
at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal(Unknown Source)
at DESede_BC.decrypt(DESede_BC.java:102)
at DESede_BC.main(DESede_BC.java:120)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.ShortBufferException;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.DESedeEngine;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
public class DESede_BC {
PaddedBufferedBlockCipher encryptCipher;
PaddedBufferedBlockCipher decryptCipher;
// Buffers used to transport the bytes from one stream to another
byte[] buf = new byte[8]; //input buffer - block size length
byte[] obuf = new byte[557]; //output buffer
byte[] key = null; //the key
public DESede_BC(){
//use a default 192 bit key
key = "thekey".getBytes();
InitCiphers();
}
public DESede_BC(byte[] keyBytes){
key = new byte[keyBytes.length];
System.arraycopy(keyBytes, 0 , key, 0, keyBytes.length);
InitCiphers();
}
private void InitCiphers(){
encryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());
encryptCipher.init(true, new KeyParameter(key));
decryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());
decryptCipher.init(false, new KeyParameter(key));
}
public void ResetCiphers() {
if(encryptCipher!=null)
encryptCipher.reset();
if(decryptCipher!=null)
decryptCipher.reset();
}
public void encrypt(InputStream in, long length, OutputStream out)
throws ShortBufferException,
IllegalBlockSizeException,
BadPaddingException,
DataLengthException,
IllegalStateException,
InvalidCipherTextException
{
try {
// Bytes written to out will be encrypted
// Read in the cleartext bytes from in InputStream and
// write them encrypted to out OutputStream
int noBytesRead = 0; //number of bytes read from input
int noBytesProcessed = 0; //number of bytes processed
while ((noBytesRead = in.read(buf)) >= 0) {
noBytesProcessed =
encryptCipher.processBytes(buf, 0, noBytesRead, obuf, 0);
out.write(obuf, 0, noBytesProcessed);
}
noBytesProcessed =
encryptCipher.doFinal(obuf, 0);
out.write(obuf, 0, noBytesProcessed);
out.flush();
}
catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
}
public void decrypt(InputStream in,long length, OutputStream out)
throws ShortBufferException, IllegalBlockSizeException, BadPaddingException,
DataLengthException, IllegalStateException, InvalidCipherTextException
{
try {
// Bytes read from in will be decrypted
// Read in the decrypted bytes from in InputStream and and
// write them in cleartext to out OutputStream
int noBytesRead = 0; //number of bytes read from input
int noBytesProcessed = 0; //number of bytes processed
while ((noBytesRead = in.read(buf)) >= 0) {
noBytesProcessed = decryptCipher.processBytes(buf, 0, noBytesRead, obuf, 0);
out.write(obuf, 0, noBytesProcessed);
}
noBytesProcessed = decryptCipher.doFinal(obuf, 0);
out.write(obuf, 0, noBytesProcessed);
out.flush();
}
catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String... args)
throws Exception
{
DESede_BC d = new DESede_BC();
FileInputStream fis2 = new FileInputStream("c:\\2.in");
FileOutputStream fos2 = new FileOutputStream("c:\\decrypted.txt");
d.decrypt(fis2, new Long("128"), fos2);
}
}
I think you need to decode your input into Base64 before doFinal:
byte[] obuf = Base64.decode(obuf, Base64.NO_OPTIONS);
byte[] decValue = c.doFinal(obuf);
I'm trying to stream microphone over UDP but my output is so noisy, it's not able to understand the input audio. Here is my code:
Server Side:
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.Port;
import javax.sound.sampled.TargetDataLine;
public class MicPlayer {
private static final String IP_TO_STREAM_TO = "localhost" ;
private static final int PORT_TO_STREAM_TO = 8888 ;
/** Creates a new instance of MicPlayer */
public MicPlayer() {
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Mixer.Info minfo[] = AudioSystem.getMixerInfo() ;
for( int i = 0 ; i < minfo.length ; i++ )
{
System.out.println( minfo[i] ) ;
}
if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) {
try {
DataLine.Info dataLineInfo = new DataLine.Info(
TargetDataLine.class , getAudioFormat() ) ;
final TargetDataLine targetDataLine = (TargetDataLine)AudioSystem.getLine( dataLineInfo ) ;
targetDataLine.open( getAudioFormat() );
targetDataLine.start();
byte tempBuffer[] = new byte[targetDataLine.getBufferSize() / 5] ;
int cnt = 0 ;
while( true )
{
targetDataLine.read( tempBuffer , 0 , tempBuffer.length );
sendThruUDP( tempBuffer ) ;
}
}
catch(Exception e )
{
System.out.println(" not correct " ) ;
System.exit(0) ;
}
}
}
public static AudioFormat getAudioFormat(){
float sampleRate = 8000.0F;
//8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
//8,16
int channels = 1;
//1,2
boolean signed = true;
//true,false
boolean bigEndian = false;
//true,false
return new AudioFormat( sampleRate, sampleSizeInBits, channels, signed, bigEndian );
}
public static void sendThruUDP( byte soundpacket[] )
{
try
{
DatagramSocket sock = new DatagramSocket() ;
sock.send( new DatagramPacket( soundpacket , soundpacket.length , InetAddress.getByName( IP_TO_STREAM_TO ) , PORT_TO_STREAM_TO ) ) ;
sock.close() ;
}
catch( Exception e )
{
e.printStackTrace() ;
System.out.println(" Unable to send soundpacket using UDP " ) ;
}
}
}
I don't think client-side has problems but here is the code;
Client Side:
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class RadioReceiver extends Thread {
private static final String IP_TO_STREAM_TO = "localhost" ;
private static final int PORT_TO_STREAM_TO = 8888 ;
/** Creates a new instance of RadioReceiver */
public RadioReceiver() {
}
public void run()
{
byte b[] = null ;
while( true )
{
b = receiveThruUDP() ;
toSpeaker( b ) ;
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
RadioReceiver r = new RadioReceiver() ;
r.start() ;
}
public static byte[] receiveThruUDP()
{
try
{
DatagramSocket sock = new DatagramSocket(PORT_TO_STREAM_TO) ;
byte soundpacket[] = new byte[1230] ;
DatagramPacket datagram = new DatagramPacket( soundpacket , soundpacket.length , InetAddress.getByName( IP_TO_STREAM_TO ) , PORT_TO_STREAM_TO ) ;
sock.receive( datagram ) ;
sock.close() ; return datagram.getData() ; // soundpacket ;
}
catch( Exception e )
{
System.out.println(" Unable to send soundpacket using UDP " ) ;
return null ;
}
}
public static void toSpeaker( byte soundbytes[] )
{
try{
DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class , getAudioFormat() ) ;
SourceDataLine sourceDataLine = (SourceDataLine)AudioSystem.getLine( dataLineInfo );
sourceDataLine.open( getAudioFormat() ) ;
sourceDataLine.start();
int cnt = 0;
sourceDataLine.write( soundbytes , 0, soundbytes.length );
sourceDataLine.drain() ;
sourceDataLine.close() ;
}
catch(Exception e )
{
System.out.println("not working in speakers " ) ;
}
}
public static AudioFormat getAudioFormat()
{
float sampleRate = 8000.0F;
//8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
//8,16
int channels = 1;
//1,2
boolean signed = true;
//true,false
boolean bigEndian = false;
//true,false
return new AudioFormat( sampleRate, sampleSizeInBits, channels, signed, bigEndian );
}
}
I'm sure that my connection is OK but i don't have any idea why my output is so noisy. It's getting me crazy i'm working on it till 1 week, please help me. Thank You.
The reason is probably that your datagram packets are too small, which causes you to send a whole bunch of packages that creates a lot of overhead. This might result in a huge packet loss rate and make them arrive in the wrong order.
So, make your buffer size bigger:
byte tempBuffer[] = new byte[8192] ;
And this comes from the DatagramSocket.receive() JavaDoc:
This method blocks until a datagram is received. The length field of the datagram packet object contains the length of the received message. If the message is longer than the packet's length, the message is truncated.
This might be a problem as well. Try to use the same size for both sending and receiving packets.
byte soundpacket[] = new byte[8192];
Also, do not continiously open and close the AudioLine to the speakers. Do also not continuously create DatagramSockets. Create one, and keep it.
I appear to be getting corruption when reading an ObjectInputStream. The attached snippet throws an exception prior to completion. I fixed the example to call oos.writeObject( p1 ) as suggested.
The Exception stack is as follows:
java.lang.OutOfMemoryError: Java heap space
at test.POJO.readExternal(STest.java:82)
at java.io.ObjectInputStream.readExternalData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at test.STest.test(STest.java:37)
I believe this OutOfMemoryError exception to be misleading. I added a print statement showing the readExternal(..) behavior and am seeing a large value being pulled from ObjectInputStream, this does not correlate to what was written. If DIM is set to 5 it works if set to 15 I get the above exception. If I lower the number of bytes written per array element I get more successful iterations.
package test;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import org.junit.Test;
public class STest
{
#Test
public void test() throws Exception
{
POJO p1 = new POJO();
POJO p2 = new POJO();
// Initialize and serialize POJO 1
// --------------------------------
p1.hydrate();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( p1 );
oos.flush();
oos.close();
byte [] baSerialized = baos.toByteArray();
// Parse POJO 2
// -------------
ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream( baSerialized ) );
p2 = (POJO)ois.readObject();
// Test Result
// ------------
byte [][] baa1 = p1._baa;
byte [][] baa2 = p2._baa;
for ( int i=0; i < baa1.length; i++ )
{
String str1 = new String( baa1[ i ] );
String str2 = new String( baa2[ i ] );
assertTrue( str1.equals( str2 ) );
}
}
}
class POJO implements Externalizable
{
protected static final int DIM = 5;
protected byte [][] _baa = null;
public POJO()
{
}
public void hydrate()
{
_baa = new byte[ DIM ][];
for ( int i = 0; i < _baa.length; i++ )
{
_baa[ i ] = ("This is a serialize and parse test, it will be interesting to see if it completes without exception, I suspect not as there appears be a bug in the JRE - " + i).getBytes();
}
}
#Override
public void readExternal( ObjectInput oi ) throws IOException, ClassNotFoundException
{
int iDim = oi.readInt();
_baa = new byte[ iDim ][];
for ( int i=0; i < iDim; i++ )
{
int iSize = oi.readInt();
System.out.println( iSize );
byte [] ba = new byte[ iSize ];
oi.read( ba );
_baa[ i ] = ba;
}
}
#Override
public void writeExternal( ObjectOutput oo ) throws IOException
{
oo.writeInt( _baa.length );
for ( int i=0; i < _baa.length; i++ )
{
oo.writeInt( _baa[ i ].length );
oo.write( _baa[ i ] );
}
}
}
p1.writeExternal(o);
That should be:
oo.writeObject(p1);
You aren't supposed to call your own writeExternal() method directly. The ObjectOutputStream does that.
I wanted to know how to encrypt a file or a photo which will eventually uploaded into the dropbox.
As i have research online and only manage to found this code (pasted at the bottom) which only encrypt the password, but I wanted to know how to encrypt a file or a photo which will eventually uploaded into the dropbox instead.
So is there any reference or help or guide on how to write a java programming ( will be using on Eclipse software ) on encrypting a file using triple DES? Thank you so much.
package com.kushal.utils;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DESEncryption {
private static final String UNICODE_FORMAT = "UTF8";
public static final String DES_ENCRYPTION_SCHEME = "DES";
private KeySpec myKeySpec;
private SecretKeyFactory mySecretKeyFactory;
private Cipher cipher;
byte[] keyAsBytes;
private String myEncryptionKey;
private String myEncryptionScheme;
SecretKey key;
public DESEncryption() throws Exception
{
myEncryptionKey = "ThisIsSecretEncryptionKey";
myEncryptionScheme = DES_ENCRYPTION_SCHEME;
keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
myKeySpec = new DESKeySpec(keyAsBytes);
mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
cipher = Cipher.getInstance(myEncryptionScheme);
key = mySecretKeyFactory.generateSecret(myKeySpec);
}
/**
* Method To Encrypt The String
*/
public String encrypt(String unencryptedString) {
String encryptedString = null;
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
byte[] encryptedText = cipher.doFinal(plainText);
BASE64Encoder base64encoder = new BASE64Encoder();
encryptedString = base64encoder.encode(encryptedText);
} catch (Exception e) {
e.printStackTrace();
}
return encryptedString;
}
/**
* Method To Decrypt An Ecrypted String
*/
public String decrypt(String encryptedString) {
String decryptedText=null;
try {
cipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] encryptedText = base64decoder.decodeBuffer(encryptedString);
byte[] plainText = cipher.doFinal(encryptedText);
decryptedText= bytes2String(plainText);
} catch (Exception e) {
e.printStackTrace();
}
return decryptedText;
}
/**
* Returns String From An Array Of Bytes
*/
private static String bytes2String(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
stringBuffer.append((char) bytes[i]);
}
return stringBuffer.toString();
}
/**
* Testing the DES Encryption And Decryption Technique
*/
public static void main(String args []) throws Exception
{
DESEncryption myEncryptor= new DESEncryption();
String stringToEncrypt="Sanjaal.com";
String encrypted=myEncryptor.encrypt(stringToEncrypt);
String decrypted=myEncryptor.decrypt(encrypted);
System.out.println("String To Encrypt: "+stringToEncrypt);
System.out.println("Encrypted Value :" + encrypted);
System.out.println("Decrypted Value :"+decrypted);
}
}
File reading is taken from here
public static byte[] encryptFile(String pFilePath, byte[] pKey) throws GeneralSecurityException, IOException
{
File file = new File(pFilePath);
long length = file.length();
InputStream is = new FileInputStream(file);
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Close the input stream and return bytes
is.close();
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
SecretKeyFactory lDESedeKeyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey kA = lDESedeKeyFactory.generateSecret(new DESedeKeySpec(pKey));
IvParameterSpec lIVSpec = new IvParameterSpec(new byte[8]);
Cipher desedeCBCCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
desedeCBCCipher.init(Cipher.ENCRYPT_MODE, kA, lIVSpec);
byte[] encrypted = desedeCBCCipher.doFinal(bytes);
return encrypted;
}
A file can probably be read as a string of binary digits (lots of 1's and 0's). You could read the file, then encrypt the string using DES the same way you would a password, outputting the result to a new file.
Decryption would work the same way, reading the encrypted file, decrypting, and outputting to an unencrypted file.