Java如何读取文件?
源文件如下,小弟没有学过Java,下面是一段JAVA用RSA加密字符串的程序,
命令行的形式是Java PublicExample ABCDEFG
哪位大哥能把它改成,读取一个公钥,加密一个txt文件,并指定输出私钥路径,及加密后txt的路径
如命令行的形式是Java PublicExample c:\未加密.txt c:\公钥.txt c:\生成私钥.txt c:\生成加密.txt
/*
Public Key cryptography using the RSA algorithm.
*/
import java.security.*;
import javax.crypto.*;
import org.bouncycastle.jce.provider.JCERSACipher;
import java.io.*;
import java.io.FileReader;
import java.io.File;
import java.io.FileWriter;
public class PublicExample {
public static void main (String[] args) throws Exception {
//
// Check args and get plaintext
if (args.length !=1) {
System.err.println( "Usage: java PublicExample text ");
System.exit(1);
}
byte[] plainText = args[0].getBytes( "UTF8 ");
//
// Generate an RSA key
System.out.println( "\nStart generating RSA key " );
KeyPairGenerator keyGen = KeyPairGenerator.getInstance( "RSA ", new org.bouncycastle.jce.provider.BouncyCastleProvider());
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();
System.out.println( "Finish generating RSA key " );
//
// Creates an RSA Cipher object (specifying the algorithm, mode, and padding).
//Cipher cipher = Cipher.getInstance( "RSA/ECB/PKCS1Padding ");
Cipher cipher = Cipher.getInstance( "RSA ", new org.bouncycastle.jce.provider.BouncyCastleProvider());
//
// Print the provider information
System.out.println( "\n " + cipher.getProvider().getInfo() );
System.out.println( "\nStart encryption " );
//
// Initializes the Cipher object.
cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
//
// Encrypt the plaintext using the public key
byte[] cipherText = cipher.doFinal(plainText);
System.out.println( "Finish encryption: " );
System.out.println( new String(cipherText, "UTF8 ") );
File file;
FileWriter fw;
/*
try {
File dir = new File(dirName);
dir.mkdirs();
} catch (Exception e) {
throw new Error( "unable to create directory " + dirName +
"\n " + e.toString());
}
*/
try
{
FileOutputStream fout = new FileOutputStream( "c:\\\\encryption.txt ");
fout.write(cipherText);
fout.close();
} catch (Exception e) {
throw new Error( "error writing to file \n " + e.toString());
}
try
{
FileOutputStream fkout = new FileOutputStream( "c:\\\\encryptionkey.dat ");
fkout.write(key.getPrivate().getEncoded());
fkout.flush();
fkout.close();
} catch (Exception e) {
throw new Error( "error writing to file \n " + e.toString());
}
//fw.flush();
//fw.close();
System.out.println( "\nStart decryption " );
//
// Initializes the Cipher object.
cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
//
// Decrypt the ciphertext using the private key
byte[] newPlainText = cipher.doFinal(cipherText);
System.out.println( "Finish decryption: " );
System.out.println( new String(newPlainText, "UTF8 ") );
//File filer;
//FileReader fr;
char buffer[];
buffer=new char[70];
byte[] decontent;
try {
//filer = new File( "c:\\ ", "encryption.txt ");
//fr = FileReader(filer);
FileInputStream fis = new FileInputStream( "c:\\\\encryption.txt ");
//byte[] encodedPublickey = new byte[fis.available()];
decontent = new byte[fis.available()];
fis.read(decontent);
fis.close();
//fw = new FileWriter(filer);
} catch (Exception e) {
throw new Error( "unable to create file\n " + e.toString());
}
cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
//
// Decrypt the ciphertext using the private key
String tt = new String(buffer);
//byte[] decontent = tt.getBytes( "UTF8 ");
//byte[] decontent = bos.getBytes();
byte[] newPlainText1 = cipher.doFinal(decontent);
System.out.println( "Finish decryption1: " );
System.out.println( new String(newPlainText1) );
}
}
[解决办法]
xuexi...
[解决办法]
没看你的代码,因为加密不懂,而且无关
能加密字符串,就应该能加密文件,无非示用FileInputStream读取,FileOutputStream写入,中间的算发不变,传进去字符串
FileInputStream fin = new FileInputStream(new File(args[0]));
FileOutputStream fout= new FileOutStream(new File(args[1]));
[解决办法]
这挺简单的,只不过你没有现成的用了,就是一个读文件的操作
=========
找两素数p和q
取n=p*q
取t=(p-1)*(q-1)
取任何一个数e,要求满足e <t并且e与t互素(就是最大公因数为1)
取d*e%t==1
这样最终得到三个数: n d e
设消息为数M (M <n)
设c=(M**d)%n就得到了加密后的消息c
设m=(c**e)%n则 m == M,从而完成对c的解密。
注:**表示次方,上面两式中的d和e可以互换。
[解决办法]
加密文件实际就是加密数据流, Oracle应该可以支持流加密的. 退一万步来说, 如果文件不大, 干脆读入流到字符串即可
[解决办法]
文件读都差不多吧,
[解决办法]
加密是对数据加减一个KEY(BYTE),当然不可能这么简单,下面一个例子希望对你有帮助,是对CLASS加密的,文件也一样
/**
@version 1.21 2004-09-11
@author Cay Horstmann
*/
import java.util.*;
import java.io.*;
import java.lang.reflect.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
This program demonstrates a custom class loader that decrypts
class files.
*/
public class ClassLoaderTest
{
public static void main(String[] args)
{
JFrame frame = new ClassLoaderFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
This frame contains two text fields for the name of the class
to load and the decryption key.
*/
class ClassLoaderFrame extends JFrame
{
public ClassLoaderFrame()
{
setTitle( "ClassLoaderTest ");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLayout(new GridBagLayout());
add(new JLabel( "Class "), new GBC(0, 0).setAnchor(GBC.EAST));
add(nameField, new GBC(1, 0).setWeight(100, 0).setAnchor(GBC.WEST));
add(new JLabel( "Key "), new GBC(0, 1).setAnchor(GBC.EAST));
add(keyField, new GBC(1, 1).setWeight(100, 0).setAnchor(GBC.WEST));
JButton loadButton = new JButton( "Load ");
add(loadButton, new GBC(0, 2, 2, 1));
loadButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
runClass(nameField.getText(), keyField.getText());
}
});
pack();
}
/**
Runs the main method of a given class.
@param name the class name
@param key the decryption key for the class files
*/
public void runClass(String name, String key)
{
try
{
ClassLoader loader = new CryptoClassLoader(Integer.parseInt(key));
Class c = loader.loadClass(name);
String[] args = new String[] {};
Method m = c.getMethod( "main ", args.getClass());
m.invoke(null, (Object) args);
}
catch (Throwable e)
{
JOptionPane.showMessageDialog(this, e);
}
}
private JTextField keyField = new JTextField( "3 ", 4);
private JTextField nameField = new JTextField(30);
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
}
/**
This class loader loads encrypted class files.
*/
class CryptoClassLoader extends ClassLoader
{
/**
Constructs a crypto class loader.
@param k the decryption key
*/
public CryptoClassLoader(int k)
{
key = k;
}
protected Class findClass(String name)
throws ClassNotFoundException
{
byte[] classBytes = null;
try
{
classBytes = loadClassBytes(name);
}
catch (IOException e)
{
throw new ClassNotFoundException(name);
}
Class cl = defineClass(name, classBytes, 0, classBytes.length);
if (cl == null)
throw new ClassNotFoundException(name);
return cl;
}
/**
Loads and decrypt the class file bytes.
@param name the class name
@return an array with the class file bytes
*/
private byte[] loadClassBytes(String name)
throws IOException
{
String cname = name.replace( '. ', '/ ') + ".caesar ";
FileInputStream in = null;
in = new FileInputStream(cname);
try
{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -1)
{
byte b = (byte) (ch - key);
buffer.write(b);
}
in.close();
return buffer.toByteArray();
}
finally
{
in.close();
}
}
private int key;
}
[解决办法]
是的。
上面的都给写了。
[解决办法]
RSA加密每次的长度是受限制的,需要把byte数组先分段
[解决办法]
^