当前位置: 首页>>代码示例>>Java>>正文


Java Cipher类代码示例

本文整理汇总了Java中com.sun.spotx.crypto.Cipher的典型用法代码示例。如果您正苦于以下问题:Java Cipher类的具体用法?Java Cipher怎么用?Java Cipher使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Cipher类属于com.sun.spotx.crypto包,在下文中一共展示了Cipher类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: engineInit

import com.sun.spotx.crypto.Cipher; //导入依赖的package包/类
/**
    * Initializes this cipher with a key and a set of algorithm
    * parameters.
    *
    * @param opMode the operation mode of this cipher
    * @param key the encryption key
    * @param params the algorithm parameters
    *
    * @exception java.security.InvalidKeyException if the given key
    * is inappropriate for initializing this cipher
    * @exception java.security.InvalidAlgorithmParameterException
    * if the given algorithm parameters are inappropriate for this cipher
    * @exception IllegalArgumentException if opMode is incorrect
    */
   protected void engineInit(int opMode, Key key, AlgorithmParameterSpec params)
           throws InvalidKeyException, InvalidAlgorithmParameterException {

if (params!=null) 
    throw new InvalidAlgorithmParameterException(RSA_ALGORITHM_NAME+" does not support AlgorithmParameterSpec. Must be null."); 

       if (!(key instanceof RSAKey)) {
           throw new InvalidKeyException();
       }

       if (opMode != Cipher.DECRYPT_MODE && opMode != Cipher.ENCRYPT_MODE) {
           throw new IllegalArgumentException("Wrong operation mode");
       }

       mode = opMode;
       ckey = (RSAKey)key;

       if (ckey.getModulusLen() == 0) {
           throw new InvalidKeyException();
       }

       messageToSign = new byte[ckey.getModulusLen()];
       bytesInMessage = 0;
   }
 
开发者ID:tomatsu,项目名称:squawk,代码行数:39,代码来源:RSA.java

示例2: RSASig

import com.sun.spotx.crypto.Cipher; //导入依赖的package包/类
/**
 * Constructs an RSA signature object that uses the specified
 * signature algorithm.
 *
 * @param sigPrefix Prefix for the signature
 * @param messageDigest Message digest for the signature
 *
 * @exception NoSuchAlgorithmException if RSA is
 * not available in the caller's environment.  
 */
RSASig(byte[] sigPrefix, MessageDigest messageDigest)
        throws NoSuchAlgorithmException {
    prefix = sigPrefix;
    md = messageDigest;

    try {
        c = Cipher.getInstance("RSA");
    } catch (NoSuchPaddingException e) {
        // we used the default mode and padding this should not happen
        throw new NoSuchAlgorithmException();
    }
}
 
开发者ID:tomatsu,项目名称:squawk,代码行数:23,代码来源:RSASig.java

示例3: initVerify

import com.sun.spotx.crypto.Cipher; //导入依赖的package包/类
/**
 * Initializes the <CODE>RSASig</CODE> object with the appropriate
 * <CODE>Key</CODE> for signature verification.
 * 
 * @param theKey the key object to use for verification
 *
 * @exception InvalidKeyException if the key type is inconsistent 
 * with the mode or signature implementation.
 */
public void initVerify(PublicKey theKey) throws InvalidKeyException {
    if (!(theKey instanceof RSAPublicKey)) {
        throw new InvalidKeyException();
    }

    c.init(Cipher.DECRYPT_MODE, theKey);

    k = (RSAKey)theKey;
}
 
开发者ID:tomatsu,项目名称:squawk,代码行数:19,代码来源:RSASig.java

示例4: initSign

import com.sun.spotx.crypto.Cipher; //导入依赖的package包/类
/**
 * Initializes the <CODE>RSASig</CODE> object with the appropriate
 * <CODE>Key</CODE> for signature creation.
 * 
 * @param theKey the key object to use for signing
 *
 * @exception InvalidKeyException if the key type is inconsistent 
 * with the mode or signature implementation.
 */
public void initSign(PrivateKey theKey) throws InvalidKeyException {
    if (!(theKey instanceof RSAPrivateKey)) {
        throw new InvalidKeyException();
    }

    c.init(Cipher.ENCRYPT_MODE, theKey);

    k = (RSAKey)theKey;
}
 
开发者ID:tomatsu,项目名称:squawk,代码行数:19,代码来源:RSASig.java

示例5: init

