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


Java CipherOutputStream類代碼示例

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


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

示例1: encrypt

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
/**
 * Encrypt the secret with RSA.
 *
 * @param secret the secret.
 * @return the encrypted secret.
 * @throws Exception
 */
public String encrypt(String secret) throws Exception {
    KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
    keyStore.load(null);
    KeyStore.PrivateKeyEntry privateKeyEntry =
        (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, null);

    Cipher inputCipher = Cipher.getInstance(RSA_ALGORITHM);
    inputCipher.init(Cipher.ENCRYPT_MODE, privateKeyEntry.getCertificate().getPublicKey());

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, inputCipher);
    cipherOutputStream.write(secret.getBytes());
    cipherOutputStream.close();

    return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}
 
開發者ID:drakeet,項目名稱:rebase-android,代碼行數:24,代碼來源:BlackBox.java

示例2: encryptMessage

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
@Nullable
static String encryptMessage(@NonNull String plainMessage) throws SecureStorageException {
    try {
        Cipher input;
        if (Build.VERSION.SDK_INT >= M) {
            input = Cipher.getInstance(KEY_TRANSFORMATION_ALGORITHM, KEY_CIPHER_MARSHMALLOW_PROVIDER);
        } else {
            input = Cipher.getInstance(KEY_TRANSFORMATION_ALGORITHM, KEY_CIPHER_JELLYBEAN_PROVIDER);
        }
        input.init(Cipher.ENCRYPT_MODE, getPublicKey());

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        CipherOutputStream cipherOutputStream = new CipherOutputStream(
                outputStream, input);
        cipherOutputStream.write(plainMessage.getBytes(KEY_CHARSET));
        cipherOutputStream.close();

        byte[] values = outputStream.toByteArray();
        return Base64.encodeToString(values, Base64.DEFAULT);

    } catch (Exception e) {
        throw new SecureStorageException(e.getMessage(), e, KEYSTORE_EXCEPTION);
    }
}
 
開發者ID:adorsys,項目名稱:secure-storage-android,代碼行數:25,代碼來源:KeystoreTool.java

示例3: EncryptActionPerformed

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
private void EncryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EncryptActionPerformed
 try{
            FileInputStream file = new FileInputStream(file_path.getText());
            FileOutputStream outStream = new FileOutputStream("Encrypt.jpg");
            byte k[]="CooL2116NiTh5252".getBytes();
            SecretKeySpec key = new SecretKeySpec(k, "AES");
            Cipher enc = Cipher.getInstance("AES");
            enc.init(Cipher.ENCRYPT_MODE, key);
            CipherOutputStream cos = new CipherOutputStream(outStream, enc);
            byte[] buf = new byte[1024];
            int read;
            while((read=file.read(buf))!=-1){
                cos.write(buf,0,read);
            }
            file.close();
            outStream.flush();
            cos.close();
            JOptionPane.showMessageDialog(null, "The file encrypted Successfully");
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }


        // TODO add your handling code here:
}
 
開發者ID:rohanpillai20,項目名稱:Web-Based-Graphical-Password-Authentication-System,代碼行數:26,代碼來源:ImgCry.java

示例4: DecryptActionPerformed

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
private void DecryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DecryptActionPerformed
try{
            FileInputStream file = new FileInputStream("C:\\Users\\SMP\\Documents\\NetBeansProjects\\Rohan\\Encrypt.jpg");
            FileOutputStream outStream = new FileOutputStream("Decrypt.jpg");
            byte k[]="CooL2116NiTh5252".getBytes();
            SecretKeySpec key = new SecretKeySpec(k, "AES");
            Cipher enc = Cipher.getInstance("AES");
            enc.init(Cipher.DECRYPT_MODE, key);
            CipherOutputStream cos = new CipherOutputStream(outStream, enc);
            byte[] buf = new byte[1024];
            int read;
            while((read=file.read(buf))!=-1){
                cos.write(buf,0,read);
            }
            file.close();
            outStream.flush();
            cos.close();
            JOptionPane.showMessageDialog(null, "The image was decrypted successfully");
            Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler "+"Decrypt.jpg");
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }


        // TODO add your handling code here:
}
 
開發者ID:rohanpillai20,項目名稱:Web-Based-Graphical-Password-Authentication-System,代碼行數:27,代碼來源:ImgCry.java

示例5: proceedTest

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
/**
 * The CICO PBE RW test specific part of the super.doTest(). Implements the
 * scenario in accordance to the class description.
 * @param type byteArrayBuffering or intByteBuffering
 * @throws IOException  any I/O operation failed.
 * @throws GeneralSecurityException any security error.
 */
