/** * Creates new {@link Threefish} encryptor. */ public static CryptoEngine threefish(String password) { final Threefish threefish = new Threefish(512); threefish.init(password, 0x1122334455667788L, 0xFF00FF00AABB9933L); return new CryptoEngine() { @Override public byte[] encryptString(final String input) { return threefish.encryptString(input); } @Override public String decryptString(final byte[] encryptedContent) { return threefish.decryptString(encryptedContent); } }; }
@Override public String decryptString(final byte[] encryptedContent) { return threefish.decryptString(encryptedContent); } };
@Override public byte[] encryptString(final String input) { return threefish.encryptString(input); }
/** * Encrypts a block. */ @Override public byte[] encryptBlock(final byte[] content, final int offset) { long[] contentBlock = bytesToLongs(content, offset, blockSizeInBytes); long[] encryptedBlock = new long[blockSize / Long.SIZE]; blockEncrypt(contentBlock, encryptedBlock); return longsToBytes(encryptedBlock); }
@Override public byte[] decryptBlock(final byte[] encryptedContent, final int offset) { long[] encryptedBlock = bytesToLongs(encryptedContent, offset, blockSizeInBytes); long[] decryptedBlock= new long[encryptedBlock.length]; blockDecrypt(encryptedBlock, decryptedBlock); return longsToBytes(decryptedBlock); }
@Test void testSimple() { String message = "Threefish!"; byte[] encrypted = threefish.encryptString(message); String message2 = threefish.decryptString(encrypted); assertEquals(message, message2); message = "Jodd was here!Jodd was here!Jodd was here!Jodd was here!Jodd was here!Jodd was here!Jodd was here!Jodd was here!Jodd was here!Jodd was here!Jodd was here!"; encrypted = threefish.encryptString(message); message2 = threefish.decryptString(encrypted); assertEquals(message, message2); }
/** * Initializes cipher in a simple way. */ public void init(final String keyMessage, final long tweak1, final long tweak2) { long[] tweak = new long[] {tweak1, tweak2}; byte[] key = new byte[blockSize / Byte.SIZE]; byte[] keyData = StringUtil.getBytes(keyMessage); System.arraycopy(keyData, 0, key, 0, key.length < keyData.length ? key.length : keyData.length); init(bytesToLongs(key), tweak); }
keySchedule(s); // calculate same keys y[1] = ed[j * 2 + 1]; demix(j, d - 1); keySchedule(0);
/** * Decrypts a string. */ public String decryptString(final byte[] encrypted) { return StringUtil.newString(decrypt(encrypted), StringPool.UTF_8); }
/** * Encrypts a string. */ public byte[] encryptString(final String plain) { return encrypt(StringUtil.getBytes(plain)); }
protected static long[] bytesToLongs(final byte[] ba) { return bytesToLongs(ba, 0, ba.length); } /**
/** * Encrypts a block. */ @Override public byte[] encryptBlock(final byte[] content, final int offset) { long[] contentBlock = bytesToLongs(content, offset, blockSizeInBytes); long[] encryptedBlock = new long[blockSize / Long.SIZE]; blockEncrypt(contentBlock, encryptedBlock); return longsToBytes(encryptedBlock); }
@Test void testLoop() { long reps = 10000; while (reps-- > 0) { String s = RandomString.get().randomAscii(MathUtil.randomInt(1, 1024)); byte[] encrypted = threefish.encryptString(s); String s2 = threefish.decryptString(encrypted); assertEquals(s, s2); } } }
@Override public byte[] decryptBlock(final byte[] encryptedContent, final int offset) { long[] encryptedBlock = bytesToLongs(encryptedContent, offset, blockSizeInBytes); long[] decryptedBlock= new long[encryptedBlock.length]; blockDecrypt(encryptedBlock, decryptedBlock); return longsToBytes(decryptedBlock); }
/** * Initializes cipher in a simple way. */ public void init(final String keyMessage, final long tweak1, final long tweak2) { long[] tweak = new long[] {tweak1, tweak2}; byte[] key = new byte[blockSize / Byte.SIZE]; byte[] keyData = StringUtil.getBytes(keyMessage); System.arraycopy(keyData, 0, key, 0, key.length < keyData.length ? key.length : keyData.length); init(bytesToLongs(key), tweak); }
keySchedule(s); // calculate same keys y[1] = ed[j * 2 + 1]; demix(j, d - 1); keySchedule(0);
/** * Decrypts a string. */ public String decryptString(final byte[] encrypted) { return StringUtil.newString(decrypt(encrypted), StringPool.UTF_8); }
/** * Encrypts a string. */ public byte[] encryptString(final String plain) { return encrypt(StringUtil.getBytes(plain)); }
protected static long[] bytesToLongs(final byte[] ba) { return bytesToLongs(ba, 0, ba.length); } /**