Amin Raykou

Greenhorn
+ Follow
since Apr 17, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Amin Raykou

Thanks Nicholas for your help.
It was so usefull.
I'll bring my lunch and try to solve the problem
13 years ago
Thanks for your help Pat.
I understand that toString() isn't the solution to get the data from the output stream.
Now I'm in a lost!!!
How could I get the data from the output stream (of the encryption void) to store it into the database?
And how could I extract it to the input stream of the decryption void?
Can you please help me with a sample of code.
Thanks a lot for your help.
13 years ago
Good morning;
Thanks for your replys.
To Pat:

The result of a cipher is binary. It is not a string. You have to convert it to something that is a string, hex, mime, radix64, or something.


The void toString() doesn't convert the output to a string?
I tryed to convert from output to string then from string to base64 encoding but it doesn't resolve the problem.
A sample code to how to convert the cipher output to a string or hexa ascii (or how to use blob on oracle 10g) will be apreciated.Thanks

To Nicholas: I know it's a shame to do such mistakes but I'm a biginner with java and cryptography, and it's just a study project.

Code ( should ) insert(s) encrypted data in a byte buffer with the
void encipherString() then make the byte buffer insert(able) in (?) database.

how can i insert the byte buffer, should i just convert it to string ?
13 years ago
Thanks for your reply.
When I tested the code with a string (without databse), it work :it encrypt the string and then it decrypt the cipher text back.
But when I want to insert the string into the database and then retrieve it and decipher, I don't get the wanted result for the first column.The other columns are perfect(decrypted to the right data).So the key and vector test run correctly for all the columns but not for the first one.
I'll try to use AES.
Thanks for your help.
13 years ago
Good evening,
I'm developping an application to encrypt data with tripleDES before getting inserted in oracle database.
Another application must consult the database, decrypt the data then show it in a jtable.
I'm a biginner in the security field, i found a source implementing triple DES encryption.
1-some byte from the encrypted data like the cote(') make some problem when executing the query, i'd like to change the type of the encryption bytes, i heard about UTF8 but i don't know how to use it.
2-when I decrypt the data from the resultset and put it in the jtable the first culumn is empty and I receive this error:
"javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher. "
It can't decrypt the first object of the resultset cause of the missing byte.
Here is the code:
The encryption class:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;


public class TripleDES {
public File f = new File("clef");

/** Read a TripleDES secret key from the specified file */
public SecretKey readKey(File f)
throws IOException, NoSuchAlgorithmException,
InvalidKeyException, InvalidKeySpecException
{
// Read the raw bytes from the keyfile
DataInputStream in = new DataInputStream(new FileInputStream(f));
byte[] rawkey = new byte[(int)f.length()];
in.readFully(rawkey);
in.close();

// Convert the raw bytes to a secret key like this
DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
SecretKey key = keyfactory.generateSecret(keyspec);
return key;
}

public void encrypt(SecretKey key, InputStream in, OutputStream out)
throws NoSuchAlgorithmException, InvalidKeyException,
NoSuchPaddingException, IOException
{
// Create and initialize the encryption engine
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
// Create a special output stream to do the work for us
CipherOutputStream cos = new CipherOutputStream(out, cipher);

// Read from the input and write to the encrypting output stream
byte[] buffer = new byte[2048];
int bytesRead;
while((bytesRead = in.read(buffer)) != -1) {
cos.write(buffer, 0, bytesRead);
}
cos.close();

// For extra security, don't leave any plaintext hanging around memory.
java.util.Arrays.fill(buffer, (byte) 0);
}

public void decrypt(SecretKey key, InputStream in, OutputStream out)
throws NoSuchAlgorithmException, InvalidKeyException, IOException,
IllegalBlockSizeException, NoSuchPaddingException,
BadPaddingException
{
// Create and initialize the decryption engine
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, key);

// Read bytes, decrypt, and write them out.
byte[] buffer = new byte[2048];
int bytesRead;
while((bytesRead = in.read(buffer)) != -1) {
out.write(cipher.update(buffer, 0, bytesRead));
}

// Write out the final bunch of decrypted bytes
out.write(cipher.doFinal());
out.flush();
}
//manipulate a string and not streams when enrypting
public String crypterString(String s){

ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());

try {
encrypt(readKey(f), in, out);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return out.toString();
}
//manipulate a string and not streams when decrypting
public String decrypterString(String s){

ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
try {
decrypt(readKey(f), in, out);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return out.toString();
}
}
I insert encrypt the data with the void crypterString() then i insert it in the database.
And here is the vector i use to fill in the jtable(the decryption application side)

public Vector donnee(ResultSet rs)
{int numRows=0;
int numColumns = 0;
TripleDES td = new TripleDES();
try {
td.readKey(td.f);
} catch (InvalidKeyException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvalidKeySpecException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Vector data = new Vector();
try{
numColumns=rs.getMetaData().getColumnCount();
while (rs.next())

{ Vector rowData = new Vector();
for (int i = 0; i < numColumns; i++)
rowData.addElement(td.decrypterString(rs.getObject(i+1).toString()));
data.addElement(rowData);
numRows++;
}

}

catch (java.sql.SQLException e)
{
JOptionPane.showMessageDialog(null,"Erreur de connexion � la base. Veuillez contacter votre administrateur syst�me","Connexion",JOptionPane.WARNING_MESSAGE);

}return data;
}

Thanks for your help.
13 years ago
Good evening,
I'm developping an application to encrypt data with tripleDES before getting inserted in oracle database.
Another application must consult the database, decrypt the data then show it in a jtable.
I'm a biginner in the security field, so i found some problems.
1-some byte from the encrypted data like the cote(') make some problem when executing the query, i'd like to change the type of the encryption bytes, i heard about UTF8 but i don't know how to use it.
2-when I decrypt the data from the resultset and put it in the jtable the first culumn is empty and I receive this error:
"javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher. "
It can't decrypt the first object of the resultset cause of the missing byte.
Here is the code:
The encryption class:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;


public class TripleDES {
public File f = new File("clef");

/** Read a TripleDES secret key from the specified file */
public SecretKey readKey(File f)
throws IOException, NoSuchAlgorithmException,
InvalidKeyException, InvalidKeySpecException
{
// Read the raw bytes from the keyfile
DataInputStream in = new DataInputStream(new FileInputStream(f));
byte[] rawkey = new byte[(int)f.length()];
in.readFully(rawkey);
in.close();

// Convert the raw bytes to a secret key like this
DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
SecretKey key = keyfactory.generateSecret(keyspec);
return key;
}

public void encrypt(SecretKey key, InputStream in, OutputStream out)
throws NoSuchAlgorithmException, InvalidKeyException,
NoSuchPaddingException, IOException
{
// Create and initialize the encryption engine
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
// Create a special output stream to do the work for us
CipherOutputStream cos = new CipherOutputStream(out, cipher);

// Read from the input and write to the encrypting output stream
byte[] buffer = new byte[2048];
int bytesRead;
while((bytesRead = in.read(buffer)) != -1) {
cos.write(buffer, 0, bytesRead);
}
cos.close();

// For extra security, don't leave any plaintext hanging around memory.
java.util.Arrays.fill(buffer, (byte) 0);
}

public void decrypt(SecretKey key, InputStream in, OutputStream out)
throws NoSuchAlgorithmException, InvalidKeyException, IOException,
IllegalBlockSizeException, NoSuchPaddingException,
BadPaddingException
{
// Create and initialize the decryption engine
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, key);

// Read bytes, decrypt, and write them out.
byte[] buffer = new byte[2048];
int bytesRead;
while((bytesRead = in.read(buffer)) != -1) {
out.write(cipher.update(buffer, 0, bytesRead));
}

// Write out the final bunch of decrypted bytes
out.write(cipher.doFinal());
out.flush();
}
//manipulate a string and not streams when enrypting
public String crypterString(String s){

ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());

try {
encrypt(readKey(f), in, out);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return out.toString();
}
//manipulate a string and not streams when decrypting
public String decrypterString(String s){

ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
try {
decrypt(readKey(f), in, out);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return out.toString();
}
}
I insert encrypt the data with the void crypterString() then i insert it in the database.
And here is the vector i use to fill in the jtable(the decryption application side)

public Vector donnee(ResultSet rs)
{int numRows=0;
int numColumns = 0;
TripleDES td = new TripleDES();
try {
td.readKey(td.f);
} catch (InvalidKeyException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvalidKeySpecException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Vector data = new Vector();
try{
numColumns=rs.getMetaData().getColumnCount();
while (rs.next())

{ Vector rowData = new Vector();
for (int i = 0; i < numColumns; i++)
rowData.addElement(td.decrypterString(rs.getObject(i+1).toString()));
data.addElement(rowData);
numRows++;
}

}

catch (java.sql.SQLException e)
{
JOptionPane.showMessageDialog(null,"Erreur de connexion � la base. Veuillez contacter votre administrateur syst�me","Connexion",JOptionPane.WARNING_MESSAGE);

}return data;
}

Thanks for your help.
13 years ago
In fact I tryed to use the SwingUtilities.invokeLater() before posting the topic but there was the same problem.Now the new thread, is the best solution I found.
Thanks for your help
13 years ago
Thanks Guido for your help.
In fact it was a threading/synchro problem.
I wrote a class extending thread, and in the void run() I call the refresh method.Then I start the thread in the action performed, it works !!!
13 years ago
Thanks a lot for your reply Guido.
The refresh method is called only in the action performed of the jbutton.And when I do that and launch the application, as soon as I clik on the button it stops running and block, the problem is that if I put this method in the main it run correctly.Here is a simple code I hope it make it clair

First code: the search method in the main:

public class test extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private static JTable jTable = null;
private JButton jButton = null;
private static String req="select * from emp";

/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(0, 80, 637, 84));
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}

/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
jTable.setSize(new Dimension(581, 260));
}
return jTable;
}

