java nonewithrsa,java - java.security.NoSuchAlgorithmException:RSA签名不可用 - 堆栈内存溢出...

这是例外

Exception in thread "main" java.security.NoSuchAlgorithmException: RSA Signature not available

at java.security.Signature.getInstance(Signature.java:229)

at MailClient.main(MailClient.java:52)

这是我的代码

import java.io.*;

import java.net.*;

import java.nio.ByteBuffer;

import java.util.*;

import java.security.*;

public class MailClient {

public String getMessage(Mail m){

return m.message;

}

public static void main(String[] args) throws Exception {

// Initialisation

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String host = args[0];

int port = Integer.parseInt(args[1]);

String userid = args[2];

while(true) {

// connect to server

Socket s = new Socket(host,port);

DataInputStream dis = new DataInputStream(s.getInputStream());

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());

oos.flush();

ObjectInputStream ois = new ObjectInputStream(s.getInputStream());

// TO DO: login

// these two lines are here just to make the supplied programs run without crashing.

// You may want to change them, and certainly add things after them

dos.writeUTF(userid);

String userPrivateKeyFileName = userid + ".prv";

// Get the key to create the signature

ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(userPrivateKeyFileName));

PrivateKey privateKey = (PrivateKey)keyIn.readObject();

keyIn.close();

// create timeStamp and random number

long t1 = (new Date()).getTime();

double q1 = Math.random();

// ByteBuffer to convert to bytes later

ByteBuffer bb = ByteBuffer.allocate(16);

bb.putLong(t1);

bb.putDouble(q1);

// create signature, using timeStamp and random number as data

Signature sig = Signature.getInstance("RSA");

sig.initSign(privateKey);

sig.update(bb.array());

byte[] signature = sig.sign();

// send data and signature

DataOutputStream out = new DataOutputStream(s.getOutputStream());

out.writeUTF(userid);

out.writeLong(t1);

out.writeDouble(q1);

out.writeInt(signature.length);

out.write(signature);

out.flush();

boolean answer = dis.readBoolean();

//passed the verifyLogin

if (answer)

{

// receive how many messages

int numMsg = dis.readInt();

System.out.println("You have " + numMsg + " incoming messages.");

// TO DO: read messages

ArrayList msg = new ArrayList<>(numMsg);

for(int i=0;i

//@Unchecked

msg = (ArrayList) ois.readObject();

}

while(!msg.isEmpty()){

//for each mail, display sender,timestamp,message

System.out.println(msg.get(0).sender);

System.out.println(msg.get(0).timestamp);

System.out.println(msg.get(0).message);

MessageDigest md = MessageDigest.getInstance("SHA-1");

md.update(msg.get(0).hashcash);

byte[] digest = md.digest();

boolean normalMail = msg.get(0).checkHashcash(digest);

if(normalMail){

//check each mail is original that it isn't modified

//receive mail

System.out.println(msg.get(0).message);

}

else{System.out.println("it's a spam message.");

System.out.println(msg.get(0).message);

}

msg.remove(0);

}

// send messages

System.out.println("Do you want to send a message [Y/N]?");

String wantToSend = br.readLine();

if (!wantToSend.equals("Y")) {

dos.writeBoolean(false);

return ;

}

dos.writeBoolean(true);

System.out.println("Enter userid of recipient:");

String recipient = br.readLine();

System.out.println("Type your message:");

String message = br.readLine();

// TO DO: send mail

Mail m = new Mail(userid, recipient, message);

MessageDigest md = MessageDigest.getInstance("SHA-1");

md.update(m.hashcash);

byte[] digest = md.digest();

while(m.checkHashcash(digest)){

m.setHashcash(digest);

}

out.write(digest);

out.flush();

// send timeStamp and digest to server

long mailTimestamp = m.timestamp.getTime();

out.writeLong(mailTimestamp);

oos.writeObject(m);

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值