當前位置: 首頁>>代碼示例>>Java>>正文


Java Cipher.doFinal方法代碼示例

本文整理匯總了Java中javax.crypto.Cipher.doFinal方法的典型用法代碼示例。如果您正苦於以下問題:Java Cipher.doFinal方法的具體用法?Java Cipher.doFinal怎麽用?Java Cipher.doFinal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.crypto.Cipher的用法示例。


在下文中一共展示了Cipher.doFinal方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * this method is used to decrypt password.
 * 
 * @param value encrypted password.
 * @return decrypted password.
 */
public static String decrypt(String value) {
  try {
    sunbird_encryption = DefaultEncryptionServivceImpl.getSalt();
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);

    String dValue = null;
    String valueToDecrypt = value.trim();
    for (int i = 0; i < ITERATIONS; i++) {
      byte[] decordedValue = new sun.misc.BASE64Decoder().decodeBuffer(valueToDecrypt);
      byte[] decValue = c.doFinal(decordedValue);
      dValue = new String(decValue).substring(sunbird_encryption.length());
      valueToDecrypt = dValue;
    }
    return dValue;
  } catch (Exception ex) {
    ProjectLogger.log("Exception Occurred while decrypting value");
  }
  return value;
}
 
開發者ID:project-sunbird,項目名稱:sunbird-utils,代碼行數:28,代碼來源:DefaultDecryptionServiceImpl.java

示例2: aesEncryptToBytes

import javax.crypto.Cipher; //導入方法依賴的package包/類
private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {  
    KeyGenerator kgen = KeyGenerator.getInstance("AES");  
    kgen.init(128, new SecureRandom(encryptKey.getBytes()));  
  
    Cipher cipher = Cipher.getInstance("AES");  
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));  
      
    return cipher.doFinal(content.getBytes("utf-8"));  
}
 
開發者ID:onsoul,項目名稱:os,代碼行數:10,代碼來源:AESUtils.java

示例3: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static String decrypt(PublicKey publicKey, String cipherText)
        throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    try {
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
    } catch (InvalidKeyException e) {
        //  IBM JDK not support Private key encryption, public key decryption
        // so fake an PrivateKey for it
        RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
        RSAPrivateKeySpec spec = new RSAPrivateKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent());
        Key fakePrivateKey = KeyFactory.getInstance("RSA").generatePrivate(spec);
        cipher = Cipher.getInstance("RSA"); //It is a stateful object. so we need to get new one.
        cipher.init(Cipher.DECRYPT_MODE, fakePrivateKey);
    }

    if (cipherText == null || cipherText.length() == 0) {
        return cipherText;
    }

    byte[] cipherBytes = Base64.base64ToByteArray(cipherText);
    byte[] plainBytes = cipher.doFinal(cipherBytes);

    return new String(plainBytes);
}
 
開發者ID:actiontech,項目名稱:dble,代碼行數:25,代碼來源:DecryptUtil.java

示例4: encrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * Encrypts a given byte array based on a shared secret.
 *
 * @param bytes
 * @return the encrypted bytes as Base64
 * @throws GeneralSecurityException
 *             on any problem during encryption
 */
public static byte[] encrypt(byte[] bytes) throws GeneralSecurityException {

    SecretKeySpec skeySpec = new SecretKeySpec(
            Base64.decodeBase64(ENCRYPTION_KEY), "AES");

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    byte[] encrypted = cipher.doFinal(bytes);
    return Base64.encodeBase64(encrypted);
}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:20,代碼來源:AESEncrypter.java

示例5: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static String decrypt(String input) {
    if(input == null) return null;
    byte[] decrypted = null;
    try{
        SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skey);
        decrypted = cipher.doFinal(Base64.decode(input, Base64.DEFAULT));
    } catch(Exception e){
        e.printStackTrace();
    }
    return new String(decrypted);
}
 
開發者ID:skydoves,項目名稱:PreferenceRoom,代碼行數:14,代碼來源:SecurityUtils.java

示例6: encrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
private byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] data = cipher.doFinal(clear);
    byte[] iv = cipher.getIV();
    byte[] packet = new byte[data.length+iv.length];
    for (int i = 0; i < iv.length; i++)
        packet[i] = iv[i];
    for (int i = 0; i < data.length; i++)
        packet[iv.length+i] = data[i];
    return data;
}
 
