ユキトンにお願いするページ

Last-modified: 2008-01-27 (日) 21:53:03

エラーを消してくれ!!

import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class nameCryptM {

   private SecretKeySpec myKey;
   private Cipher cipher;
   nameCryptM(String keyword)
	throws NoSuchAlgorithmException, NoSuchPaddingException {
	byte[] byteKey = {
	    (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
	    (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08
	};
	byte[] tmpKey = keyword.getBytes();
	for (int i = 0; i < tmpKey.length && i < byteKey.length; i++) {
	    byteKey[i] = tmpKey[i];
	}
	myKey = new SecretKeySpec(byteKey, "DES");
	cipher = Cipher.getInstance("DES");
   }
   public static String main(String mod,String pass) {
		String[] argv = {mod,pass};
		if (argv.length > 0 && argv[0].equals("D") && argv.length != 2) {
			System.out.println("usage: java nameCryptM D key");
			System.exit(0);
		} else if (argv.length != 2 && argv.length != 3) {
			System.out.print("usage: java nameCryptM mode key [word]\n\n" +
			     "       key:  keyword (8 chars)\n" +
			     "       mode: E or D\n" +
			     "        \"E\" for encription\n" +
			     "        \"D\" for decription\n" +
			     "       word: you want to encrypt it\n");
			System.exit(0);
		}
	int i = 1;
	//int ii = 1;
	String Line;
	//String[] Name;
	//String[] EnFile;
	//Name = new String(char[ii]);
	//File = new String(char[ii]);
	try {
	    nameCryptM crypt = new nameCryptM(argv[1]);
	    if (argv[0].equals("E")) {
		FileOutputStream out = new FileOutputStream("secret");
		crypt.encrypt(argv[2], out);
		out.close();
		System.out.println("Encrypted word is in the \"secret\" file.");
	    } else if (argv[0].equals("D")) {
			File file = new File("encryptFile"+i);
			while (file.exists()||file.isDirectory()) {
				System.out.println("存在しました。");
				System.out.println("encryptFile"+i);
				//EnFile = "encryptFile"+i;
				//ConstII enFile = "encryptFile"+i;
				FileInputStream in = new FileInputStream("./"+"encryptFile"+i+"\\secret");
				System.out.println("暗号化されてるファイル名:" + crypt.decrypt(in));
				Line = crypt.decrypt(in);
				in.close();
				//Name = Line;
				//ConstI enname = Line;
				i++;
				//ii++;
				file = new File("encryptFile"+i);
			}
	    } else {
		System.err.println("Sorry, it's unsupported mode.");
		System.exit(1);
	    }
	} catch (Exception e) {
	    System.err.println(e);
	    System.exit(1);
	}
	 return String.toString(Line);
   }
   public void encrypt(String plane, OutputStream out)
	throws InvalidKeyException, IllegalBlockSizeException, IOException,
	       BadPaddingException {
	cipher.init(Cipher.ENCRYPT_MODE, myKey);
	byte[] secret = cipher.doFinal(plane.getBytes());
	out.write(secret);
   }
   public String decrypt(InputStream in)
	throws InvalidKeyException, IllegalBlockSizeException, IOException,
	       BadPaddingException {
	cipher.init(Cipher.DECRYPT_MODE, myKey);
	byte[] secret = new byte[in.available()];
	in.read(secret);
	byte[] plane = cipher.doFinal(secret);
	return new String(plane);
   }

}