引入import sun.misc.BASE64Decoder和import sun.misc.BASE64Encoder的问题

jaksmine 2011-04-30 06:07:33
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>cd\

C:\>javac server.java
server.java:13: 警告:sun.misc.BASE64Decoder is Sun proprietary API and may be r
emoved in a future release
import sun.misc.BASE64Decoder;
^
server.java:14: 警告:sun.misc.BASE64Encoder is Sun proprietary API and may be r
emoved in a future release
import sun.misc.BASE64Encoder;
^
server.java:85: 警告:sun.misc.BASE64Encoder is Sun proprietary API and may be r
emoved in a future release
BASE64Encoder encoder = new BASE64Encoder();
^
server.java:85: 警告:sun.misc.BASE64Encoder is Sun proprietary API and may be r
emoved in a future release
BASE64Encoder encoder = new BASE64Encoder();
^
server.java:101: 警告:sun.misc.BASE64Decoder is Sun proprietary API and may be
removed in a future release
BASE64Decoder decoder = new BASE64Decoder();
^
server.java:101: 警告:sun.misc.BASE64Decoder is Sun proprietary API and may be
removed in a future release
BASE64Decoder decoder = new BASE64Decoder();
^
6 警告

该收告诉一下怎么解决???
...全文
2414 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jaksmine 2011-05-01
  • 打赏
  • 举报
回复
怎么没人理我。大家帮我看看啦。。。。。
jaksmine 2011-04-30
  • 打赏
  • 举报
回复
我还是把我的源码贴出来吧,我的程序是做一个服务器端和浏览器端用rsa加密信息传输的功能。
服务器端程序:
import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.crypto.Cipher;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;

class ServerThread extends Thread
{
/** 指定加密算法为RSA */
private static String ALGORITHM = "RSA";
/** 指定key的大小 */
private static int KEYSIZE = 1024;
/** 指定公钥存放文件 */
private static String PUBLIC_KEY_FILE = "Server_PublicKey";
/** 指定私钥存放文件 */
private static String PRIVATE_KEY_FILE = "Server_PrivateKey";

Socket sock;
public ServerThread(Socket s) //服务器端进程。
{
sock=s;
}
/**
* 生成密钥对
*/
private static void generateKeyPair() throws Exception
{
/** RSA算法要求有一个可信任的随机数源 */
SecureRandom sr = new SecureRandom();
/** 为RSA算法创建一个KeyPairGenerator对象 */
KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM);
/** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */
kpg.initialize(KEYSIZE, sr);
/** 生成密匙对 */
KeyPair kp = kpg.generateKeyPair();
/** 得到公钥 */
Key publicKey = kp.getPublic();
/** 得到私钥 */
Key privateKey = kp.getPrivate();
/** 用对象流将生成的密钥写入文件 */
ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream(PUBLIC_KEY_FILE));
ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(PRIVATE_KEY_FILE));
oos1.writeObject(publicKey);
oos2.writeObject(privateKey);
/** 清空缓存,关闭文件输出流 */
oos1.close();
oos2.close();
}

/**
* 加密方法
* source: 源数据
*/
public static String encrypt(String source) throws Exception
{
generateKeyPair();
/** 将文件中的公钥对象读出 */
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
Key key = (Key) ois.readObject();
ois.close();
/** 得到Cipher对象来实现对源数据的RSA加密 */
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] b = source.getBytes();
/** 执行加密操作 */
byte[] b1 = cipher.doFinal(b);
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(b1);
}

/**
* 解密算法
* cryptograph:密文
*/
public static String decrypt(String cryptograph) throws Exception
{
/** 将文件中的私钥对象读出 */
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
Key key = (Key) ois.readObject();
/** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder decoder = new BASE64Decoder();
byte[] b1 = decoder.decodeBuffer(cryptograph);
/** 执行解密操作 */
byte[] b = cipher.doFinal(b1);
return new String(b);
}
public void run()
{
try
{
/* 这个类是一个抽象类,表示所有字节输入流类的父类。*/
InputStream in= sock.getInputStream( ); //读取传送的内容
DataInputStream din= new DataInputStream(in); // 数据输入流
String message =din.readUTF();
OutputStream out= sock.getOutputStream( );
DataOutputStream dos= new DataOutputStream(out);
System.out.println("从浏览器接收到得密文为:");

String cryptograph;
String target;
try
{
cryptograph = encrypt(message);//生成的密文
System.out.println(cryptograph);
System.out.println("解密后的明文为:");
target = decrypt(cryptograph);//生成的密文
System.out.println(target);
dos.writeUTF(message); // 使用独立于机器的 UTF-8 编码格式,将一个串写入该基本输出流。
}
catch (Exception e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
in.close();
out.close();
sock.close();}
catch (IOException e)
{
System.out.println(e);
}
}
}

public class server
{
public static void main(String args[])
{
ServerSocket s=null;
try
{
s=new ServerSocket(5432); //设置端口号为5432
System.out.println("web服务器正在监听...");
}
catch (IOException e)
{
System.out.println(e);
System.exit(1);
}
while(true)
{
try
{
Socket cs = s.accept(); // 监听与本 socket 的连接并且接受它。
new ServerThread(cs).start();
}
catch(IOException e)
{
System.out.println("服务器与浏览器已经断开连接");
System.out.println(e);
}
}
}
}

浏览器端程序:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.net.InetAddress;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;


import javax.crypto.Cipher;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
public class browser extends Applet implements ActionListener
{
/** 指定加密算法为RSA */
private static String ALGORITHM = "RSA";
/** 指定key的大小 */
private static int KEYSIZE = 1024;
/** 指定公钥存放文件 */
private static String PUBLIC_KEY_FILE = "Browser_PublicKey";
/** 指定私钥存放文件 */
private static String PRIVATE_KEY_FILE = "Browser_PrivateKey";

Button but;
Label lab, lab1, lab2, lab3; // 标签
Panel pa,pa1,pa2,pa3; // 容器
TextField t1,t2,t3,t4;
TextArea ta1,ta2,ta3;
public void init()
{
pa = new Panel();
pa1 = new Panel();
ta1 = new TextArea("", 5, 20);
lab1 = new Label("请输入发送内容:");
lab2 = new Label("正在连接web服务器...");
pa.add(lab2);
add(pa);
pa1.add(lab1);
pa1.add(ta1);
but=new Button("发送");
pa1.add(but);
but.addActionListener(this); // 接收动作事件的监听者接口
add(pa1);
pa2=new Panel();
lab=new Label("加密后的结果为:");
pa2.add(lab);
ta2 = new TextArea("", 5, 20);
pa2.add(ta2);
add(pa2);
pa3 = new Panel();
lab3 = new Label("解密后的结果为:");
pa3.add(lab3);
ta3 = new TextArea("", 5, 20);
pa3.add(ta3);
add(pa3);
}

/**
* 生成密钥对
*/
private static void generateKeyPair() throws Exception
{
/** RSA算法要求有一个可信任的随机数源 */
SecureRandom sr = new SecureRandom();
/** 为RSA算法创建一个KeyPairGenerator对象 */
KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM);
/** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */
kpg.initialize(KEYSIZE, sr);
/** 生成密匙对 */
KeyPair kp = kpg.generateKeyPair();
/** 得到公钥 */
Key publicKey = kp.getPublic();
/** 得到私钥 */
Key privateKey = kp.getPrivate();
/** 用对象流将生成的密钥写入文件 */
ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream(PUBLIC_KEY_FILE));
ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(PRIVATE_KEY_FILE));
oos1.writeObject(publicKey);
oos2.writeObject(privateKey);
/** 清空缓存,关闭文件输出流 */
oos1.close();
oos2.close();
}

/**
* 加密方法
* source: 源数据
*/
public static String encrypt(String source) throws Exception
{
generateKeyPair();
/** 将文件中的公钥对象读出 */
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
Key key = (Key) ois.readObject();
ois.close();
/** 得到Cipher对象来实现对源数据的RSA加密 */
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] b = source.getBytes();
/** 执行加密操作 */
byte[] b1 = cipher.doFinal(b);
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(b1);
}

/**
* 解密算法
* cryptograph:密文
*/
public static String decrypt(String cryptograph) throws Exception
{
/** 将文件中的私钥对象读出 */
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
Key key = (Key) ois.readObject();
/** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder decoder = new BASE64Decoder();
byte[] b1 = decoder.decodeBuffer(cryptograph);

/** 执行解密操作 */
byte[] b = cipher.doFinal(b1);
return new String(b);
}

public void actionPerformed(ActionEvent e)
{
try
{
Socket s=new Socket ("127.0.0.1",5432); // 服务器程序所在的IP地址和其监听的端口号
InetAddress address=s.getInetAddress();
lab2.setEnabled(true);
lab2.setText("您连接:"+address+"成功,信息已成功发送。");
OutputStream out= s.getOutputStream( ); //从键盘上写入内容
DataOutputStream dout= new DataOutputStream(out);
dout.writeUTF(ta1.getText());
InputStream in= s.getInputStream( ); //从服务器上读取获得的数据
DataInputStream din= new DataInputStream(in);
String e1=din.readUTF();
String cryptograph;
String target;
try
{
cryptograph = encrypt(e1); //生成的密文
ta2.setText(cryptograph);
target = decrypt(cryptograph);//解密密文
ta3.setText(target);
}
catch (Exception e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}


in.close();
out.close();
s.close();
}
catch (IOException e2)
{
lab1.setText("连接失败");
System.out.println(e2);
}
}
}
在Myeclipse中运行正常,可是在浏览器中却不可触发按钮事件,是怎么回事????


jaksmine 2011-04-30
  • 打赏
  • 举报
回复
可是运行后用appletviewer查看结果是正确的,用浏览器就不行了,现在我必须用浏览器查看,不知道是不是这个警告的问题???
Bannings 2011-04-30
  • 打赏
  • 举报
回复
都是警告,又没有问题.没有正式发行的API

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