本文整理汇总了Java中javax.crypto.CipherOutputStream.write方法的典型用法代码示例。如果您正苦于以下问题:Java CipherOutputStream.write方法的具体用法?Java CipherOutputStream.write怎么用?Java CipherOutputStream.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.crypto.CipherOutputStream
的用法示例。
在下文中一共展示了CipherOutputStream.write方法的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: 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
示例3: 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;
}
示例4: 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;
}
}
示例5: 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);
}
}
示例6: testCipherOutputStream
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
/**
* CipherOutputStream(OutputStream os) method testing. Tests that
* CipherOutputStream uses NullCipher if Cipher is not specified
* in the constructor.
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "CipherOutputStream",
args = {java.io.OutputStream.class}
)
public void testCipherOutputStream() 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.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("NullCipher should be used " + "if Cipher is not specified.");
}
}
示例7: testWrite1
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
/**
* write(int b) method testing. Tests that method writes correct values to
* the underlying output stream.
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Can not check IOException.",
method = "write",
args = {int.class}
)
public void testWrite1() 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, new NullCipher());
for (int i = 0; i < data.length; i++) {
cos.write(data[i]);
}
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream wrote incorrect data.");
}
}
示例8: testWrite2
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
/**
* write(byte[] b) method testing. Tests that method writes correct values
* to the underlying output stream.
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Can not check IOException.",
method = "write",
args = {byte[].class}
)
public void testWrite2() 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, new NullCipher());
cos.write(data);
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream wrote incorrect data.");
}
try {
cos.write(null);
fail("NullPointerException expected");
} catch (NullPointerException e) {
//expected
}
}
示例9: testWrite3
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
/**
* write(byte[] b, int off, int len) method testing.
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Can not check IOException.",
method = "write",
args = {byte[].class, int.class, int.class}
)
public void testWrite3() 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, new NullCipher());
for (int i = 0; i < data.length; i++) {
cos.write(data, i, 1);
}
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream wrote incorrect data.");
}
}
示例10: testWrite5
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
/**
* @tests write(byte[] b, int off, int len)
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Can not check IOException.",
method = "write",
args = {byte[].class, int.class, int.class}
)
public void testWrite5() throws Exception {
//Regression for HARMONY-758
Cipher cf = Cipher.getInstance("DES/CBC/PKCS5Padding");
NullCipher nc = new NullCipher();
CipherOutputStream stream1 = new CipherOutputStream(new BufferedOutputStream((OutputStream) null), nc);
CipherOutputStream stream2 = new CipherOutputStream(stream1, cf);
CipherOutputStream stream3 = new CipherOutputStream(stream2, nc);
stream3.write(new byte[] {0}, 0, 0);
//no exception expected
}
示例11: testFlush
import javax.crypto.CipherOutputStream; //导入方法依赖的package包/类
/**
* flush() method testing. Tests that method flushes the data to the
* underlying output stream.
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Can not check IOException.",
method = "flush",
args = {}
)
public void testFlush() 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.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream did not flush the data.");
}
}
示例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);
}
}