/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(99, 20, 91, 26));
jButton.setText("Rechercher");



}
return jButton;
}

//the method to search and refresh the jtable

public static void rechercher(){
try{

Dbmanager d = new Dbmanager(); //Dbmanager is a class in witch I put the methods needed for connection and getting query...

d.connect("database", "user", "password");
MonTableModel m =new MonTableModel(d.ColNames(d.getQuery(req)),d.donnee(d.getQuery(req))); //MonTableModel is the class extending AbstractTableModel

jTable.setModel(m);
while(true){
m.majDonnees(d.donnee(d.getQuery(req)));


try {
Thread.sleep(2000);
}catch(InterruptedException ie){}
}
}catch(Exception et){et.printStackTrace();}
}

public static void main(String[] args) {
new test();
rechercher(); // here I call the method of search and refresh


}

/**
* This is the default constructor
*/
public test() {
super();
initialize();

}


private void initialize() {
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.setVisible(true);

}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
}
This code run perfectly, but I need to call the search in an action performed to get the search options and give the user the oppertunity to run the search (so to disable the search at the run of the application), so I modifyed the code:

public class test extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JTable jTable = null;
private static JButton jButton = null;
private static String req="select * from emp";

* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(0, 80, 637, 84));
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}

/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
jTable.setSize(new Dimension(581, 260));
}
return jTable;
}