import com.sun.spotx.crypto.Cipher; //导入依赖的package包/类
/**
 * Chops up a master secret into the client and server MAC secrets, 
 * bulk encryption keys and IVs. Also initializes the Cipher and 
 * MessageDigest objects used in record encoding/decoding. 
 * 
 * @param role role (either CLIENT or SERVER) of this side in the SSL 
 *             negotiation
 * @param clientRand 32-byte random value chosen by the client
 * @param serverRand 32-byte random value chosen by the server
 * @param suite negotiated cipher suite
 * @param masterSecret master secret resulting from the key exchange
 *
 * @exception Exception if the negotiated cipher suite involves an 
 *                      unsupported hash or cipher algorithm
 */
void init(byte role, byte[] clientRand, byte[] serverRand, 
          short suite, byte[] masterSecret) throws Exception {
	
    CipherSuiteData data = new CipherSuiteData(suite, ver);
    data.generateKeys(clientRand, serverRand, masterSecret);

    // depending on role, we choose corresponding MAC secrets 
    // and cipher bulk keys for encoder and decoder
    byte[] encodeSecret;
    byte[] decodeSecret;
    SecretKeySpec decodeCipherKey;
    SecretKeySpec encodeCipherKey;
    if (role == CLIENT) {
        encodeSecret = data.getClientMACSecret();
        decodeSecret = data.getServerMACSecret();
        encodeCipherKey = data.getClientBulkKey();
        decodeCipherKey = data.getServerBulkKey();
    } else {
        encodeSecret = data.getServerMACSecret();
        decodeSecret = data.getClientMACSecret();
        encodeCipherKey = data.getServerBulkKey();
        decodeCipherKey = data.getClientBulkKey();
    }
    
    Cipher encodeCipher = data.getEncodeCipher();
    encodeCipher.init(Cipher.ENCRYPT_MODE, encodeCipherKey);
    Cipher decodeCipher = data.getDecodeCipher();
    decodeCipher.init(Cipher.DECRYPT_MODE, decodeCipherKey);
    
    encoder = new RecordEncoder(data.getEncodeDigest(), encodeSecret,
            data.getPadLength(), encodeCipher, ver);
    decoder = new RecordDecoder(data.getDecodeDigest(), decodeSecret,
            data.getPadLength(), decodeCipher, ver);
}
 
开发者ID:tomatsu,项目名称:squawk,代码行数:50,代码来源:Record.java

示例6: engineInit

import com.sun.spotx.crypto.Cipher; //导入依赖的package包/类
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params)
throws InvalidKeyException, InvalidAlgorithmParameterException {


    initOk = false;
    if (!(key instanceof SecretKeySpec)) {
        throw new InvalidKeyException("Key is not of required type SecretKey.");
    }
    if ((opmode != Cipher.ENCRYPT_MODE) && (opmode != Cipher.DECRYPT_MODE)) {
        throw new IllegalArgumentException("Unsupported mode.");
    }
	
    if (params instanceof IvParameterSpec) {
        ivParameter=(IvParameterSpec)params;
        if (!modeCBC) {
    	throw new InvalidAlgorithmParameterException("AlgorithmParameterSpec must be null for "+ALG1+" in ECB mode.");
        }
    } else if (params!=null) {
        throw new InvalidAlgorithmParameterException("AlgorithmParameterSpec params has to be null or IvParamater. Is: "+params.getClass().getName());
    }
    
    if (modeCBC) {
        IV = new int[4];
        if (ivParameter == null) {
            clearIV();
        } else {
    	unpackBlock(ivParameter.getIV(), 0, IV); 
        }
    }
    
    mode = opmode;
    this.key = (SecretKeySpec)key;

    generateWorkingKey(this.key.getEncoded(), (mode == Cipher.ENCRYPT_MODE)); 
    initOk = true;
}
 
开发者ID:tomatsu,项目名称:squawk,代码行数:37,代码来源:Alg1.java

示例7: engineUpdate

import com.sun.spotx.crypto.Cipher; //导入依赖的package包/类
protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalStateException, ShortBufferException {
    if ((inputLen < 0) || ((inputLen % BLOCK_SIZE) != 0) ||
    (inputOffset + inputLen > input.length) ||
    (outputOffset + inputLen > output.length))
		throw new ShortBufferException();

if (!initOk) {
    throw new IllegalStateException();
       }
       
       int res = inputLen;
       int lastC0 = 0, lastC1 = 0, lastC2 = 0, lastC3 = 0;
       
       while (inputLen > 0) {
           if (mode == Cipher.ENCRYPT_MODE) {
               unpackBlock(input, inputOffset, C);
               if (modeCBC) {
                   C[0] ^= IV[0]; C[1] ^= IV[1]; C[2] ^= IV[2]; C[3] ^= IV[3];
               }
               encryptBlock(workingKey);
               if (modeCBC) {
                   IV[0] = C[0]; IV[1] = C[1]; IV[2] = C[2]; IV[3] = C[3];
               }
               packBlock(output, outputOffset, C);
           } else {
               unpackBlock(input, inputOffset, C);
               if (modeCBC) {
                   // remember cipher block. this will be our next IV
                   lastC0 = C[0]; lastC1 = C[1]; lastC2 = C[2]; lastC3 = C[3];
               }
               decryptBlock(workingKey);
               if (modeCBC) {
                   C[0] ^= IV[0]; C[1] ^= IV[1]; C[2] ^= IV[2]; C[3] ^= IV[3];
                   IV[0] = lastC0; IV[1] = lastC1; IV[2] = lastC2; IV[3] = lastC3;
               }
               packBlock(output, outputOffset, C);
           }
           inputOffset += BLOCK_SIZE;
           outputOffset += BLOCK_SIZE;
           inputLen -= BLOCK_SIZE;
       }
       
       return res;
   }
 
