Skip to content

Instantly share code, notes, and snippets.

@wuyongzheng
Created July 17, 2014 07:36
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 wuyongzheng/5b4b8babc63350742283 to your computer and use it in GitHub Desktop.
Save wuyongzheng/5b4b8babc63350742283 to your computer and use it in GitHub Desktop.
CFBTest
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
public class CFBTest
{
final protected static char[] hexArray = "0123456789abcdef".toCharArray();
public static String bytesToHex(byte[] bytes, int offset, int size) {
char[] hexChars = new char[size * 2];
for ( int j = 0; j < size; j++ ) {
int v = bytes[offset+j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static void runMode (String mode) throws Exception
{
System.out.println("Testing Mode " + mode);
byte [] keybytes = "1234567890123456".getBytes();
System.out.println("Key: " + bytesToHex(keybytes, 0, 16));
Cipher enc = Cipher.getInstance(mode);
enc.init(Cipher.ENCRYPT_MODE,
new SecretKeySpec(keybytes, "AES"),
new IvParameterSpec(new byte[128/8]));
Cipher dec = Cipher.getInstance(mode);
dec.init(Cipher.ENCRYPT_MODE,
new SecretKeySpec(keybytes, "AES"),
new IvParameterSpec(new byte[128/8]));
int [] sizes = new int [] {3,2,16,17};
byte [] inbuf = "abcdefghijklmnopqrstuvwxyz".getBytes();
byte [] buf1 = new byte [26];
byte [] buf2 = new byte [26];
for (int size : sizes) {
System.out.println("plain: " + bytesToHex(inbuf, 0, size));
int retsize = enc.update(inbuf, 0, size, buf1);
System.out.println("enc: " + bytesToHex(buf1, 0, retsize));
retsize = dec.update(buf1, 0, retsize, buf2);
System.out.println("dec: " + bytesToHex(buf2, 0, retsize));
}
System.out.println();
}
public static void main (String [] args) throws Exception
{
runMode("AES/CTR/NoPadding");
runMode("AES/CFB/NoPadding");
runMode("AES/OFB/NoPadding");
}
}
Testing Mode AES/CTR/NoPadding
Key: 31323334353637383930313233343536
plain: 616263
enc: b9d7fb
dec: 616263
plain: 6162
enc: 29a5
dec: 6162
plain: 6162636465666768696a6b6c6d6e6f70
enc: 066ef7d6fe32b55ff74411db5e61d8b9
dec: 6162636465666768696a6b6c6d6e6f70
plain: 6162636465666768696a6b6c6d6e6f7071
enc: 36e09f590281a4ce0e42b1cc99317f0628
dec: 6162636465666768696a6b6c6d6e6f7071
Testing Mode AES/CFB/NoPadding
Key: 31323334353637383930313233343536
plain: 616263
enc:
dec:
plain: 6162
enc:
dec:
plain: 6162636465666768696a6b6c6d6e6f70
enc: b9d7fb29a5066ef7d6fe32b55ff74411
dec: 61626361626162636465666768696a6b
plain: 6162636465666768696a6b6c6d6e6f7071
enc: e474ad5ef59d279526b95852fe021787
dec: 3679fbe7f172d5c42ca6eed243a9bdf4
Testing Mode AES/OFB/NoPadding
Key: 31323334353637383930313233343536
plain: 616263
enc:
dec:
plain: 6162
enc:
dec:
plain: 6162636465666768696a6b6c6d6e6f70
enc: b9d7fb29a5066ef7d6fe32b55ff74411
dec: 61626361626162636465666768696a6b
plain: 6162636465666768696a6b6c6d6e6f7071
enc: de50408811d0e22b8fd41caf5d1a8894
dec: 6c6d6e6f706162636465666768696a6b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment