当前位置: 技术问答>java相关
tea 加密难题
来源: 互联网 发布时间:2015-07-12
本文导语: 如何用java编写tea加密算法加密字节数组后能在c语言中解密 数据的格式:密钥(16字节)+加密后的数据 c语言一方,按4字节取出密钥并强制类型转换成long 再每8个字节解密 问题在于java与c语言的字节序不同,在加密过程...
如何用java编写tea加密算法加密字节数组后能在c语言中解密
数据的格式:密钥(16字节)+加密后的数据
c语言一方,按4字节取出密钥并强制类型转换成long 再每8个字节解密
问题在于java与c语言的字节序不同,在加密过程中应如何变化?
数据的格式:密钥(16字节)+加密后的数据
c语言一方,按4字节取出密钥并强制类型转换成long 再每8个字节解密
问题在于java与c语言的字节序不同,在加密过程中应如何变化?
|
为了测试,我写了个测试的 main()
void encipher( unsigned long * const v, const unsigned long * const k );
void decipher( unsigned long * const v, const unsigned long * const k );
int main(int argc, char* argv[])
{
unsigned long v1 [2] = {100, 100};
unsigned long k1 [4] = {100, 200, 300, 400};
unsigned long* const vs = v1;
const unsigned long* const ks = k1;
encipher(vs, ks);
printf("%d, %d n", v1 [0] , v1 [1] );
decipher(vs, ks);
printf("%d, %d n", v1 [0] , v1 [1] );
return 0;
}
void encipher( unsigned long * const v, const unsigned long * const k );
void decipher( unsigned long * const v, const unsigned long * const k );
int main(int argc, char* argv[])
{
unsigned long v1 [2] = {100, 100};
unsigned long k1 [4] = {100, 200, 300, 400};
unsigned long* const vs = v1;
const unsigned long* const ks = k1;
encipher(vs, ks);
printf("%d, %d n", v1 [0] , v1 [1] );
decipher(vs, ks);
printf("%d, %d n", v1 [0] , v1 [1] );
return 0;
}
|
对应测试的 java 代码:
public class Tea {
public Tea() {
}
public static void main(String argv[])
{
int v1 [] = {100, 100};
int k1 [] = {100, 200, 300, 400};
encipher(v1, k1);
System.out.println(" " + v1 [0] + " " + v1 [1] );
decipher(v1, k1);
System.out.println(" " + v1 [0] + " " + v1 [1] );
}
static void encipher( int [] v, int [] k )
{
int y = v[0], z = v[1];
int sum = 0, delta = 0x9E3779B9;
int a = k[0], b = k[1], c = k[2], d = k[3], n = 32;
while ( n-- > 0 )
{
sum += delta;
y += ( z > 5 ) + b;
z += ( y > 5 ) + d;
}
v[0] = y;
v[1] = z;
}
static void decipher( int [] v, int [] k )
{
int y = v[0], z = v[1];
int sum = 0xC6EF3720, delta = 0x9E3779B9;
int a = k[0], b = k[1], c = k[2], d = k[3], n = 32;
// sum = delta 5 ) + d;
y -= ( z > 5 ) + b;
sum -= delta;
}
v[0] = y;
v[1] = z;
}
}
public class Tea {
public Tea() {
}
public static void main(String argv[])
{
int v1 [] = {100, 100};
int k1 [] = {100, 200, 300, 400};
encipher(v1, k1);
System.out.println(" " + v1 [0] + " " + v1 [1] );
decipher(v1, k1);
System.out.println(" " + v1 [0] + " " + v1 [1] );
}
static void encipher( int [] v, int [] k )
{
int y = v[0], z = v[1];
int sum = 0, delta = 0x9E3779B9;
int a = k[0], b = k[1], c = k[2], d = k[3], n = 32;
while ( n-- > 0 )
{
sum += delta;
y += ( z > 5 ) + b;
z += ( y > 5 ) + d;
}
v[0] = y;
v[1] = z;
}
static void decipher( int [] v, int [] k )
{
int y = v[0], z = v[1];
int sum = 0xC6EF3720, delta = 0x9E3779B9;
int a = k[0], b = k[1], c = k[2], d = k[3], n = 32;
// sum = delta 5 ) + d;
y -= ( z > 5 ) + b;
sum -= delta;
}
v[0] = y;
v[1] = z;
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。