First of all, Im pretty new to codings. It’s been like 6 months so far so
my question could be something stupid. sorry if I get to confuse you.
SO, Im trying to send json formed string from JS(Node.js)server to Java Server.
The Java Server controlls DB and JS Server is trying to send certain promised strings to
take control for some functions in Java Server and DB.
Everythings are fine except for encrypting/decrypting part.
Java server is formed already and I need to follow its way to encrypt/decrypt.
So Im trying to import given Java module and use it on node.js using crypto-js, trying to
Encrypt it with AES using Java module for SEED.
I want to know how to import this Java code and use it on node.js
The JAVA module is really really long for me and maybe for you to read.
I’ll be glad if I get any clue how to import Java module from javascript.
i don’t expect you to read through the whole code but posting whole code just in case.
/**
@file KISA_SEED_ECB.java
@brief SEED CBC ��ȣ �˰�����
@author Copyright (c) 2013 by KISA
@remarks http://seed.kisa.or.kr/
*/
public class KISA_SEED_ECB {
// DEFAULT : JAVA = BIG_ENDIAN
private static int ENDIAN = Common.BIG_ENDIAN;
private static final int EndianChange(int dwS) { return ( (/*ROTL(dwS,8)*/(((dwS) << (8)) | (((dwS) >> (32-(8)))&0x000000ff)) & 0x00ff00ff) | (/*ROTL(dwS,24)*/(((dwS) << (24)) | (((dwS) >> (32-(24)))&0x00ffffff)) & 0xff00ff00) ); }
private static final int SS0[] = {
0x2989a1a8, 0x05858184, 0x16c6d2d4, 0x13c3d3d0, 0x14445054, 0x1d0d111c, 0x2c8ca0ac, 0x25052124,
0x1d4d515c, 0x03434340, 0x18081018, 0x1e0e121c, 0x11415150, 0x3cccf0fc, 0x0acac2c8, 0x23436360,
0x28082028, 0x04444044, 0x20002020, 0x1d8d919c, 0x20c0e0e0, 0x22c2e2e0, 0x08c8c0c8, 0x17071314,
0x2585a1a4, 0x0f8f838c, 0x03030300, 0x3b4b7378, 0x3b8bb3b8, 0x13031310, 0x12c2d2d0, 0x2ecee2ec,
0x30407070, 0x0c8c808c, 0x3f0f333c, 0x2888a0a8, 0x32023230, 0x1dcdd1dc, 0x36c6f2f4, 0x34447074,
0x2ccce0ec, 0x15859194, 0x0b0b0308, 0x17475354, 0x1c4c505c, 0x1b4b5358, 0x3d8db1bc, 0x01010100,
0x24042024, 0x1c0c101c, 0x33437370, 0x18889098, 0x10001010, 0x0cccc0cc, 0x32c2f2f0, 0x19c9d1d8,
0x2c0c202c, 0x27c7e3e4, 0x32427270, 0x03838380, 0x1b8b9398, 0x11c1d1d0, 0x06868284, 0x09c9c1c8,
0x20406060, 0x10405050, 0x2383a3a0, 0x2bcbe3e8, 0x0d0d010c, 0x3686b2b4, 0x1e8e929c, 0x0f4f434c,
0x3787b3b4, 0x1a4a5258, 0x06c6c2c4, 0x38487078, 0x2686a2a4, 0x12021210, 0x2f8fa3ac, 0x15c5d1d4,
0x21416160, 0x03c3c3c0, 0x3484b0b4, 0x01414140, 0x12425250, 0x3d4d717c, 0x0d8d818c, 0x08080008,
0x1f0f131c, 0x19899198, 0x00000000, 0x19091118, 0x04040004, 0x13435350, 0x37c7f3f4, 0x21c1e1e0,
0x3dcdf1fc, 0x36467274, 0x2f0f232c, 0x27072324, 0x3080b0b0, 0x0b8b8388, 0x0e0e020c, 0x2b8ba3a8,
0x2282a2a0, 0x2e4e626c, 0x13839390, 0x0d4d414c, 0x29496168, 0x3c4c707c, 0x09090108, 0x0a0a0208,
0x3f8fb3bc, 0x2fcfe3ec, 0x33c3f3f0, 0x05c5c1c4, 0x07878384, 0x14041014, 0x3ecef2fc, 0x24446064,
0x1eced2dc, 0x2e0e222c, 0x0b4b4348, 0x1a0a1218, 0x06060204, 0x21012120, 0x2b4b6368, 0x26466264,
0x02020200, 0x35c5f1f4, 0x12829290, 0x0a8a8288, 0x0c0c000c, 0x3383b3b0, 0x3e4e727c, 0x10c0d0d0,
0x3a4a7278, 0x07474344, 0x16869294, 0x25c5e1e4, 0x26062224, 0x00808080, 0x2d8da1ac, 0x1fcfd3dc,
0x2181a1a0, 0x30003030, 0x37073334, 0x2e8ea2ac, 0x36063234, 0x15051114, 0x22022220, 0x38083038,
0x34c4f0f4, 0x2787a3a4, 0x05454144, 0x0c4c404c, 0x01818180, 0x29c9e1e8, 0x04848084, 0x17879394,
0x35053134, 0x0bcbc3c8, 0x0ecec2cc, 0x3c0c303c, 0x31417170, 0x11011110, 0x07c7c3c4, 0x09898188,
0x35457174, 0x3bcbf3f8, 0x1acad2d8, 0x38c8f0f8, 0x14849094, 0x19495158, 0x02828280, 0x04c4c0c4,
0x3fcff3fc, 0x09494148, 0x39093138, 0x27476364, 0x00c0c0c0, 0x0fcfc3cc, 0x17c7d3d4, 0x3888b0b8,
0x0f0f030c, 0x0e8e828c, 0x02424240, 0x23032320, 0x11819190, 0x2c4c606c, 0x1bcbd3d8, 0x2484a0a4,
0x34043034, 0x31c1f1f0, 0x08484048, 0x02c2c2c0, 0x2f4f636c, 0x3d0d313c, 0x2d0d212c, 0x00404040,
0x3e8eb2bc, 0x3e0e323c, 0x3c8cb0bc, 0x01c1c1c0, 0x2a8aa2a8, 0x3a8ab2b8, 0x0e4e424c, 0x15455154,
0x3b0b3338, 0x1cccd0dc, 0x28486068, 0x3f4f737c, 0x1c8c909c, 0x18c8d0d8, 0x0a4a4248, 0x16465254,
0x37477374, 0x2080a0a0, 0x2dcde1ec, 0x06464244, 0x3585b1b4, 0x2b0b2328, 0x25456164, 0x3acaf2f8,
0x23c3e3e0, 0x3989b1b8, 0x3181b1b0, 0x1f8f939c, 0x1e4e525c, 0x39c9f1f8, 0x26c6e2e4, 0x3282b2b0,
0x31013130, 0x2acae2e8, 0x2d4d616c, 0x1f4f535c, 0x24c4e0e4, 0x30c0f0f0, 0x0dcdc1cc, 0x08888088,
0x16061214, 0x3a0a3238, 0x18485058, 0x14c4d0d4, 0x22426260, 0x29092128, 0x07070304, 0x33033330,
0x28c8e0e8, 0x1b0b1318, 0x05050104, 0x39497178, 0x10809090, 0x2a4a6268, 0x2a0a2228, 0x1a8a9298
};
private static final int SS1[] = {
0x38380830, 0xe828c8e0, 0x2c2d0d21, 0xa42686a2, 0xcc0fcfc3, 0xdc1eced2, 0xb03383b3, 0xb83888b0,
0xac2f8fa3, 0x60204060, 0x54154551, 0xc407c7c3, 0x44044440, 0x6c2f4f63, 0x682b4b63,
//.. skipping code due to length issue
};
private static final int SS2[] = {
0xa1a82989, 0x81840585, 0xd2d416c6, 0xd3d013c3, 0x50541444, 0x111c1d0d, 0xa0ac2c8c, 0x21242505,
0x515c1d4d, 0x43400343, 0x10181808, 0x121c1e0e, 0x51501141, 0xf0fc3ccc, 0xc2c80aca, //.. skipping code due to length issue
};
private static final int SS3[] = {
0x08303838, 0xc8e0e828, 0x0d212c2d, 0x86a2a426, 0xcfc3cc0f, 0xced2dc1e, 0x83b3b033, 0x88b0b838,
0x8fa3ac2f, 0x40606020, 0x45515415, 0xc7c3c407, 0x44404404, 0x4f636c2f, 0x4b63682b,
//.. skipping code due to length issue
};
private static final byte GetB0(int A) { return (byte)(A & 0x0ff); }
private static final byte GetB1(int A) { return (byte)((A>>8) & 0x0ff); }
private static final byte GetB2(int A) { return (byte)((A>>16) & 0x0ff); }
private static final byte GetB3(int A) { return (byte)((A>>24) & 0x0ff); }
// TODO : check
// Round function F and adding output of F to L.
// L0, L1 : left input values at each round
// R0, R1 : right input values at each round
// K : round keys at each round
private static final void SeedRound(int[] T, int LR[], int L0, int L1, int R0, int R1, int[] K, int K_offset) {
T[0] = LR[R0] ^ K[K_offset+0];
T[1] = LR[R1] ^ K[K_offset+1];
T[1] ^= T[0];
T[1] = SS0[GetB0(T[1])&0x0ff] ^ SS1[GetB1(T[1])&0x0ff] ^
SS2[GetB2(T[1])&0x0ff] ^ SS3[GetB3(T[1])&0x0ff];
T[0] += T[1];
T[0] = SS0[GetB0(T[0])&0x0ff] ^ SS1[GetB1(T[0])&0x0ff] ^
SS2[GetB2(T[0])&0x0ff] ^ SS3[GetB3(T[0])&0x0ff];
T[1] += T[0];
T[1] = SS0[GetB0(T[1])&0x0ff] ^ SS1[GetB1(T[1])&0x0ff] ^
SS2[GetB2(T[1])&0x0ff] ^ SS3[GetB3(T[1])&0x0ff];
T[0] += T[1];
LR[L0] ^= T[0]; LR[L1] ^= T[1];
}
private static final int LR_L0 = 0;
private static final int LR_L1 = 1;
private static final int LR_R0 = 2;
private static final int LR_R1 = 3;
private static final int BLOCK_SIZE_SEED = 16;
private static final int BLOCK_SIZE_SEED_INT = 4;
/********************************* Encryption *********************************/
private static void KISA_SEED_Encrypt_Block_forECB( int[] in, int in_offset, int[] out, int out_offset, KISA_SEED_KEY ks ) {
int LR[] = new int[4]; // Iuput/output values at each rounds
int T[] = new int[2]; // Temporary variables for round function F
int K[] = ks.key_data; // Pointer of round keys
// Set up input values for first round
LR[LR_L0] = in[in_offset+0];
LR[LR_L1] = in[in_offset+1];
LR[LR_R0] = in[in_offset+2];
LR[LR_R1] = in[in_offset+3];
// Reorder for big endian
// Because SEED use little endian order in default
if(Common.BIG_ENDIAN != ENDIAN) {
LR[LR_L0] = EndianChange(LR[LR_L0]);
LR[LR_L1] = EndianChange(LR[LR_L1]);
LR[LR_R0] = EndianChange(LR[LR_R0]);
LR[LR_R1] = EndianChange(LR[LR_R1]);
}
SeedRound(T, LR, LR_L0, LR_L1, LR_R0, LR_R1, K, 0); // Round 1
SeedRound(T, LR, LR_R0, LR_R1, LR_L0, LR_L1, K, 2); // Round 2
SeedRound(T, LR, LR_L0, LR_L1, LR_R0, LR_R1, K, 4); // Round 3
//.. skipping code due to length issue
SeedRound(T, LR, LR_R0, LR_R1, LR_L0, LR_L1, K, 26); // Round 14
SeedRound(T, LR, LR_L0, LR_L1, LR_R0, LR_R1, K, 28); // Round 15
SeedRound(T, LR, LR_R0, LR_R1, LR_L0, LR_L1, K, 30); // Round 16
if(Common.BIG_ENDIAN != ENDIAN) {
LR[LR_L0] = EndianChange(LR[LR_L0]);
LR[LR_L1] = EndianChange(LR[LR_L1]);
LR[LR_R0] = EndianChange(LR[LR_R0]);
LR[LR_R1] = EndianChange(LR[LR_R1]);
}
// Copying output values from last round to pbData
out[out_offset+0] = LR[LR_R0];
out[out_offset+1] = LR[LR_R1];
out[out_offset+2] = LR[LR_L0];
out[out_offset+3] = LR[LR_L1];
}
public static int[] chartoint32_for_SEED_ECB(byte[] in, int inLen) {
int[] data;
int len, i;
if(inLen % 4 > 0)
len = (inLen/4)+1;
else
len = (inLen/4);
data = new int[len];
for(i=0;i<len;i++)
{
Common.byte_to_int(data, i, in, i*4, ENDIAN);
}
return data;
}
public static byte[] int32tochar_for_SEED_ECB(int in[], int inLen) {
byte[] data;
int i;
data = new byte[inLen];
if(ENDIAN != Common.BIG_ENDIAN) {
for(i=0;i<inLen;i++)
{
data[i] = (byte)(in[i/4] >> ((i%4)*8));
}
} else {
for(i=0;i<inLen;i++)
{
data[i] = (byte)(in[i/4] >> ((3-(i%4))*8));
}
}
return data;
}
public static byte[] SEED_ECB_Encrypt(byte[] pbszUserKey, byte[] pbData, int offset, int length) {
KISA_SEED_INFO info = new KISA_SEED_INFO();
int[] outbuf;
int[] data;
byte[] cdata;
int outlen;
int nRetOutLeng[] = new int[] { 0 };
int nPaddingLeng[] = new int[] { 0 };
byte[] pbszPlainText = pbData;
int nPlainTextLen = length;
int nPlainTextPadding = (BLOCK_SIZE_SEED - (nPlainTextLen) % BLOCK_SIZE_SEED);
byte[] newpbszPlainText = new byte[nPlainTextLen + nPlainTextPadding];
Common.arraycopy(newpbszPlainText, pbszPlainText, nPlainTextLen);
byte[] pbszCipherText = new byte[nPlainTextLen + nPlainTextPadding];
SEED_ECB_init( info, KISA_ENC_DEC.KISA_ENCRYPT, pbszUserKey);
outlen =((newpbszPlainText.length/BLOCK_SIZE_SEED) )*BLOCK_SIZE_SEED_INT ;
outbuf = new int[outlen];
data = chartoint32_for_SEED_ECB(newpbszPlainText, nPlainTextLen);
SEED_ECB_Process( info, data, nPlainTextLen, outbuf, nRetOutLeng );
SEED_ECB_Close( info, outbuf, nRetOutLeng[0], nPaddingLeng );
cdata = int32tochar_for_SEED_ECB(outbuf, nRetOutLeng[0] + nPaddingLeng[0]);
Common.arraycopy(pbszCipherText, cdata, nRetOutLeng[0] + nPaddingLeng[0]);
data = null;
cdata = null;
outbuf = null;
return pbszCipherText;
}
/********************************* Decryption *********************************/
/**
@brief Data�� ���� Decrypt
@param pbData : ��ȣȭ�� ������ : 16 byte���� ��
@param pdwRoundKey : SeedRoundKey()�� ���� ������� Ű
*/
// Same as encrypt, except that round keys are applied in reverse order
public static void KISA_SEED_Decrypt_Block_forECB( int[] in, int in_offset, int[] out, int out_offset, KISA_SEED_KEY ks ) {
int LR[] = new int[4]; // Iuput/output values at each rounds
int T[] = new int[2]; // Temporary variables for round function F
int K[] = ks.key_data; // Pointer of round keys
// Set up input values for first round
LR[LR_L0] = in[in_offset+0];
LR[LR_L1] = in[in_offset+1];
LR[LR_R0] = in[in_offset+2];
LR[LR_R1] = in[in_offset+3];
// Reorder for big endian
if(Common.BIG_ENDIAN != ENDIAN) {
LR[LR_L0] = EndianChange(LR[LR_L0]);
LR[LR_L1] = EndianChange(LR[LR_L1]);
LR[LR_R0] = EndianChange(LR[LR_R0]);
LR[LR_R1] = EndianChange(LR[LR_R1]);
}
SeedRound(T, LR, LR_L0, LR_L1, LR_R0, LR_R1, K, 30); // Round 1
//.. skipping code due to length issue
SeedRound(T, LR, LR_R0, LR_R1, LR_L0, LR_L1, K, 0); // Round 16
if(Common.BIG_ENDIAN != ENDIAN) {
LR[LR_L0] = EndianChange(LR[LR_L0]);
LR[LR_L1] = EndianChange(LR[LR_L1]);
LR[LR_R0] = EndianChange(LR[LR_R0]);
LR[LR_R1] = EndianChange(LR[LR_R1]);
}
// Copy output values from last round to pbData
out[out_offset+0] = LR[LR_R0];
out[out_offset+1] = LR[LR_R1];
out[out_offset+2] = LR[LR_L0];
out[out_offset+3] = LR[LR_L1];
}
public static byte[] SEED_ECB_Decrypt(byte[] pbszUserKey, byte[] pbData, int offset, int length) {
KISA_SEED_INFO info = new KISA_SEED_INFO();
int[] outbuf;
int[] data;
byte[] cdata;
int outlen = 0;
int nRetOutLeng[] = new int[] { 0 };
int nPaddingLeng[] = new int[] { 0 };
byte[] pbszCipherText = pbData;
int nCipherTextLen = length;
if (nCipherTextLen % BLOCK_SIZE_SEED > 0)
{
byte[] result = null;
return result;
}
byte []newpbszCipherText = new byte[nCipherTextLen];
Common.arraycopy(newpbszCipherText, pbszCipherText, nCipherTextLen);
byte[] pbszPlainText = new byte[nCipherTextLen];
SEED_ECB_init( info, KISA_ENC_DEC.KISA_DECRYPT, pbszUserKey);
outlen = (nCipherTextLen/16)*4 ;
outbuf = new int[outlen];
data = chartoint32_for_SEED_ECB(newpbszCipherText, nCipherTextLen);
SEED_ECB_Process( info, data, nCipherTextLen, outbuf, nRetOutLeng );
if (SEED_ECB_Close( info, outbuf, nRetOutLeng[0], nPaddingLeng ) > 0 )
{
cdata = int32tochar_for_SEED_ECB( outbuf, nRetOutLeng[0] - nPaddingLeng[0] );
Common.arraycopy(pbszPlainText, cdata, nRetOutLeng[0] - nPaddingLeng[0]);
int message_length = nRetOutLeng[0] - nPaddingLeng[0];
if(message_length < 0)
{
message_length = 0;
}
byte[] result = new byte[message_length];
System.arraycopy(pbszPlainText, 0, result, 0, message_length);
data = null;
cdata = null;
outbuf = null;
return result;
}
else
{
byte[] result = null;
return result;
}
}
public static final class KISA_SEED_INFO {
public int encrypt;
public int ivec[] = new int[4];
public KISA_SEED_KEY seed_key = new KISA_SEED_KEY();
public int ecb_buffer[] = new int[4];
public int buffer_length;
public int[] ecb_last_block = new int[4];
public int last_block_flag;
public KISA_SEED_INFO() {
encrypt = 0;
ivec[0] = ivec[1] = ivec[2] = ivec[3] = 0;
seed_key.init();
ecb_buffer[0] = ecb_buffer[1] = ecb_buffer[2] = ecb_buffer[3] = 0;
buffer_length = 0;
ecb_last_block[0] = ecb_last_block[1] = ecb_last_block[2] = ecb_last_block[3] = 0;
last_block_flag = 0;
}
}
public static final class KISA_ENC_DEC {
public static final int _KISA_DECRYPT = 0;
public static final int _KISA_ENCRYPT = 1;
public int value;
public KISA_ENC_DEC(int value ) {
this.value = value;
}
public static final KISA_ENC_DEC KISA_ENCRYPT = new KISA_ENC_DEC(_KISA_ENCRYPT);
public static final KISA_ENC_DEC KISA_DECRYPT = new KISA_ENC_DEC(_KISA_DECRYPT);
}
public static final class KISA_SEED_KEY {
public int[] key_data = new int[32];
public void init() {
for(int i=0; i<key_data.length; i++) {
key_data[i] = 0;
}
}
}
public static int SEED_ECB_init( KISA_SEED_INFO pInfo, KISA_ENC_DEC enc, byte[] pbszUserKey) {
int ABCD[] = new int[4]; // Iuput/output values at each rounds(�� ���� ��/���)
int T[] = new int[2]; // Temporary variable
int K[];
if( null == pInfo ||
null == pbszUserKey)
return 0;
K = pInfo.seed_key.key_data; // Pointer of round keys
pInfo.encrypt = enc.value; //
pInfo.last_block_flag = pInfo.buffer_length = 0;
// Set up input values for Key Schedule
ABCD[ABCD_A] = Common.byte_to_int(pbszUserKey, 0*4, ENDIAN);
ABCD[ABCD_B] = Common.byte_to_int(pbszUserKey, 1*4, ENDIAN);
ABCD[ABCD_C] = Common.byte_to_int(pbszUserKey, 2*4, ENDIAN);
ABCD[ABCD_D] = Common.byte_to_int(pbszUserKey, 3*4, ENDIAN);
// Reorder for big endian
if(Common.BIG_ENDIAN != ENDIAN) {
ABCD[ABCD_A] = EndianChange(ABCD[ABCD_A]);
ABCD[ABCD_B] = EndianChange(ABCD[ABCD_B]);
ABCD[ABCD_C] = EndianChange(ABCD[ABCD_C]);
ABCD[ABCD_D] = EndianChange(ABCD[ABCD_D]);
}
// i-th round keys( K_i,0 and K_i,1 ) are denoted as K[2*(i-1)] and K[2*i-1], respectively
RoundKeyUpdate0(T, K, 0, ABCD, KC0 ); // K_1,0 and K_1,1
//.. skipping code due to length issue
RoundKeyUpdate0(T, K, 28, ABCD, KC14); // K_15,0 and K_15,1
T[0] = ABCD[ABCD_A] + ABCD[ABCD_C] - KC15;
T[1] = ABCD[ABCD_B] - ABCD[ABCD_D] + KC15;
K[30] = SS0[GetB0(T[0])&0x0ff] ^ SS1[GetB1(T[0])&0x0ff] ^ // K_16,0
SS2[GetB2(T[0])&0x0ff] ^ SS3[GetB3(T[0])&0x0ff];
K[31] = SS0[GetB0(T[1])&0x0ff] ^ SS1[GetB1(T[1])&0x0ff] ^ // K_16,1
SS2[GetB2(T[1])&0x0ff] ^ SS3[GetB3(T[1])&0x0ff];
return 1;
}
public static int SEED_ECB_Process( KISA_SEED_INFO pInfo, int[] in, int inLen, int[] out, int[] outLen ) {
int nCurrentCount = BLOCK_SIZE_SEED;
int in_offset = 0;
int out_offset = 0;
if( null == pInfo ||
null == in ||
null == out ||
0 > inLen )
return 0;
if( KISA_ENC_DEC._KISA_ENCRYPT == pInfo.encrypt ) {
in_offset = 0;
out_offset = 0;
while( nCurrentCount <= inLen )
{
KISA_SEED_Encrypt_Block_forECB( in, in_offset, out, out_offset, pInfo.seed_key );
nCurrentCount += BLOCK_SIZE_SEED;
in_offset += BLOCK_SIZE_SEED_INT;
out_offset += BLOCK_SIZE_SEED_INT;
}
outLen[0] = nCurrentCount - BLOCK_SIZE_SEED;
pInfo.buffer_length = inLen - outLen[0];
Common.memcpy( pInfo.ecb_buffer, in, in_offset, pInfo.buffer_length );
}
else {
in_offset = 0;
out_offset = 0;
while( nCurrentCount <= inLen )
{
KISA_SEED_Decrypt_Block_forECB( in, in_offset, out, out_offset, pInfo.seed_key );
nCurrentCount += BLOCK_SIZE_SEED;
in_offset += BLOCK_SIZE_SEED_INT;
out_offset += BLOCK_SIZE_SEED_INT;
}
outLen[0] = nCurrentCount - BLOCK_SIZE_SEED;
Common.memcpy( pInfo.ecb_last_block, out, out_offset - 4, BLOCK_SIZE_SEED );
}
return 1;
}
public static int SEED_ECB_Close( KISA_SEED_INFO pInfo, int[] out, int out_offset, int[] outLen ) {
int nPaddngLeng;
int i;
outLen[0] = 0;
if( null == out )
return 0;
if( KISA_ENC_DEC._KISA_ENCRYPT == pInfo.encrypt ) {
nPaddngLeng = BLOCK_SIZE_SEED - pInfo.buffer_length;
for( i = pInfo.buffer_length; i<BLOCK_SIZE_SEED; i++ ) {
Common.set_byte_for_int(pInfo.ecb_buffer, i, (byte)nPaddngLeng, ENDIAN);
}
KISA_SEED_Encrypt_Block_forECB( pInfo.ecb_buffer, 0, out, (out_offset)/4, pInfo.seed_key );
outLen[0] = BLOCK_SIZE_SEED;
}
else {
nPaddngLeng = Common.get_byte_for_int(pInfo.ecb_last_block, BLOCK_SIZE_SEED-1, ENDIAN);
if( nPaddngLeng > 0 && nPaddngLeng <= BLOCK_SIZE_SEED )
{
for( i = nPaddngLeng; i > 0; i--)
{
Common.set_byte_for_int(out, out_offset - i, (byte)0x00, ENDIAN);
}
outLen[0] = nPaddngLeng;
}
else
{
return 0;
}
}
return 1;
}
//.. skipping code due to length issueSystem.out.print(Integer.toHexString(0xff&result[i])+" ");
System.out.print("nn");
result = null;
pbszCipherText = null;
/*********************************************************************
* ��Ʈ���� 2
*********************************************************************/
PLAINTEXT_LENGTH = 2;
info = new KISA_SEED_INFO();
SEED_ECB_init( info, KISA_ENC_DEC.KISA_ENCRYPT, pbUserKey);
process_blockLeng = 32;
outbuf = new int[process_blockLeng];
pbszPlainText = new byte[process_blockLeng];
nPlainTextPadding = (BLOCK_SIZE_SEED - (PLAINTEXT_LENGTH)%BLOCK_SIZE_SEED);
CIPHERTEXT_LENGTH = PLAINTEXT_LENGTH + nPlainTextPadding;
pbszCipherText = new byte[PLAINTEXT_LENGTH + nPlainTextPadding];
for (i = 0; i < PLAINTEXT_LENGTH - process_blockLeng; )
{
System.arraycopy(pbData, i, pbszPlainText, 0, process_blockLeng);
data = chartoint32_for_SEED_ECB(pbszPlainText, process_blockLeng);
SEED_ECB_Process( info, data, process_blockLeng, outbuf, nRetOutLeng );
cdata = int32tochar_for_SEED_ECB(outbuf, nRetOutLeng[0]);
System.arraycopy(cdata, 0, pbszCipherText, i, nRetOutLeng[0]);
i += nRetOutLeng[0];
}
remainleng = PLAINTEXT_LENGTH % process_blockLeng;
if (remainleng == 0)
{
remainleng = process_blockLeng;
}
System.arraycopy(pbData, i, pbszPlainText, 0, remainleng);
data = chartoint32_for_SEED_ECB(pbszPlainText, remainleng);
SEED_ECB_Process( info, data, remainleng, outbuf, nRetOutLeng );
cdata = int32tochar_for_SEED_ECB(outbuf, nRetOutLeng[0]);
System.arraycopy(cdata, 0, pbszCipherText, i, nRetOutLeng[0]);
i += nRetOutLeng[0];
SEED_ECB_Close( info, outbuf, 0, nPaddingLeng );
cdata = int32tochar_for_SEED_ECB(outbuf, nPaddingLeng[0]);
System.arraycopy(cdata, 0, pbszCipherText, i, nPaddingLeng[0]);
System.out.print("Ciphertext(enc)t: ");
for(i=0; i<CIPHERTEXT_LENGTH; i++) System.out.print(Integer.toHexString(0xff&pbszCipherText[i])+" ");
System.out.print("n");
data = null;
cdata = null;
outbuf = null;
info = null;
//��ȣ
CIPHERTEXT_LENGTH = 16;
result = new byte[] {0};
info = new KISA_SEED_INFO();
EncryptedMessage_length = CIPHERTEXT_LENGTH;
if (EncryptedMessage_length % BLOCK_SIZE_SEED > 0)
{
System.out.print("DECRYPT FAIL! ");
}
else
{
SEED_ECB_init( info, KISA_ENC_DEC.KISA_DECRYPT, pbUserKey);
process_blockLeng = 32;
outbuf = new int[process_blockLeng];
byte[] cipherText = new byte[process_blockLeng];
pbszPlainText = new byte[EncryptedMessage_length];
for (i = 0; i < EncryptedMessage_length - process_blockLeng; )
{
System.arraycopy(pbszCipherText, i, cipherText, 0, process_blockLeng);
data = chartoint32_for_SEED_ECB(cipherText, process_blockLeng);
SEED_ECB_Process( info, data, process_blockLeng, outbuf, nRetOutLeng );
cdata = int32tochar_for_SEED_ECB(outbuf, nRetOutLeng[0]);
System.arraycopy(cdata, 0, pbszPlainText, i, nRetOutLeng[0]);
i += nRetOutLeng[0];
}
remainleng = EncryptedMessage_length % process_blockLeng;
if (remainleng == 0)
{
remainleng = process_blockLeng;
}
System.arraycopy(pbszCipherText, i, cipherText, 0, remainleng);
data = chartoint32_for_SEED_ECB(cipherText, remainleng);
SEED_ECB_Process( info, data, remainleng, outbuf, nRetOutLeng );
if (SEED_ECB_Close( info, outbuf, nRetOutLeng[0], nPaddingLeng ) > 0 )
{
cdata = int32tochar_for_SEED_ECB( outbuf, remainleng - nPaddingLeng[0] );
System.arraycopy(cdata, 0, pbszPlainText, i, remainleng - nPaddingLeng[0]);
PLAINTEXT_LENGTH = i + remainleng - nPaddingLeng[0];
result = new byte[PLAINTEXT_LENGTH];
System.arraycopy(pbszPlainText, 0, result, 0, PLAINTEXT_LENGTH);
data = null;
cdata = null;
outbuf = null;
}
else
{
result = new byte[10];
System.out.print("DECRYPT FAIL! ");
}
}
System.out.print("Plaintext(dec)t: ");
for (i=0; i<PLAINTEXT_LENGTH; i++) System.out.print(Integer.toHexString(0xff&result[i])+" ");
System.out.print("nn");
result = null;
pbszCipherText = null;
/*********************************************************************
* ��Ʈ���� 3
*********************************************************************/
PLAINTEXT_LENGTH = 18;
info = new KISA_SEED_INFO();
SEED_ECB_init( info, KISA_ENC_DEC.KISA_ENCRYPT, pbUserKey);
process_blockLeng = 32;
outbuf = new int[process_blockLeng];
pbszPlainText = new byte[process_blockLeng];
nPlainTextPadding = (BLOCK_SIZE_SEED - (PLAINTEXT_LENGTH)%BLOCK_SIZE_SEED);
CIPHERTEXT_LENGTH = PLAINTEXT_LENGTH + nPlainTextPadding;
pbszCipherText = new byte[PLAINTEXT_LENGTH + nPlainTextPadding];
for (i = 0; i < PLAINTEXT_LENGTH - process_blockLeng; )
{
System.arraycopy(pbData2, i, pbszPlainText, 0, process_blockLeng);
data = chartoint32_for_SEED_ECB(pbszPlainText, process_blockLeng);
SEED_ECB_Process( info, data, process_blockLeng, outbuf, nRetOutLeng );
cdata = int32tochar_for_SEED_ECB(outbuf, nRetOutLeng[0]);
System.arraycopy(cdata, 0, pbszCipherText, i, nRetOutLeng[0]);
i += nRetOutLeng[0];
}
remainleng = PLAINTEXT_LENGTH % process_blockLeng;
if (remainleng == 0)
{
remainleng = process_blockLeng;
}
System.arraycopy(pbData2, i, pbszPlainText, 0, remainleng);
data = chartoint32_for_SEED_ECB(pbszPlainText, remainleng);
SEED_ECB_Process( info, data, remainleng, outbuf, nRetOutLeng );
cdata = int32tochar_for_SEED_ECB(outbuf, nRetOutLeng[0]);
System.arraycopy(cdata, 0, pbszCipherText, i, nRetOutLeng[0]);
i += nRetOutLeng[0];
SEED_ECB_Close( info, outbuf, 0, nPaddingLeng );
cdata = int32tochar_for_SEED_ECB(outbuf, nPaddingLeng[0]);
System.arraycopy(cdata, 0, pbszCipherText, i, nPaddingLeng[0]);
System.out.print("Ciphertext(enc)t: ");
for(i=0; i<CIPHERTEXT_LENGTH; i++) System.out.print(Integer.toHexString(0xff&pbszCipherText[i])+" ");
System.out.print("n");
data = null;
cdata = null;
outbuf = null;
info = null;
//��ȣ
CIPHERTEXT_LENGTH = 32;
result = new byte[] {0};
info = new KISA_SEED_INFO();
EncryptedMessage_length = CIPHERTEXT_LENGTH;
if (EncryptedMessage_length % BLOCK_SIZE_SEED > 0)
{
System.out.print("DECRYPT FAIL! ");
}
else
{
SEED_ECB_init( info, KISA_ENC_DEC.KISA_DECRYPT, pbUserKey);
process_blockLeng = 32;
outbuf = new int[process_blockLeng];
byte[] cipherText = new byte[process_blockLeng];
pbszPlainText = new byte[EncryptedMessage_length];
for (i = 0; i < EncryptedMessage_length - process_blockLeng; )
{
System.arraycopy(pbszCipherText, i, cipherText, 0, process_blockLeng);
data = chartoint32_for_SEED_ECB(cipherText, process_blockLeng);
SEED_ECB_Process( info, data, process_blockLeng, outbuf, nRetOutLeng );
cdata = int32tochar_for_SEED_ECB(outbuf, nRetOutLeng[0]);
System.arraycopy(cdata, 0, pbszPlainText, i, nRetOutLeng[0]);
i += nRetOutLeng[0];
}
remainleng = EncryptedMessage_length % process_blockLeng;
if (remainleng == 0)
{
remainleng = process_blockLeng;
}
System.arraycopy(pbszCipherText, i, cipherText, 0, remainleng);
data = chartoint32_for_SEED_ECB(cipherText, remainleng);
SEED_ECB_Process( info, data, remainleng, outbuf, nRetOutLeng );
if (SEED_ECB_Close( info, outbuf, nRetOutLeng[0], nPaddingLeng ) > 0 )
{
cdata = int32tochar_for_SEED_ECB( outbuf, remainleng - nPaddingLeng[0] );
System.arraycopy(cdata, 0, pbszPlainText, i, remainleng - nPaddingLeng[0]);
PLAINTEXT_LENGTH = i + remainleng - nPaddingLeng[0];
result = new byte[PLAINTEXT_LENGTH];
System.arraycopy(pbszPlainText, 0, result, 0, PLAINTEXT_LENGTH);
data = null;
cdata = null;
outbuf = null;
}
else
{
result = new byte[10];
System.out.print("DECRYPT FAIL! ");
}
}
System.out.print("Plaintext(dec)t: ");
for (i=0; i<PLAINTEXT_LENGTH; i++) System.out.print(Integer.toHexString(0xff&result[i])+" ");
System.out.print("nn");
}
}
I tried to change the java code into js code and work it on crypto-js
I don’t even know how to use the SEED on js.
I don’t see such thing as SEED on crypto-js file.
So Im trying to import the give SEED file from java and somehow use it on crypto-js
Jae Hyun Yoo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.