encrypt.java
来自「JGRoups源码」· Java 代码 · 共 1,405 行 · 第 1/3 页
JAVA
1,405 行
newMsg.putHeader(EncryptHeader.KEY, new EncryptHeader( EncryptHeader.KEY_REQUEST, getSymVersion())); passDown(new Event(Event.MSG, newMsg)); return newMsg; } /* (non-Javadoc) * @see org.jgroups.stack.Protocol#down(org.jgroups.Event) */ public void down(Event evt) { switch (evt.getType()) { case Event.MSG : try { if (queue_down){ if(trace) log.trace("queueing down message as no session key established" + evt.getArg()); downMessageQueue.put(evt); // queue messages if we are waiting for a new key } else { // make sure the down queue is drained first to keep ordering if (!suppliedKey){ drainDownQueue(); } sendDown(evt); } } catch (Exception e) { log.warn("unable to send down event " + e); } return; case Event.VIEW_CHANGE: View view=(View)evt.getArg(); if (log.isInfoEnabled()) log.info("handling view: " + view); if (!suppliedKey){ handleViewChange(view); } break; default : break; } passDown(evt); } /** * @throws Exception * @throws QueueClosedException */ private void drainDownQueue() throws Exception, QueueClosedException { // we do not synchronize here as we only have one down thread so we should never get an issue // first lets replay any oustanding events Event tmp =null; while((tmp = (Event)downMessageQueue.poll(0L) )!= null){ sendDown(tmp); } } /** * @param evt * @throws Exception */ private void sendDown(Event evt) throws Exception { if (evt.getType() != Event.MSG) { return; } Message msg = (Message) evt.getArg(); if(msg.getLength() == 0) { passDown(evt); return; } EncryptHeader hdr=new EncryptHeader(EncryptHeader.ENCRYPT, getSymVersion()); hdr.encrypt_entire_msg=this.encrypt_entire_message; if(encrypt_entire_message) { byte[] serialized_msg=Util.streamableToByteBuffer(msg); byte[] encrypted_msg=encryptMessage(symEncodingCipher, serialized_msg); Message tmp=msg.copy(false); // we need to preserve headers which may already be present tmp.setBuffer(encrypted_msg); tmp.setSrc(local_addr); tmp.putHeader(EncryptHeader.KEY, hdr); passDown(new Event(Event.MSG, tmp)); return; } // put our encrypt header on the message msg.putHeader(EncryptHeader.KEY, hdr); // copy neeeded because same message (object) may be retransmitted -> no double encryption Message msgEncrypted = msg.copy(false); msgEncrypted.setBuffer(encryptMessage(symEncodingCipher, msg.getBuffer())); passDown(new Event(Event.MSG, msgEncrypted)); } /** * * @param cipher * @param plain * @return * @throws Exception */ private byte[] encryptMessage(Cipher cipher, byte[] plain) throws Exception { return cipher.doFinal(plain); } private SecretKeySpec decodeKey(byte[] encodedKey) throws Exception { // try and decode secrey key sent from keyserver byte[] keyBytes = asymCipher.doFinal(encodedKey); SecretKeySpec keySpec = null; try { keySpec = new SecretKeySpec(keyBytes, getAlgorithm(symAlgorithm)); // test reconstituted key to see if valid Cipher temp = Cipher.getInstance(symAlgorithm); temp.init(Cipher.SECRET_KEY, keySpec); } catch (Exception e) { log.fatal(e); keySpec = null; } return keySpec; } /** * used to reconstitute public key sent in byte form from peer * @param encodedKey * @return PublicKey */ private PublicKey generatePubKey(byte[] encodedKey) { PublicKey pubKey = null; try { KeyFactory KeyFac = KeyFactory.getInstance(getAlgorithm(asymAlgorithm)); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(encodedKey); pubKey = KeyFac.generatePublic(x509KeySpec); } catch (Exception e) { e.printStackTrace(); } return pubKey; } /* * simple helper method so we can see the format of the byte arrays in a * readable form could be better to use Base64 but will do for now */ private String formatArray(byte[] array) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < array.length; i++) { buf.append(Integer.toHexString(array[i])); } return buf.toString(); } /** * @return Returns the asymInit. */ protected int getAsymInit() { return asymInit; } /** * @return Returns the asymProvider. */ protected String getAsymProvider() { return asymProvider; } /** * @return Returns the desKey. */ protected SecretKey getDesKey() { return secretKey; } /** * @return Returns the kpair. */ protected KeyPair getKpair() { return Kpair; } /** * @return Returns the asymCipher. */ protected Cipher getAsymCipher() { return asymCipher; } /** * @return Returns the serverPubKey. */ protected PublicKey getServerPubKey() { return serverPubKey; } /** * @return Returns the symAlgorithm. */ protected String getSymAlgorithm() { return symAlgorithm; } /** * @return Returns the symInit. */ protected int getSymInit() { return symInit; } /** * @return Returns the symProvider. */ protected String getSymProvider() { return symProvider; } /** * @return Returns the asymAlgorithm. */ protected String getAsymAlgorithm() { return asymAlgorithm; } /** * @return Returns the symVersion. */ private String getSymVersion() { return symVersion; } /** * @param symVersion * The symVersion to set. */ private void setSymVersion(String symVersion) { this.symVersion = symVersion; } /** * @return Returns the secretKey. */ private SecretKey getSecretKey() { return secretKey; } /** * @param secretKey The secretKey to set. */ private void setSecretKey(SecretKey secretKey) { this.secretKey = secretKey; } /** * @return Returns the keyStoreName. */ protected String getKeyStoreName() { return keyStoreName; } /** * @return Returns the symDecodingCipher. */ protected Cipher getSymDecodingCipher() { return symDecodingCipher; } /** * @return Returns the symEncodingCipher. */ protected Cipher getSymEncodingCipher() { return symEncodingCipher; } /** * @return Returns the local_addr. */ protected Address getLocal_addr() { return local_addr; } /** * @param local_addr The local_addr to set. */ protected void setLocal_addr(Address local_addr) { this.local_addr = local_addr; } /** * @return Returns the keyServerAddr. */ protected Address getKeyServerAddr() { return keyServerAddr; } /** * @param keyServerAddr The keyServerAddr to set. */ protected void setKeyServerAddr(Address keyServerAddr) { this.keyServerAddr = keyServerAddr; } public static class EncryptHeader extends org.jgroups.Header implements Streamable { short type; public static final short ENCRYPT = 0; public static final short KEY_REQUEST = 1; public static final short SERVER_PUBKEY = 2; public static final short SECRETKEY = 3; public static final short SECRETKEY_READY = 4; // adding key for Message object purpose static final String KEY = "encrypt"; String version; boolean encrypt_entire_msg=false; public EncryptHeader() { } public EncryptHeader(short type) { //this(type, 0l); this.type = type; this.version = ""; } public EncryptHeader(short type, String version) { this.type = type; this.version = version; } public void writeExternal(java.io.ObjectOutput out) throws IOException { out.writeShort(type); out.writeObject(version); } public void readExternal(java.io.ObjectInput in) throws IOException, ClassNotFoundException { type = in.readShort(); version = (String)in.readObject(); } public void writeTo(DataOutputStream out) throws IOException { out.writeShort(type); Util.writeString(version, out); out.writeBoolean(encrypt_entire_msg); } public void readFrom(DataInputStream in) throws IOException, IllegalAccessException, InstantiationException { type=in.readShort(); version=Util.readString(in); encrypt_entire_msg=in.readBoolean(); } public String toString() { return "ENCRYPT [type=" + type + " version=\"" + version + "\"]"; } public long size() { long retval=Global.SHORT_SIZE + Global.BYTE_SIZE + Global.BYTE_SIZE; if(version != null) retval+=version.length() +2; return retval; } /* * (non-Javadoc) * * @see java.lang.Object#equals(java.lang.Object) */ public boolean equals(Object obj) { if (obj instanceof EncryptHeader) { return ((((EncryptHeader) obj).getType() == type) && ((((EncryptHeader) obj) .getVersion().equals(version)))); } return false; } /** * @return Returns the type. */ protected short getType() { return type; } /** * @return Returns the version. */ protected String getVersion() { return version; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?