/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(99, 20, 91, 26));
jButton.setText("Rechercher");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
req="select ename from emp" ;
rechercher();} //here I add the search method in the action performed
});



}
return jButton;
}


public void rechercher(){
try{

Dbmanager d = new Dbmanager();
d.connect("base", "user", "password");
MonTableModel m =new MonTableModel(d.ColNames(d.getQuery(req)),d.donnee(d.getQuery(req)));
jTable.setModel(m);
while(true){
m.majDonnees(d.donnee(d.getQuery(req)));


try {
Thread.sleep(2000);
}catch(InterruptedException ie){}
}
}catch(Exception et){et.printStackTrace();}
}

public static void main(String[] args) {
new test(); // I delete the call of the search method from the main
}

/**
* This is the default constructor
*/
public test() {
super();
initialize();

}


private void initialize() {
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.setVisible(true);


}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}



}
The problem is that with that code the application bloks as soon as I press the button, and I don't get error message, it seems like the "while(true)" bloc is running without showing nothing in the table.

please help!!!
13 years ago
Good evening,
I developp a frame with a jtable to consult an oracle database using an AbstractTableModel.
The jtable must be refreshed to show the new records added to the database.
I developped a void to search the records and refresh the result every 2 seconds.
This void run correctly if it's called in the void main.
I want to integrate this void in an action performed of a jbutton to put the search under control(to specify the options of the search and change the query if needed),but doing this make the application bloked without error message.
I don't understand why the void run correctly under the void main and doesn't in an action performed.
thanks
13 years ago