blob: 0b54e43fbf7701e8936921d579a5d416c56ebefe [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.crypto.examples;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Properties;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.utils.AES;
import org.apache.commons.crypto.utils.Utils;
/**
* Example showing the CryptoCipher API using a ByteBuffer
*/
public class CipherByteBufferExample {
/**
* Converts ByteBuffer to String
*
* @param buffer input byte buffer
* @return the converted string
*/
private static String asString(final ByteBuffer buffer) {
final ByteBuffer copy = buffer.duplicate();
final byte[] bytes = new byte[copy.remaining()];
copy.get(bytes);
return new String(bytes, StandardCharsets.UTF_8);
}
/**
* Converts String to UTF8 bytes
*
* @param input the input string
* @return UTF8 bytes
*/
private static byte[] getUTF8Bytes(final String input) {
return input.getBytes(StandardCharsets.UTF_8);
}
public static void main(final String[] args) throws Exception {
final SecretKeySpec key = AES.newSecretKeySpec(getUTF8Bytes("1234567890123456"));
final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));
final Properties properties = new Properties();
//Creates a CryptoCipher instance with the transformation and properties.
final String transform = AES.CBC_PKCS5_PADDING;
final ByteBuffer outBuffer;
final int bufferSize = 1024;
final int updateBytes;
final int finalBytes;
try (CryptoCipher encipher = Utils.getCipherInstance(transform, properties)) {
final ByteBuffer inBuffer = ByteBuffer.allocateDirect(bufferSize);
outBuffer = ByteBuffer.allocateDirect(bufferSize);
inBuffer.put(getUTF8Bytes("hello world!"));
inBuffer.flip(); // ready for the cipher to read it
// Show the data is there
System.out.println("inBuffer=" + asString(inBuffer));
// Initializes the cipher with ENCRYPT_MODE, key and iv.
encipher.init(Cipher.ENCRYPT_MODE, key, iv);
// Continues a multiple-part encryption/decryption operation for byte buffer.
updateBytes = encipher.update(inBuffer, outBuffer);
System.out.println(updateBytes);
// We should call do final at the end of encryption/decryption.
finalBytes = encipher.doFinal(inBuffer, outBuffer);
System.out.println(finalBytes);
}
outBuffer.flip(); // ready for use as decrypt
final byte [] encoded = new byte[updateBytes + finalBytes];
outBuffer.duplicate().get(encoded);
System.out.println(Arrays.toString(encoded));
// Now reverse the process
try (CryptoCipher decipher = Utils.getCipherInstance(transform, properties)) {
decipher.init(Cipher.DECRYPT_MODE, key, iv);
final ByteBuffer decoded = ByteBuffer.allocateDirect(bufferSize);
decipher.update(outBuffer, decoded);
decipher.doFinal(outBuffer, decoded);
decoded.flip(); // ready for use
System.out.println("decoded="+asString(decoded));
}
}
}