@Override
public void proceedTest(String type) throws IOException,
        GeneralSecurityException {
    ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
    try (CipherOutputStream ciOutput = new CipherOutputStream(baOutput,
            getDecryptCipher())) {
        if (type.equals(CICO_PBE_Test.BYTE_ARR_BUFFER)) {
            proceedTestUsingByteArrayBuffer(ciOutput);
        } else {
            proceedTestUsingIntBuffer(ciOutput);
        }
        ciOutput.flush();
    }
    // Compare input and output
    if (!TestUtilities.equalsBlock(plainText, baOutput.toByteArray(), TEXT_SIZE)) {
        throw new RuntimeException("outputText not same with expectedText"
                + " when test " + type);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:27,代碼來源:CICO_PBE_RW_Test.java

示例6: decryptWithEmptyAAD

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
private void decryptWithEmptyAAD() throws Exception {
    System.out.println("decryptWithEmptyAAD() started");
    // initialize it with empty AAD to get exception during decryption
    Cipher decryptCipher = createCipher(Cipher.DECRYPT_MODE,
            encryptCipher.getParameters());
    try (ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
            CipherOutputStream ciOutput = new CipherOutputStream(baOutput,
                    decryptCipher)) {
        if (decrypt(ciOutput, baOutput)) {
            throw new RuntimeException(
                    "Decryption has been perfomed successfully in"
                            + " spite of the decrypt Cipher has NOT been"
                            + " initialized with AAD");
        }
    }
    System.out.println("decryptWithEmptyAAD() passed");
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:18,代碼來源:WrongAAD.java

示例7: decryptWithWrongAAD

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
private void decryptWithWrongAAD() throws Exception {
    System.out.println("decrypt with wrong AAD");

    // initialize it with wrong AAD to get an exception during decryption
    Cipher decryptCipher = createCipher(Cipher.DECRYPT_MODE,
            encryptCipher.getParameters());
    byte[] someAAD = Helper.generateBytes(AAD_SIZE + 1);
    decryptCipher.updateAAD(someAAD);

    // init output stream
    try (ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
            CipherOutputStream ciOutput = new CipherOutputStream(baOutput,
                    decryptCipher);) {
        if (decrypt(ciOutput, baOutput)) {
            throw new RuntimeException(
                    "A decryption has been perfomed successfully in"
                            + " spite of the decrypt Cipher has been"
                            + " initialized with fake AAD");
        }
    }

    System.out.println("Passed");
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:24,代碼來源:WrongAAD.java

示例8: runTest

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
@Override
public void runTest(BufferType bufType) throws IOException,
        GeneralSecurityException {

    ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
    try (CipherOutputStream ciOutput = new CipherOutputStream(baOutput,
            decryptCipher)) {
        if (bufType == BufferType.BYTE_ARRAY_BUFFERING) {
            doByteTest(ciOutput);
        } else {
            doIntTest(ciOutput);
        }
    }

    check(plaintext, baOutput.toByteArray());
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:17,代碼來源:ReadWriteSkip.java

示例9: streamEncrypt

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
public static byte[] streamEncrypt(String message, SecretKey key,
    MessageDigest digest)
    throws Exception {

    byte[] data;
    Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE, key);
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(bos, digest);
        CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
            oos.writeObject(message);
        }
        data = bos.toByteArray();
    }

    if (debug) {
        System.out.println(DatatypeConverter.printHexBinary(data));
    }
    return data;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:22,代碼來源:CipherStreamClose.java

示例10: encryptOrDecrypt

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
public void encryptOrDecrypt(String key, int mode, InputStream is,
		OutputStream os) throws Exception {

	DESKeySpec dks = new DESKeySpec(key.getBytes());
	SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
	SecretKey desKey = skf.generateSecret(dks);
	Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for
	// SunJCE

	if (mode == Cipher.ENCRYPT_MODE) {
		cipher.init(Cipher.ENCRYPT_MODE, desKey);
		CipherInputStream cis = new CipherInputStream(is, cipher);
		doCopy(cis, os);
	} else if (mode == Cipher.DECRYPT_MODE) {
		cipher.init(Cipher.DECRYPT_MODE, desKey);
		CipherOutputStream cos = new CipherOutputStream(os, cipher);
		doCopy(is, cos);
	}
}
 
開發者ID:cyberheartmi9,項目名稱:Mona-Secure-Multi-Owner-Data-Sharing-for-Dynamic-Group-in-the-Cloud,代碼行數:20,代碼來源:CipherExample.java

示例11: wrapOutputStream

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
/**
   * <p>Wraps an <code>OutputStream</code> with a <code>CipherOutputStream</code> using this encryptor's cipher.</p>
   * <p>If an <code>ivLength</code> has been specified or an explicit IV has been set during construction
   * and <code>prependIV</code> is set to <code>true</code> this method will write an IV to the <code>OutputStream</code> before wrapping it.</p>
   * @param os
   * @return
   * @throws GeneralSecurityException
   * @throws IOException
   */
  public CipherOutputStream wrapOutputStream(OutputStream os) throws GeneralSecurityException, IOException {
  	Cipher cipher = getCipher();
  	if(!generateIV) {
  		iv = null;
  	} else if(iv == null && ivLength > 0) {
  		generateIV();
      }
if(iv != null) {
	cipher.init(Cipher.DECRYPT_MODE, getKey(), getAlgorithmParameterSpec(iv));
} else {
	cipher.init(Cipher.DECRYPT_MODE, getKey());
	iv = cipher.getIV();
}
if(prependIV && iv != null) {
	os.write(iv);
}
 		return new CipherOutputStream(os, cipher);
  }
 
開發者ID:martinwithaar,項目名稱:Encryptor4j,代碼行數:28,代碼來源:Encryptor.java

示例12: encryptString

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
public String encryptString(String initialText) {
    try {

        KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, null);
        RSAPublicKey publicKey = (RSAPublicKey) privateKeyEntry.getCertificate().getPublicKey();

        Cipher inCipher = Cipher.getInstance(CIPHER_TRANSFORMATION, CIPHER_PROVIDER);
        inCipher.init(Cipher.ENCRYPT_MODE, publicKey);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, inCipher);
        cipherOutputStream.write(initialText.getBytes("UTF-8"));
        cipherOutputStream.close();

        byte[] vals = outputStream.toByteArray();

        String cipher = Base64.encodeToString(vals, Base64.DEFAULT);
        save(cipher);
        return cipher;

    } catch (Exception e) {
        Log.e(TAG, Log.getStackTraceString(e));
    }
    return null;
}
 
開發者ID:ivoribeiro,項目名稱:AndroidQuiz,代碼行數:26,代碼來源:SecurityManager.java

示例13: decrypt

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
public boolean decrypt(String file, String dest,Key key)  {

        try{
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            InputStream is = new FileInputStream(file);
            OutputStream out = new FileOutputStream(dest);
            CipherOutputStream cos = new CipherOutputStream(out, cipher);
            byte[] buffer = new byte[1024];
            int r;
            while ((r = is.read(buffer)) >= 0) {
                System.out.println();
                cos.write(buffer, 0, r);
            }
            cos.close();
            out.close();
            is.close();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }
 
開發者ID:tztztztztz,項目名稱:bookish-meme,代碼行數:25,代碼來源:FileEncryptAndDecrypt.java

示例14: testCorruptDecryptEmpty

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
@SuppressWarnings("InsecureCryptoUsage")
public void testCorruptDecryptEmpty(Iterable<TestVector> tests) throws Exception {
  for (TestVector t : tests) {
    Cipher cipher = Cipher.getInstance(t.algorithm);
    cipher.init(Cipher.DECRYPT_MODE, t.key, t.params);
    cipher.updateAAD(t.aad);
    byte[] ct = Arrays.copyOf(t.ct, t.ct.length);
    ct[ct.length - 1] ^= (byte) 1;
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    CipherOutputStream cos = new CipherOutputStream(os, cipher);
    cos.write(ct);
    try {
      // cos.close() should call cipher.doFinal().
      cos.close();
      byte[] decrypted = os.toByteArray();
      fail(
          "this should fail; decrypted:"
              + TestUtil.bytesToHex(decrypted)
              + " pt: "
              + TestUtil.bytesToHex(t.pt));
    } catch (IOException ex) {
      // expected
    }
  }
}
 
開發者ID:google,項目名稱:wycheproof,代碼行數:26,代碼來源:CipherOutputStreamTest.java

示例15: testInputStream

import javax.crypto.CipherOutputStream; //導入依賴的package包/類
@Test
public void testInputStream() throws Exception {
	AES aes = new AES();
	String orig = "I'm a password, really";
	ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());
	CipherInputStream cis = aes.inputStream(bais, true);
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	Symm.base64.encode(cis, baos);
	cis.close();

	byte[] b64encrypted;
	System.out.println(new String(b64encrypted=baos.toByteArray()));
	
	
	baos.reset();
	CipherOutputStream cos = aes.outputStream(baos, false);
	Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);
	cos.close();
	Assert.assertEquals(orig, new String(baos.toByteArray()));
}
 
開發者ID:att,項目名稱:AAF,代碼行數:21,代碼來源:JU_AES.java


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