開發者ID:rctl,項目名稱:CryptoVoice,代碼行數:14,代碼來源:Call.java

示例7: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * Decrypt Text
 */
public static String decrypt(String encryptedText) {
    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] cipherText = Base64.getDecoder().decode(encryptedText.getBytes("UTF8"));
        String decryptedString = new String(cipher.doFinal(cipherText), "UTF-8");
        return decryptedString;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
開發者ID:GrigorisParaskevakos,項目名稱:BankAccount_PasaskevakosG_BootCamp3,代碼行數:17,代碼來源:AdvancedEncryptionStandard.java

示例8: s_encrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * 加密
 * 
 * @param _key
 *            密鑰
 * @param _dat
 *            待加密數據
 * @return
 * @throws Exception
 */
public static byte[] s_encrypt( byte[] _key , byte[] _dat ) throws Exception {

	// 還原密鑰
	Key k = _s_to_key( _key );

	// 實例化
	Cipher cipher = Cipher.getInstance( ALGORITHM );

	// 初始化,設置為加密模式
	cipher.init( Cipher.ENCRYPT_MODE , k );

	// 執行操作
	return cipher.doFinal( _dat );
}
 
開發者ID:aiyoyoyo,項目名稱:jeesupport,代碼行數:25,代碼來源:DESUtils.java

示例9: encode

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * 加密
 *
 * @param plainText 要加密文字
 * @return 加密文字
 * @throws Exception
 */
public static String encode(String plainText) throws Exception {
    if (TextUtils.isEmpty(secretKey) || TextUtils.isEmpty(iv))
        throw new NullPointerException("u should init first");
    SecretKey deskey = null;
    DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);
    Cipher cipher = Cipher.getInstance(DESEDE_CBC_PKCS5_PADDING);
    IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
    cipher.init(1, deskey, ips);
    byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
    return Base64Util.encode(encryptData);
}
 
開發者ID:ronghao,項目名稱:CacheManage,代碼行數:21,代碼來源:Des3Util.java

示例10: encrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static byte[] encrypt(byte[] key, byte[] text) throws Exception {      
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");      
    Cipher cipher = Cipher.getInstance("AES");      
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);      
    byte[] encrypted = cipher.doFinal(text);      
    return encrypted;      
}
 
開發者ID:blxble,項目名稱:mesh-core-on-android,代碼行數:8,代碼來源:AES128.java

示例11: aesEncryptToBytes

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * AES加密
 * @param content 待加密的內容
 * @param encryptKey 加密密鑰
 * @return 加密後的byte[]
 * @throws Exception
 */
public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(encryptKey.getBytes()));

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));

    return cipher.doFinal(content.getBytes("utf-8"));
}
 
開發者ID:mrjiangyan,項目名稱:BreakfastServer,代碼行數:17,代碼來源:EncodeUtils.java

示例12: encryptData

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * 用公鑰加密 <br>
 * 每次加密的字節數,不能超過密鑰的長度值減去11
 *
 * @param data 需加密數據的byte數據
 * @param publicKey 公鑰
 * @return 加密後的byte型數據
 */
public static byte[] encryptData(byte[] data, PublicKey publicKey) {
    try {
        Cipher cipher = Cipher.getInstance(RSA);
        // 編碼前設定編碼方式及密鑰
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        // 傳入編碼數據並返回編碼結果
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
開發者ID:imliujun,項目名稱:LJFramework,代碼行數:21,代碼來源:RSAUtils.java

示例13: combination_14

import javax.crypto.Cipher; //導入方法依賴的package包/類
private void combination_14(List<byte[]> results, int mode, byte[] AAD,
        byte[] plainText, AlgorithmParameters params) throws Exception {
    Cipher c = createCipher(mode, params);
    // prepare ByteBuffer to test
    ByteBuffer buf = ByteBuffer.allocateDirect(AAD.length);
    buf.put(AAD);

    // process the first half of AAD data
    buf.position(0);
    buf.limit(AAD.length / 2);
    c.updateAAD(buf);

    // process the rest of AAD data
    buf.limit(AAD.length);
    c.updateAAD(buf);

    // prepare buffers to encrypt/decrypt
    ByteBuffer in = ByteBuffer.allocate(plainText.length);
    in.put(plainText);
    in.position(0);
    in.limit(plainText.length);
    ByteBuffer out = ByteBuffer.allocate(c.getOutputSize(in.limit()));
    out.position(0);
    out.limit(c.getOutputSize(in.limit()));

    // process input text
    c.update(in, out);
    c.doFinal(in, out);
    int resultSize = out.position();
    byte[] result14 = new byte[resultSize];
    out.position(0);
    out.limit(resultSize);
    out.get(result14, 0, resultSize);
    results.add(result14);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:36,代碼來源:Encrypt.java

示例14: decrypt

import javax.crypto.Cipher; //導入方法依賴的package包/類
public static String decrypt(String data) throws Exception {
    Key deskey = keyGenerator(desKey);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    //初始化Cipher對象,設置為解密模式
    IvParameterSpec iv = new IvParameterSpec(DES_IV);
    AlgorithmParameterSpec paramSpec = iv;        
    cipher.init(Cipher.DECRYPT_MODE, deskey,iv);
    // 執行解密操作
    return new String(cipher.doFinal(Base64.decodeBase64(data)));
}
 
開發者ID:jeffreyning,項目名稱:nh-micro,代碼行數:11,代碼來源:DESUtil.java

示例15: encryptCTS

import javax.crypto.Cipher; //導入方法依賴的package包/類
/**
 * Encrypt AES in CBC-CTS mode using derived keys.
 */
private byte[] encryptCTS(byte[] baseKey, int usage, byte[] ivec,
    byte[] new_ivec, byte[] plaintext, int start, int len,
    boolean confounder_exists)
    throws GeneralSecurityException, KrbCryptoException {

    byte[] Ke = null;
    byte[] Ki = null;

    if (debug) {
        System.err.println("usage: " + usage);
        if (ivec != null) {
            traceOutput("old_state.ivec", ivec, 0, ivec.length);
        }
        traceOutput("plaintext", plaintext, start, Math.min(len, 32));
        traceOutput("baseKey", baseKey, 0, baseKey.length);
    }

    try {
        // derive Encryption key
        byte[] constant = new byte[5];
        constant[0] = (byte) ((usage>>24)&0xff);
        constant[1] = (byte) ((usage>>16)&0xff);
        constant[2] = (byte) ((usage>>8)&0xff);
        constant[3] = (byte) (usage&0xff);
        constant[4] = (byte) 0xaa;
        Ke = dk(baseKey, constant);  // Encryption key

        byte[] toBeEncrypted = null;
        if (confounder_exists) {
            byte[] confounder = Confounder.bytes(BLOCK_SIZE);
            toBeEncrypted = new byte[confounder.length + len];
            System.arraycopy(confounder, 0, toBeEncrypted,
                                    0, confounder.length);
            System.arraycopy(plaintext, start, toBeEncrypted,
                                    confounder.length, len);
        } else {
            toBeEncrypted = new byte[len];
            System.arraycopy(plaintext, start, toBeEncrypted, 0, len);
        }

        // encryptedData + HMAC
        byte[] output = new byte[toBeEncrypted.length + hashSize];

        // AES in JCE
        Cipher cipher = Cipher.getInstance("AES/CTS/NoPadding");
        SecretKeySpec secretKey = new SecretKeySpec(Ke, "AES");
        IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, encIv);
        cipher.doFinal(toBeEncrypted, 0, toBeEncrypted.length, output);

        // Derive integrity key
        constant[4] = (byte) 0x55;
        Ki = dk(baseKey, constant);
        if (debug) {
            traceOutput("constant", constant, 0, constant.length);
            traceOutput("Ki", Ki, 0, Ke.length);
        }

        // Generate checksum
        // H1 = HMAC(Ki, conf | plaintext | pad)
        byte[] hmac = getHmac(Ki, toBeEncrypted);

        // encryptedData + HMAC
        System.arraycopy(hmac, 0, output, toBeEncrypted.length,
                            hmac.length);
        return output;
    } finally {
        if (Ke != null) {
            Arrays.fill(Ke, 0, Ke.length, (byte) 0);
        }
        if (Ki != null) {
            Arrays.fill(Ki, 0, Ki.length, (byte) 0);
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:79,代碼來源:AesDkCrypto.java


注:本文中的javax.crypto.Cipher.doFinal方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。