⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 securecredential.java~

📁 Java p2p程序设计2002年版
💻 JAVA~
字号:
package com.sams.jxta.security;

import net.jxta.credential.Credential;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.peer.PeerID;
import net.jxta.id.ID;
import net.jxta.membership.MembershipService;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.document.MimeMediaType;
import net.jxta.document.Element;

import jxta.security.hash.Hash;
import jxta.security.impl.hash.MD5Hash;
import jxta.security.impl.publickey.RSAKey;
import jxta.security.impl.publickey.RSA;
import jxta.security.impl.publickey.RSAPublickeyData;
import jxta.security.impl.publickey.RSAPrivatekeyData;
import jxta.security.impl.cipher.KeyBuilder;
import jxta.security.exceptions.CryptoException;
import jxta.security.crypto.JxtaCrypto;
import jxta.security.impl.crypto.JxtaCryptoSuite;
import jxta.security.signature.Signature;
import jxta.security.util.URLBase64;

import java.text.DateFormat;

/*
 * This class represents a Credential that is supplied to a
 * peer after a successful apply process.The credential is 
 * signed using an RSA Signature 
 */
public class SecureCredential implements Credential {

    // Reference to the Membership Service
    private SecureMembershipService membershipService;
    private RSAKey rsaKey;
	private Signature signature;
	byte[] signatureArray;
	byte[] messageBytes;
		
	private static final org.apache.log4j.Category LOG = 
        org.apache.log4j.Category.getInstance(SecureCredential.class.getName());

   /*
    * Constructor which takes a Membership Service Object
    */
    public SecureCredential(SecureMembershipService membershipService){
		this.membershipService = membershipService;   
		// We create a signed message
		createSignedMessage();
    }
   
    /*
     * Getter for the Membership Service
     */
    public MembershipService  getSourceService(){
        
        return membershipService;
    }

    /*
     * This method returns the PeerGroup ID
     */
    public ID getPeerGroupID(){
        return membershipService.getPeerGroup().getPeerGroupID();
    }
    
    /*
     * This method returns the Peer ID
     */
    public ID getPeerID(){
        return membershipService.getPeerGroup().getPeerID();
    }

    /*
     * This method returns a Structured Document representing the Credential
     */
    public StructuredDocument getDocument(MimeMediaType as) throws Exception {
        
            StructuredDocument doc =
                StructuredDocumentFactory.newStructuredDocument( as,"SecureCredential" );

            /*Element e = doc.createElement( "Message", message);
            doc.appendChild( e );
            e = doc.createElement("Signature",new String(messageSignature.getBytes());
            doc.appendChild( e );
            return doc;
            */
            //??? Initially I wanted to put the Message signature into the structured 
            // document.But somehow it does not seem to work.I guess it has got something to
            // do with the encoding.Any ideas ?
            return doc;
    }

    private void createSignedMessage(){
    	String date    = DateFormat.getDateTimeInstance().format(new java.util.Date());
    	String message = "Membership since "+date;
    	// Now we have created a Message that identifies the time 
    	// of Peer Membership Approval 
    	LOG.debug("Message is = "+message);
		// Get the MD5 Hash of the message
        messageBytes = getMD5Hash(message);
        System.out.println("MESSAGE =>"+new String(messageBytes));
    	// Next step is to sign the message 
    	// For this we use the RSA Algorithm 
		try{
		
			rsaKey = (RSAKey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA,
									             KeyBuilder.LENGTH_RSA_512,false);
			JxtaCrypto suite = new JxtaCryptoSuite(JxtaCrypto.PROFILE_RSA_SHA1,rsaKey, 
					        			Signature.ALG_RSA_SHA_PKCS1,(byte)0);
  			RSA rsaAlgorithm = new RSA(rsaKey);
			LOG.debug("Setting Public Key");
			// This method computes a public key
			rsaAlgorithm.setPublicKey();
			LOG.debug("Setting Private Key");
			// This method computes a private key			
			rsaAlgorithm.setPrivateKey();
			//Get the data for both public and private keys
			RSAPublickeyData publicKeyData =(RSAPublickeyData)rsaAlgorithm.getPublickey();
   	        RSAPrivateKeyData privateKeyData =(RSAPrivateKeyData)rsaAlgorithm.getPrivatekey();
   	        // Get a signature Object for the actual signing
   	        signature = suite.getJxtaSignature();
   	        signature.init(Signature.MODE_SIGN);
			signatureArray = signature.sign(messageBytes, 0, messageBytes.length);
		} catch (CryptoException cryptoException){
    		LOG.error("EXCEPTION IN SIGNING MESSAGE",cryptoException);
   		} catch (Exception exception){
    		LOG.error("EXCEPTION IN SIGNING MESSAGE",exception);    	
    	}
   	}

    public Signature getSignature(){
    	return signature;
   	}

    public byte[] getMessageBytes(){
    	return messageBytes;
   	}
    
    public byte[] getMessageSignatureBytes(){
    	return signatureArray;
   	}
    
	// Utility method to create an MD5 Hash
    private byte[] getMD5Hash(String string){
    	
   		  	// We can create a Hash Object
   		  	// It is not always necessary to use the Suite
    		Hash hash = new MD5Hash();
    		// Get the digest length of the Hash
       		int digestLength = hash.getDigestLength();
			byte[] passwordInBytes = string.getBytes();
			// Calculate the length needed by the output byte array
			int outputLength = (string.length() < digestLength ? digestLength : string.length());
			// Create a new array to hold the output
			byte[] outputBytes = new byte[digestLength]; 
			// This is where the actual hashing is done
			hash.doFinal(passwordInBytes, 0, passwordInBytes.length, outputBytes, 0);
			return outputBytes;

   	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -