本文整理汇总了Java中javax.crypto.CipherOutputStream.close方法的典型用法代码示例。如果您正苦于以下问题:Java CipherOutputStream.close方法的具体用法?Java CipherOutputStream.close怎么用?Java CipherOutputStream.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.crypto.CipherOutputStream
的用法示例。
在下文中一共展示了CipherOutputStream.close方法的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);
}
示例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);
}
}
示例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: 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;
}
示例6: 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;
}
}
示例7: 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
}
}
}
示例8: 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()));
}
示例9: write
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
public void write (Kryo kryo, Output output, Object object) {
Cipher cipher = getCipher(Cipher.ENCRYPT_MODE);
CipherOutputStream cipherStream = new CipherOutputStream(output, cipher);
Output cipherOutput = new Output(cipherStream, 256) {
public void close () throws KryoException {
// Don't allow the CipherOutputStream to close the output.
}
};
serializer.write(kryo, cipherOutput, object);
cipherOutput.flush();
try {
cipherStream.close();
} catch (IOException ex) {
throw new KryoException(ex);
}
}
示例10: write
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
public void write (Kryo kryo, Output output, Object object) {
Cipher cipher = getCipher(Cipher.ENCRYPT_MODE);
CipherOutputStream cipherStream = new CipherOutputStream(output, cipher);
Output cipherOutput = new Output(cipherStream, 256) {
public void close () throws KryoException {
// Don't allow the CipherOutputStream to close the output.
}
};
kryo.writeObject(cipherOutput, object, serializer);
cipherOutput.flush();
try {
cipherStream.close();
} catch (IOException ex) {
throw new KryoException(ex);
}
}
示例11: encryptString
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
private byte[] encryptString(Key key, String service, String value) throws CryptoFailedException {
try {
Cipher cipher = Cipher.getInstance(ENCRYPTION_TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, key);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// write initialization vector to the beginning of the stream
byte[] iv = cipher.getIV();
outputStream.write(iv, 0, iv.length);
// encrypt the value using a CipherOutputStream
CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
cipherOutputStream.write(value.getBytes("UTF-8"));
cipherOutputStream.close();
return outputStream.toByteArray();
} catch (Exception e) {
throw new CryptoFailedException("Could not encrypt value for service " + service, e);
}
}
示例12: testClose
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
/**
* close() method testing. Tests that the method calls the close() method of
* the underlying input stream.
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Can not check IOException.",
method = "close",
args = {}
)
public void testClose() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestOutputStream tos = new TestOutputStream();
CipherOutputStream cos = new CipherOutputStream(tos){};
cos.write(data);
cos.close();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream did not flush the data.");
}
assertTrue("The close() method should call the close() method "
+ "of its underlying output stream.", tos.wasClosed());
}
示例13: encryptAESKey
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
/**
* Encrypts randomly generated AES key using RSA public key
*/
private byte[] encryptAESKey(byte[] secretKey) throws GeneralSecurityException, IOException {
KeyStore keyStore = KeyStore.getInstance(KEY_STORE_ANDROID);
keyStore.load(null);
KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry)
keyStore.getEntry(KEY_ALIAS_AMAZE, null);
Cipher cipher = Cipher.getInstance(ALGO_RSA, "AndroidOpenSSL");
cipher.init(Cipher.ENCRYPT_MODE, keyEntry.getCertificate().getPublicKey());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
CipherOutputStream outputStream = new CipherOutputStream(byteArrayOutputStream, cipher);
outputStream.write(secretKey);
outputStream.close();
return byteArrayOutputStream.toByteArray();
}
示例14: aesDecrypt
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
@Override
public byte[] aesDecrypt(byte[] data, int offset, int length, byte[] key,
byte[] initVector) throws CryptoException {
try {
SecretKeySpec scs = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding", "ZBC");
IvParameterSpec iv = new IvParameterSpec(initVector);
ByteArrayOutputStream baos = new ByteArrayOutputStream(length);
cipher.init(Cipher.DECRYPT_MODE, scs, iv);
CipherOutputStream out = new CipherOutputStream(baos, cipher);
out.write(data, offset, length);
out.close();
baos.close();
return baos.toByteArray();
} catch (Exception e) {
throw new CryptoException(e);
}
}
示例15: aesEncrypt
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
@Override
public byte[] aesEncrypt(byte[] data, byte[] key, byte[] initVector)
throws CryptoException {
try {
SecretKeySpec scs = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding", "ZBC");
IvParameterSpec iv = new IvParameterSpec(initVector);
cipher.init(Cipher.ENCRYPT_MODE, scs, iv);
ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length);
CipherOutputStream out = new CipherOutputStream(baos, cipher);
out.write(data, 0, data.length);
out.close();
baos.close();
return baos.toByteArray();
} catch (Exception e) {
throw new CryptoException(e);
}
}