本文整理汇总了Java中org.apache.commons.crypto.stream.CryptoOutputStream类的典型用法代码示例。如果您正苦于以下问题:Java CryptoOutputStream类的具体用法?Java CryptoOutputStream怎么用?Java CryptoOutputStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CryptoOutputStream类属于org.apache.commons.crypto.stream包,在下文中一共展示了CryptoOutputStream类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.apache.commons.crypto.stream.CryptoOutputStream; //导入依赖的package包/类
public static void main(String []args) throws IOException {
final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"),"AES");
final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));
Properties properties = new Properties();
final String transform = "AES/CBC/PKCS5Padding";
String input = "hello world!";
//Encryption with CryptoOutputStream.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (CryptoOutputStream cos = new CryptoOutputStream(transform, properties, outputStream, key, iv)) {
cos.write(getUTF8Bytes(input));
cos.flush();
}
// The encrypted data:
System.out.println("Encrypted: "+Arrays.toString(outputStream.toByteArray()));
// Decryption with CryptoInputStream.
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
try (CryptoInputStream cis = new CryptoInputStream(transform, properties, inputStream, key, iv)) {
byte[] decryptedData = new byte[1024];
int decryptedLen = 0;
int i;
while ((i = cis.read(decryptedData, decryptedLen, decryptedData.length - decryptedLen)) > -1) {
decryptedLen += i;
}
System.out.println("Decrypted: "+new String(decryptedData, 0, decryptedLen, StandardCharsets.UTF_8));
}
}
示例2: createEncryptionStream
import org.apache.commons.crypto.stream.CryptoOutputStream; //导入依赖的package包/类
@Override
public OutputStream createEncryptionStream(OutputStream out) {
if (!initialized) {
reset();
}
try {
return new CryptoOutputStream(cipherMode, properties, out, key, new
IvParameterSpec(iv));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例3: getCryptOut
import org.apache.commons.crypto.stream.CryptoOutputStream; //导入依赖的package包/类
public OutputStream getCryptOut(final OutputStream out, final KeyIv keyIv)
throws IOException {
return new CryptoOutputStream(TRANSFORM, new Properties(), out,
new SecretKeySpec(keyIv.getKey(), "AES"), new IvParameterSpec(keyIv.getIv()));
}