Skip to content

Instantly share code, notes, and snippets.

@moagrius
Created April 13, 2017 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moagrius/6d6d520da659b9adf02b986a5d6ce2a5 to your computer and use it in GitHub Desktop.
Save moagrius/6d6d520da659b9adf02b986a5d6ce2a5 to your computer and use it in GitHub Desktop.
package com.qozix.crypto;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.SecureRandom;
public class BlockTests3 {
private byte[] mKey = new byte[16];
private byte[] mIv = new byte[16];
{
SecureRandom secureRandom = new SecureRandom();
secureRandom.nextBytes(mIv);
secureRandom.nextBytes(mKey);
}
private byte[] mMessage = new byte[50];
{
for(int i = 0; i < mMessage.length; i++) {
mMessage[i] = (byte) (i + 1);
}
}
SecretKeySpec mKeySpec;
IvParameterSpec mIvSpec;
private Cipher mEncryptionCipher;
private Cipher mDecryptionCipher;
public BlockTests3() throws Exception {
mKeySpec = new SecretKeySpec(mKey, "AES");
mIvSpec = new IvParameterSpec(mIv);
mEncryptionCipher = Cipher.getInstance("AES/CTR/NoPadding");
mDecryptionCipher = Cipher.getInstance("AES/CTR/NoPAdding");
mEncryptionCipher.init(Cipher.ENCRYPT_MODE, mKeySpec, mIvSpec);
mDecryptionCipher.init(Cipher.DECRYPT_MODE, mKeySpec, mIvSpec);
System.out.println("clear");
printBytes(mMessage);
System.out.println("encryptMessage");
mMessage = encryptMessage(mMessage);
printBytes(mMessage);
System.out.println("decryptMessage");
mMessage = decryptMessage(mMessage);
printBytes(mMessage);
System.out.println();
System.out.println("Done.");
}
private byte[] encryptMessage(byte[] input) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(input);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(byteArrayOutputStream, mEncryptionCipher);
int size = byteArrayInputStream.available();
int bytesRead;
byte[] buffer = new byte[mEncryptionCipher.getBlockSize()];
while((bytesRead = byteArrayInputStream.read(buffer, 0, buffer.length)) > -1) {
cipherOutputStream.write(buffer, 0, Math.min(buffer.length, bytesRead));
}
return byteArrayOutputStream.toByteArray();
}
private byte[] decryptMessage(byte[] encrypted) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encrypted);
CipherInputStream cipherInputStream = new CipherInputStream(byteArrayInputStream, mDecryptionCipher);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int bytesRead;
byte[] buffer = new byte[mDecryptionCipher.getBlockSize()];
while((bytesRead = cipherInputStream.read(buffer, 0, buffer.length)) > -1) {
byteArrayOutputStream.write(buffer, 0, Math.min(bytesRead, buffer.length));
}
return byteArrayOutputStream.toByteArray();
}
private void printBytes(byte[] bytes) {
for(byte b : bytes) {
System.out.print(printByte(b) + ",");
}
System.out.println();
}
private String printByte(byte b) {
int intValue = (int) b;
String stringValue = String.valueOf(intValue);
while(stringValue.length() < 4) {
stringValue = " " + stringValue;
}
return stringValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment