Quadcap Embeddable Database

com/quadcap/crypto/Tea.java

Go to the documentation of this file.
00001 package com.quadcap.crypto; 00002 00003 /* Copyright 2002 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.util.Random; 00042 00043 import java.nio.ByteBuffer; 00044 import java.nio.IntBuffer; 00045 00046 import com.quadcap.util.text.Text; 00047 import com.quadcap.util.Util; 00048 00049 /** 00050 * Implementation of Tiny Encryption Algorithm 00051 * 00052 * @author Stan Bailes 00053 */ 00054 public class Tea extends AbstractSymmetricKey implements SymmetricKey { 00055 static final int delta = 0x9E3779B9; 00056 int a, b, c, d; 00057 int[] v = new int[2]; 00058 00059 /** 00060 * Initialize key from serialized representation 00061 */ 00062 public void init(String s) { 00063 String[] vx = Text.extractN(s, "*:*:*:*:*"); 00064 a = Integer.parseInt(vx[1]); 00065 b = Integer.parseInt(vx[2]); 00066 c = Integer.parseInt(vx[3]); 00067 d = Integer.parseInt(vx[4]); 00068 } 00069 00070 public void init(byte[] k) { 00071 a = Util.integer(k, 0); 00072 b = Util.integer(k, 4); 00073 c = Util.integer(k, 8); 00074 d = Util.integer(k, 12); 00075 } 00076 00077 /** 00078 * Initialize: Create a random key 00079 */ 00080 public void init(Random r) { 00081 a = r.nextInt(); 00082 b = r.nextInt(); 00083 c = r.nextInt(); 00084 d = r.nextInt(); 00085 } 00086 00087 /** 00088 * Return the serialized form of the key 00089 */ 00090 public String toString() { 00091 return "TEA:" + a + ":" + b + ":" + c + ":" + d; 00092 } 00093 00094 /** 00095 * Encrypt a buffer (must be multiple of 8 bytes) 00096 */ 00097 public void encrypt(ByteBuffer m, ByteBuffer c) { 00098 while (m.position() < m.limit()) { 00099 v[0] = m.getInt(); 00100 v[1] = m.getInt(); 00101 encrypt(v); 00102 c.putInt(v[0]); 00103 c.putInt(v[1]); 00104 } 00105 } 00106 00107 /** 00108 * Decrypt a buffer (must be a multiple of 8 bytes) 00109 */ 00110 public void decrypt(ByteBuffer c, ByteBuffer m) { 00111 while ( c.position() < c.limit()) { 00112 v[0] = c.getInt(); 00113 v[1] = c.getInt(); 00114 decrypt(v); 00115 m.putInt(v[0]); 00116 m.putInt(v[1]); 00117 } 00118 } 00119 00120 public int getBlockSize() { return 8; } 00121 00122 final void encrypt(int[] v) { 00123 int y = v[0]; 00124 int z = v[1]; 00125 int sum = 0; 00126 00127 for (int n = 32; n-- > 0; ) { 00128 sum += delta; 00129 y += (z << 4) + a ^ z + sum ^ (z >>> 5) + b; 00130 z += (y << 4) + c ^ y + sum ^ (y >>> 5) + d; 00131 } 00132 v[0] = y; 00133 v[1] = z; 00134 } 00135 00136 final void decrypt(int[] v) { 00137 int y = v[0]; 00138 int z = v[1]; 00139 int sum = 0xC6EF3720; 00140 for (int n = 32; n-- > 0; ) { 00141 z -= (y << 4) + c ^ y + sum ^ (y >>> 5) + d; 00142 y -= (z << 4) + a ^ z + sum ^ (z >>> 5) + b; 00143 sum -= delta; 00144 } 00145 v[0] = y; 00146 v[1] = z; 00147 } 00148 }