|
1. For file name I included it with the digital signature.
2. I zipped my outputs.
3. Created a checksum.
Does this do it? About your concern of dynamic and static the only way I could think of is that each program should have a built in converter to satisfy the condition that was mentioned in your first pdf.
import java.io.*;
import java.math.*;
import java.security.*;
import java.security.interfaces.*;
import java.security.spec.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
public class SigningDocument_2
{
public static void main(String[] args) throws Exception
{
JFileChooser chooser = new JFileChooser();
JFrame frame = new JFrame();
chooser.showOpenDialog(frame);
File file = chooser.getSelectedFile();
frame.dispose();
String FileName = file.getPath();
InputStream in = new FileInputStream(FileName);
int bytesRead=0;
int bytesToRead=(int) new File(FileName).length();
byte[] input = new byte[bytesToRead];
while (bytesRead < bytesToRead)
{
int result = in.read(input, bytesRead, bytesToRead - bytesRead);
if (result == -1) break;
bytesRead += result;
}
byte [] FileNameToByte = FileName.getBytes();
byte [] DataToSign = new byte[input.length +FileNameToByte.length];
for (int i = 0; i < input.length; i++)
{
DataToSign[i] = input[i];
}
int k =0;
for (int i = input.length; i < DataToSign.length; i++)
{
DataToSign[i] = FileNameToByte[k];
k++;
}
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
keyGen.initialize(1024);
KeyPair keypair = keyGen.genKeyPair();
DSAPrivateKey privateKey = (DSAPrivateKey)keypair.getPrivate();
DSAPublicKey publicKey = (DSAPublicKey)keypair.getPublic();
DSAParams dsaParams = privateKey.getParams();
BigInteger p = dsaParams.getP();
BigInteger q = dsaParams.getQ();
BigInteger g = dsaParams.getG();
BigInteger y = publicKey.getY();
byte[] signature= createSignature(privateKey,DataToSign);
File output = new File("Bytes.txt");
PrintWriter out = new PrintWriter(output);
for (int i =0; i < input.length; i++)
{
out.println(input[i]);
}
out.println(FileName);
out.close();
File output2 = new File("Key.txt");
PrintWriter out2 = new PrintWriter(output2);
out2.println("DSA");
out2.println(p);
out2.println(q);
out2.println(g);
out2.println(y);
out2.close();
File output3 = new File("Signature.txt");
PrintWriter out3 = new PrintWriter(output3);
for (int i =0; i < signature.length; i++)
{
out3.println(signature[i]);
}
out3.close();
Zip("Bytes.txt","Key.txt","Signature.txt");
unZip("Result.zip","checksum.txt");
}
public static byte[] createSignature(PrivateKey key, byte[] buffer)
{
try
{
Signature sig = Signature.getInstance(key.getAlgorithm());
sig.initSign(key);
sig.update(buffer, 0, buffer.length);
return sig.sign();
}
catch (SignatureException e) {}
catch (InvalidKeyException e) {}
catch (NoSuchAlgorithmException e) {}
return null;
}
public static void Zip(String Bytes,String Key,String Signature)
{
String[] filenames = new String[]{Bytes,Key,Signature};
byte[] buf = new byte[1024];
try
{
String outFilename = "Result.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
for (int i=0; i < filenames.length; i++)
{
FileInputStream in = new FileInputStream(filenames[i]);
out.putNextEntry(new ZipEntry(filenames[i]));
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
CheckedInputStream cis = new CheckedInputStream(
new FileInputStream(outFilename), new Adler32());
byte[] tempBuf = new byte[1024];
while (cis.read(tempBuf) >= 0) {}
long checksum = cis.getChecksum().getValue();
PrintWriter checksumout = new PrintWriter(new File("checksum.txt"));
checksumout.write(checksum+"");
checksumout.close();
}
catch (IOException e) {}
}
public static void unZip(String zip,String checksumfile) throws NoSuchAlgorithmException, InvalidKeySpecException
{
try
{
ZipFile zf = new ZipFile(zip);
CheckedInputStream cis = new CheckedInputStream(
new FileInputStream(zip), new Adler32());
byte[] tempBuf = new byte[1024];
while (cis.read(tempBuf) >= 0) {}
long checksum = cis.getChecksum().getValue();
Scanner scan = new Scanner(new File(checksumfile));
long verifychecksum = scan.nextLong();
if (verifychecksum == checksum)
{
Enumeration<?> e = zf.entries();
while (e.hasMoreElements())
{
ZipEntry ze = (ZipEntry) e.nextElement();
FileOutputStream fout = new FileOutputStream(ze.getName());
InputStream in = zf.getInputStream(ze);
for (int c = in.read(); c != -1; c = in.read())
{
fout.write(c);
}
in.close();
fout.close();
}
ArrayList<String> Bytes = new ArrayList<String>();
Scanner scanBytes = new Scanner(new File("Bytes.txt"));
while(scanBytes.hasNext())
Bytes.add(scanBytes.nextLine());
ArrayList<String> Key = new ArrayList<String>();
Scanner scanKey = new Scanner(new File("Key.txt"));
while(scanKey.hasNext())
Key.add(scanKey.nextLine());
String algorithm = Key.get(0);
BigInteger p = new BigInteger(Key.get(1));
BigInteger q = new BigInteger(Key.get(2));
BigInteger g = new BigInteger(Key.get(3));
BigInteger y = new BigInteger(Key.get(4));
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
ArrayList<String> Signature = new ArrayList<String>();
Scanner scanSignature = new Scanner(new File("Signature.txt"));
while(scanSignature.hasNext())
Signature.add(scanSignature.nextLine());
byte [] sig = new byte[Signature.size()];
for (int i =0; i < sig.length;i++)
{
sig[i] = new Byte(Signature.get(i));
}
byte [] bytes = new byte[Bytes.size()];
for (int i =0; i < bytes.length-1;i++)
{
bytes[i] = new Byte(Bytes.get(i));
}
String filename = Bytes.get(Bytes.size()-1);
byte [] FileNameToByte = filename.getBytes();
byte [] DataToSign = new byte[bytes.length +FileNameToByte.length-1];
for (int i = 0; i < bytes.length-1; i++)
{
DataToSign[i] = bytes[i];
}
int k =0;
for (int i = bytes.length-1; i < DataToSign.length; i++)
{
DataToSign[i] = FileNameToByte[k];
k++;
}
boolean bool = verifySignature(publicKey,DataToSign,sig);
if(bool)
{
BuildFile(filename,bytes);
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + filename);
}
else
JOptionPane.showMessageDialog(null,"Warning", "Warning Message",JOptionPane.WARNING_MESSAGE);
}
}
catch (IOException e) {}
}
public static boolean verifySignature(PublicKey key, byte[] buffer, byte[] signature)
{
try
{
Signature sig = Signature.getInstance(key.getAlgorithm());
sig.initVerify(key);
sig.update(buffer, 0, buffer.length);
return sig.verify(signature);
}
catch (SignatureException e) {}
catch (InvalidKeyException e) {}
catch (NoSuchAlgorithmException e) {}
return false;
}
public static void BuildFile(String FileName,byte[] bytes) throws IOException
{
FileOutputStream m = new FileOutputStream(FileName);
m.write(bytes);
m.close();
}
}
modified on Saturday, September 5, 2009 10:15 PM
|
|
|
|
|
public class duplicate {
int temp, true_count;
int ar[] = { 1, 2, 3, 4, 1 };
int count;
public void find() {
for (int i = 0; i < ar.length - 1; i++) {
temp = ar[i];
for (int j = i; j <= ar.length-1 ; j++) {
if (temp == ar[j]) {
count += 1;
}
}
}
}
public static void main(String args[]) {
duplicate dup = new duplicate();
dup.find();
System.out.println("The No of duplicates is :" + dup.count);
}
}
This program is supposed to count the no. of duplicates in thearray and print the same. There is something wrong in the loop. PLs tell me what is wrong with the code.
Thanks
|
|
|
|
|
Have you tried stepping through the code? Even with pencil you will see very quickly:
for (int i = 0; i < ar.length - 1; i++)
Elements of the array are 0..4 and the length is 5. At what value for i is the loop exited?
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
Time to do some homework
- Computers (and i assume all being aside humans) do intent to count from 0!
- Make sure you're not finding the number itself. (for example: do not compare the first digit to itself!).
- step out of the loop if you've found what you were looking for.
greets
Torsten
I never finish anyth...
|
|
|
|
|
In other words change your for loops to
1. for (int i = 0; i < ar.length; i++)
2. for (int j = i+1; j < ar.length; j++)
Regards
|
|
|
|
|
I want that in textbox if 0 is insert than it shows an alert message. The textbox insert numbers only. How will I validate it? please help
thanx in advance.
|
|
|
|
|
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Validate extends JFrame
{
TextField t;
public Validate()
{
t= new TextField(10);
add(t);
t.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if ((key == KeyEvent.VK_0))
{
JOptionPane.showMessageDialog(null,"0 Was Clicked", "Note",JOptionPane.INFORMATION_MESSAGE);
}
else
if((key == KeyEvent.VK_1)||(key == KeyEvent.VK_2)||(key == KeyEvent.VK_3)||(key == KeyEvent.VK_4)||(key == KeyEvent.VK_5)||(key == KeyEvent.VK_6||(key == KeyEvent.VK_7)||(key == KeyEvent.VK_8)||(key == KeyEvent.VK_9)))
{
return;
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Input", "Note",JOptionPane.WARNING_MESSAGE);
String s = t.getText();
t.setText(s.replaceAll(e.getKeyChar()+"",""));
}
}
}
);
setTitle("Validate");
setLocationRelativeTo(null);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
new Validate();
}
}
|
|
|
|
|
Would it not be simpler to use a JFormattedTextField to ensure numeric input and an InputVerifier to check the value is not 0?
|
|
|
|
|
True there are many ways to do it.
Snippet 1: Integers
public Validate()
{
Text.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();
if (!(Character.isDigit(c) ||(c == KeyEvent.VK_BACK_SPACE) ||(c == KeyEvent.VK_DELETE)))
{
getToolkit().beep();
e.consume();
}
if( c == KeyEvent.VK_0)
{
Label.setText("0 entered");
Label.setForeground(Color.red);
}
else
{
Label.setText("ok");
Label.setForeground(Color.black);
}
}
});
Snippet 2: Double
public class Validate()
{
DoubleValidate dv = new DoubleValidate();
public Validate()
{
Text.setDocument(dv);
add(Text);
}
static class DoubleValidate extends PlainDocument
{
public void insertString(int offset,String string, AttributeSet attributes)throws BadLocationException
{
if (string == null)
{
return;
}
else
{
String newValue;
int length = getLength();
if (length == 0)
{
newValue = string;
}
else
{
String currentContent = getText(0, length);
StringBuffer currentBuffer = new StringBuffer(currentContent);
currentBuffer.insert(offset, string);
newValue = currentBuffer.toString();
}
try
{
Double.parseDouble(newValue);
super.insertString(offset, string,attributes);
}
catch (NumberFormatException exception)
{
Toolkit.getDefaultToolkit().beep();
}
}
}
}
}
|
|
|
|
|
Yes, there are many ways to make something more complicated than it needs to be.
|
|
|
|
|
I m trying to Run a blackbery Application on simulator using Jdk,jre and Eclipse The project but When i run Build.xml as Run as Ant then get some building errors like
Buildfile: E:\Current\BlackBerry Projects\4th\mfunds-java\build\build.xml
[typedef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
buildAndPackageTarget:
[typedef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
clean:
[delete] Deleting directory E:\Current\BlackBerry Projects\4th\mfunds-java\preprocessed\${alias}
[delete] Deleting directory E:\Current\BlackBerry Projects\4th\mfunds-java\classes\${alias}
[echo] Target ${alias} was cleaned
[typedef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
preprocess:
[mkdir] Created dir: E:\Current\BlackBerry Projects\4th\mfunds-java\preprocessed\${alias}
[wtkpreprocess] **************************************************************
[wtkpreprocess] * Antenna 1.0.1 initialized for project "telSPACE Java" *
[wtkpreprocess] * Using Sun Wireless Toolkit 2.5 (CLDC-1.0; MIDP-2.0) *
[wtkpreprocess] **************************************************************
[wtkpreprocess] Preprocessing 40 file(s) at E:\Current\BlackBerry Projects\4th\mfunds-java\src
[echo] Pre-processed successfuly with: ${platform}, ${platformver}, ${alias}, ${alias2}
[typedef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.
compile:
[mkdir] Created dir: E:\Current\BlackBerry Projects\4th\mfunds-java\classes\${alias}
BUILD FAILED
E:\Current\BlackBerry Projects\4th\mfunds-java\build\build.xml:334: The following error occurred while executing this line:
E:\Current\BlackBerry Projects\4th\mfunds-java\build\build.xml:377: Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
Total time: 18 seconds
please suggest some thing
|
|
|
|
|
Ok,
The problem lies in...
E:\Current\BlackBerry Projects\4th\mfunds-java\build\build.xml:377: Problem: failed to create task or type if
That error tells me you that...
Cause: The name is undefined.
and you should...
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any / declarations have taken place.
See? All that and all I did was read what your compiler shat out...
|
|
|
|
|
Please give me project in Java my platform is netbeans 5.5. or tell me some websites where i got projects. I want to try to make my project please help me.
thanx in advance.
|
|
|
|
|
You want someone to do what ? Send you a project ?
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
I write a code for automatic ID generated. But If somebody deleted the Id for example I have three ID i.e. 001,002, 003. If somebody deleted 002 so when we add the id is generated 004 but I want the ID is generated 002. Please tell me
thanx in advance.
|
|
|
|
|
http://doc.ddart.net/mssql/sql70/dbcc_5.htm
Read about this
Extra resource
http://www.h2database.com/html/main.html This contains SQL grammar for most queries.
Regards
|
|
|
|
|
hello every1!!
i m making my final year project on video watermarking using SVD transform.
i wanted to know how to compute SVD of an image using JAMA...
Can u plz explain the sysntax for it..
Ankita..< >
|
|
|
|
|
http://andrewtrusty.com/math2605/2605project4.html
http://edbardsley.org/classes/15-211/lab4/lossy.html
These links will help
Regards
|
|
|
|
|
|
Let me know the features of jdk 1.6 and 1.7
and also latest versions of servelt/jsp
|
|
|
|
|
http://java.sun.com/javase/6/docs/
http://java.sun.com/javase/7/docs/index.html
Read These.
Regards
|
|
|
|
|
Okay, I have included my code below. I am supposed to have the user input a html file name (i got that part down) and then i'm supposed to look at the file and make two lists of words, one of words that are italicized between <i> </i> and one of words that are bold between <b> </b>. So I need to know how to do this. I think I should use a regular expression? Am I rite? regular expression looking for the <b> and </b> tag and creating anything in between as a string that I add to a string container. then i print the string container as output. is this okay? can you help me with the regular expression?
<pre>package final_project_2;
import java.io.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
try{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the name of the file: ");
String line = input.readLine();
FileInputStream in = null;
in = new FileInputStream(line);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "IOException: " + e.getMessage());
}
}
}</pre>
|
|
|
|
|
import java.io.File;
import java.util.*;
public class RegularExpression
{
public static void main(String[] args) throws Exception
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter HTML file Name:");
File HtmlFile = new File(scan.next());
Scanner scan2 = new Scanner(HtmlFile);
int FirstIndex =0,LastIndex =0;
ArrayList<String> Bold = new ArrayList<String>();
ArrayList<String> Italics = new ArrayList<String>();
while(scan2.hasNext())
{
String q = scan2.nextLine();
for (int i =0; i <q.length(); i++)
{
if (i+3<=q.length())
{
if (q.substring(i, i+3).equalsIgnoreCase("<b>"))
{
FirstIndex = i+3;
for (int j = FirstIndex; j <q.length(); j++)
{
if (j+4<=q.length())
{
if (q.substring(j, j+4).equalsIgnoreCase("</b>"))
{
LastIndex = j;
Bold.add(q.substring(FirstIndex,LastIndex));
break;
}
}
}
}
else
if (q.substring(i, i+3).equalsIgnoreCase("<i>"))
{
for (int j = FirstIndex; j <q.length(); j++)
{
if (j+4<=q.length())
{
if (q.substring(j, j+4).equalsIgnoreCase("</i>"))
{
LastIndex = j;
Italics.add(q.substring(FirstIndex,LastIndex));
break;
}
}
}
}
}
}
}
System.out.println("Bold:");
for (int i =0; i <Bold.size(); i++)
{
System.out.println(Bold.get(i));
}
System.out.println("Italics:");
for (int i =0; i <Bold.size(); i++)
{
System.out.println(Italics.get(i));
}
}
}
|
|
|
|
|
i need Java codings of Vernam Cipher Algorithm.can anybody help me plz.
|
|
|
|
|
Before all the cross-posting, did you even bother to read this or here?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|