помогите найти ошибки(((
Код:
//GSig.java
/* Генерация DSA-подписи */
import java.io.*;
import java.security.*;
class GSig
{
//сохранение байтового массива в файл
public static void saveToFile (byte[] info,
String filename)
{
try
{
FileOutputStream fos = new FileOutputStream
(filename);
fos.write(info);
fos.close();
}
catch (Exception e)
{
System.err.println(“Caught exception “ + e.toString());
}
}// saveToFile ()
public static void main(String args[])
{
try
{
/* Генерация ключей */
KeyPairGenerator keyGen =
KeyPairGenerator.getInstance(“DSA”, “SUN”);
SecureRandom random =
SecureRandom.getInstance(“SHA1PRNG”, “SUN”);
keyGen.initialize(1024, random);
KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
/* Создание объекта класса Signature */
Signature dsa =
Signature.getInstance(“SHA1withDSA”, “SUN”);
/* Инициализация частным ключом */
dsa.initSign(priv);
/* Чтение данных из файла “data”. Вызов метода update() */
FileInputStream fis = new FileInputStream(“data”);
BufferedInputStream bufin =
new BufferedInputStream(fis);
byte[] buffer = new byte[1024];
int len;
while (bufin.available() != 0)
{
len = bufin.read(buffer);
dsa.update(buffer, 0, len);
}
bufin.close();
/* Генерация подписи */
byte[] realSig = dsa.sign();
/* Сохранение подписи в файл “signature” */
saveToFile (realSig,”signature”);
/* Сохранение открытого ключа в файл “pubkey” */
byte[] key = pub.getEncoded();
saveToFile (key,”pubkey”);
}
catch (Exception e)
{
System.err.println(“Caught exception “ + e.toString());
}
}// main()
}// class GSig
Код:
/* Верификация DSA-подписи */
import java.io.*;
import java.security.*;
import java.security.spec.*;
class VSig
{
//чтение из файла в байтовый массив
public static byte[] readFromFile (String fileName)
{
byte[] info;
try
{
FileInputStream fis =
new FileInputStream(fileName);
info = new byte[fis.available()];
fis.read(info);
fis.close();
}
catch (Exception e)
{
System.err.println(“Caught exception “ + e.toString());
info = new byte[0];
}
return(info);
}// copyFromFile ()
public static void main(String args[])
{
try
{
/* Получение encoded public key из файла “pubkey” */
byte[] encKey = readFromFile(“pubkey”);
/* Создание спецификации ключа */
X509EncodedKeySpec pubKeySpec =
new X509EncodedKeySpec(encKey);
/* Создание объектов Лунафсещкн и ЗгидшсЛун*/
KeyFactory keyFactory = KeyFactory.getInstance (“DSA”, “SUN”);
PublicKey pubKey = keyFactory.generatePublic
(pubKeySpec);
/* Чтение подписи из файла “signature” */
byte[] sigToVerify = readFromFile(“signature”);
/* Создание объекта класса Signature и инициализация с помощью открытого ключа */
Signature sig = Signature.getInstance (“SHA1withDSA”, “SUN”);
sig.initVerify(pubKey);
/* Чтение данных из файла “data” и вызов метода update() */
FileInputStream datafis = new FileInputStream (“data”);
BufferedInputStream bufin =
new BufferedInputStream(datafis);
byte[] buffer = new byte[1024];
int len;
while (bufin.available() != 0)
{
len = bufin.read(buffer);
sig.update(buffer, 0, len);
}
bufin.close();
/* Верификация */
boolean verifies = sig.verify(sigToVerify);
System.out.println(“Signature verifies: “ + verifies);
}
catch (Exception e)
{
System.err.println(“Caught exception “ + e.toString());
}
}// main()
}// class VSig