开发者ID:tomatsu,项目名称:squawk,代码行数:45,代码来源:Alg1.java

示例8: engineInit

import com.sun.spotx.crypto.Cipher; //导入依赖的package包/类
protected void engineInit(int theMode, Key theKey, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException {
if (params!=null) 
    throw new InvalidAlgorithmParameterException(ALG2+" does not support AlgorithmParameterSpec. Must be null"); 
if (!(theKey instanceof SecretKeySpec)) {
    throw new InvalidKeyException();
}
 if   ((theMode != Cipher.ENCRYPT_MODE) && 
     (theMode != Cipher.DECRYPT_MODE)) {
    throw new InvalidAlgorithmParameterException();	    
    //CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
}
       /*if (!theKey.isInitialized()) {
    CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
       }*/

this.mode = theMode;
       this.key = theKey;
       
byte[] K = ((SecretKeySpec)theKey).getEncoded();
       int kLen = ((SecretKeySpec)theKey).getEncoded().length;

/* Initialize S */
for (int i = 0; i < 256; i++) {
           S[i] = (byte) i;
       }

int j = 0;
       int k = 0;
byte temp;
for (int i = 0; i < 256; i++) {
    j = (j + ((S[i] + K[k]) & 0xff)) & 0xff;
    temp = S[i];
    S[i] = S[j];
    S[j] = temp;
           if (++k >= kLen) k = 0;
}
       
       jj = ii = 0;
       
       initOk = true;
       needsReset = false;
   }
 
开发者ID:tomatsu,项目名称:squawk,代码行数:43,代码来源:Alg2.java

示例9: RecordEncoder

import com.sun.spotx.crypto.Cipher; //导入依赖的package包/类
/**
 * Constructs RecordEncoder object
 * 
 * @param dgst digest for MAC computation
 * @param secret MAC secret
 * @param padLen padding length
 * @param cphr cipher used for encoding
 * @param ver version of SSL/TLS
 */
RecordEncoder(MessageDigest dgst, byte[] secret, int padLen, Cipher cphr, byte ver) {
    macSecret = secret;
    digest = dgst;
    digestLength = digest.getDigestLength();
    padLength = padLen;
    cipher = cphr;
    version = ver;
}
 
开发者ID:tomatsu,项目名称:squawk,代码行数:18,代码来源:Record.java

示例10: RecordDecoder

import com.sun.spotx.crypto.Cipher; //导入依赖的package包/类
/**
 * Constructs RecordDecoder object
 * 
 * @param dgst digest for MAC computation
 * @param secret MAC secret
 * @param padLen padding length
 * @param cphr cipher used for decoding
 * @param ver version of SSL/TLS
 */
RecordDecoder(MessageDigest dgst, byte[] secret, int padLen, Cipher cphr, byte ver) {
    macSecret = secret;
    digest = dgst;
    digestLength = digest.getDigestLength();
    padLength = padLen;
    cipher = cphr;
    version = ver;
}
 
开发者ID:tomatsu,项目名称:squawk,代码行数:18,代码来源:Record.java

示例11: getEncodeCipher

import com.sun.spotx.crypto.Cipher; //导入依赖的package包/类
/**
 * Get cipher used for encoding
 * @return encode cipher
 */
Cipher getEncodeCipher() {
    return encodeCipher;
}
 
开发者ID:tomatsu,项目名称:squawk,代码行数:8,代码来源:Record.java

示例12: getDecodeCipher

import com.sun.spotx.crypto.Cipher; //导入依赖的package包/类
/**
 * Get cipher used for decoding
 * @return decode cipher
 */
Cipher getDecodeCipher() {
    return decodeCipher;
}
 
开发者ID:tomatsu,项目名称:squawk,代码行数:8,代码来源:Record.java


注:本文中的com.sun.spotx.crypto.Cipher类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。