Results 1 to 7 of 7
- 01-23-2011, 09:56 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Help required in executing the following code
Can anyone pls tell me what is the error in the following code.
It gives me 1 warning and 1 error.
pls help me with this code.
the code is as follows.
/**
* jBorZoi - An Elliptic Curve Cryptography Library
*
* Copyright (C) 2003 Dragongate Technologies Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.dragongate_technologies.borZoi;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import com.dragongate_technologies.borZoi.internal.*;
/**
* Elliptic Curve Cryptography Functions
* @author <a href="http://www.dragongate-technologies.com">Dragongate Technologies Ltd.</a>
* @version 0.90
*/
public class ECC {
private static byte[] Enc(byte[] U, byte[] KB, int keysize) {
Rijndael r = new Rijndael();
r.makeKey(KB, keysize);
byte c[] = new byte[U.length];
r.encrypt(U, c);
return c;
}
private static byte[] Dec(byte[] U, byte[] KB, int keysize) {
Rijndael r = new Rijndael();
r.makeKey(KB, keysize);
byte p[] = new byte[U.length];
r.decrypt(U, p);
return p;
}
/**
* The Advanced Encryption Symmetric (AES) decryption algorithm in
* Cipher Block Chaining (CBC) mode with a null initialization
* vector.
* <p>
* The AES implementation used is the public domain optimised
* Java implementation of the Rijndael (AES) block cipher by
* Paul Barreto.
* (see com.dragongate_technologies.borZoi.internal.Rijnda el)
* </p><p>
* Decrypt an octet string C with key KB of length keysize.
* @param KB the key
* @param C the ciphertext to be decrypted
* @param keysize can be 128, 192 or 256 bits
* @return the decrypted plain text
*/
public static int[] AES_CBC_IV0_Decrypt(int[] KB, int[] C, int keysize) {
if (C.length % 16 != 0) {
throw (
new RuntimeException("AES_CBC_IV0_Decrypt: C.length not a multiple of 16"));
} else if (C.length < 16) {
throw (new RuntimeException("AES_CBC_IV0_Decrypt: C.length < 16"));
}
int k = ((C.length + 1) / 16);
int T[] = new int[C.length];
int U[] = new int[16];
int CI[] = new int[16];
for (int i = 1; i <= k; i++) {
for (int n = 0; n < 16; n++) {
CI[n] = C[(i - 1) * 16 + n];
}
U =
Utils.toIntArray(
Dec(Utils.toByteArray(CI), Utils.toByteArray(KB), keysize));
for (int j = 0; j < 16; j++) {
if (i > 1)
T[(i - 1) * 16 + j] = U[j] ^ C[(i - 2) * 16 + j];
else
T[j] = U[j];
}
}
int padLen = T[(k * 16) - 1];
if (padLen < 1) {
throw (new RuntimeException("AES_CBC_IV0_Decrypt: padLen < 1"));
} else if (padLen > 16) {
throw (
new RuntimeException(
"AES_CBC_IV0_Decrypt: padLen(" + padLen + ")>16"));
}
for (int l = 1; l < padLen; l++) {
if (T[(k * 16) - 1 - l] != padLen) {
throw (
new RuntimeException("AES_CBC_IV0_Decrypt: OCTET != padLen"));
}
}
int M[] = new int[T.length - padLen];
for (int m = 0; m < T.length - padLen; m++) {
M[m] = T[m];
}
return M;
}
/**
* The Advanced Encryption Symmetric (AES) encryption algorithm in
* Cipher Block Chaining (CBC) mode with a null initialization
* vector.
* </p><p>
* The AES implementation used is the public domain optimised
* Java implementation of the Rijndael (AES) block cipher by
* Paul Barreto.
* (see com.dragongate_technologies.borZoi.internal.Rijnda el)
* </p><p>
* Encrypt an octet string M with key KB of length keysize.
* @param KB the key
* @param M the plaintext to be encrypted
* @param keysize can be 128, 192 or 256 bits
* @return the encrypted cipher text
*/
public static int[] AES_CBC_IV0_Encrypt(int[] KB, int[] M, int keysize) {
int padLen = 16 - (M.length % 16);
int k = ((M.length + 1) / 16);
if ((M.length + 1) % 16 != 0)
k++;
int P1[] = new int[1];
P1[0] = padLen;
int P2[] = new int[padLen];
for (int l = 0; l < padLen; l++) {
P2[l] = P1[0];
}
int T[] = Utils.concatenate(M, P2);
int C[] = new int[16];
int U[] = new int[16];
for (int i = 1; i <= k; i++) {
for (int j = 0; j < 16; j++) {
if (i == 1)
U[j] = T[(i - 1) * 16 + j];
else
U[j] = T[(i - 1) * 16 + j] ^ C[(i - 2) * 16 + j];
}
if (i == 1)
C =
Utils.toIntArray(
Enc(
Utils.toByteArray(U),
Utils.toByteArray(KB),
keysize));
else
C =
Utils.concatenate(
C,
Utils.toIntArray(
Enc(
Utils.toByteArray(U),
Utils.toByteArray(KB),
keysize)));
}
return C;
}
/** Key Derivation Function 2 (KDF2) from the IEEE P1363a
* draft standard.
* It generates a secret key of length oLen bits from shared
* secret Z and key derivation parameter P.
*/
public static int[] KDF2(int[] Z, int oLen, int[] P) {
// Note:
// oLen cannot be > hbits * (2^32-1) bits, because the size of an
// int is 32 bits
int[] K = new int[0];
int[] CB = new int[1];
int cThreshold = (oLen / 160);
if (oLen % 160 != 0)
cThreshold++;
try {
MessageDigest sha = MessageDigest.getInstance("SHA");
//sha.update(data);
//byte[] hash = sha.digest(data);
for (byte i = 1; i <= cThreshold; i++) {
CB[0] = i;
sha.update(Utils.toByteArray(Utils.concatenate(Z, CB, P)));
K =
Utils.concatenate(
K,
Utils.revIntArray(Utils.toIntArray(sha.digest()))) ;
//K = Utils.concatenate(K, Utils.toIntArray(sha.digest()));
sha.reset(); // not needed after diget()
}
} catch (NoSuchAlgorithmException e) {
}
//return Utils.resize (Utils.revIntArray(K), oLen);
return Utils.resize(K, oLen);
}
/** MAC1 as described in the IEEE P1363 standard.
* It computes a HMAC message authentication code tag from
* secret key KB and message M.
*/
public static int[] MAC1(int[] K, int[] M) {
int[] HH = new int[0];
try {
int i;
int[] KK;
MessageDigest sha = MessageDigest.getInstance("SHA");
// SHA1 Blocksize B = 512
if (K.length > (8 * 512)) {
sha.update(Utils.toByteArray(K));
// kkLen = 20 octets, 160 bits
KK = Utils.revIntArray(Utils.toIntArray(sha.digest()));
} else
KK = K;
int[] P = new int[512 - KK.length];
for (i = 0; i < P.length; i++) {
P[i] = 0;
}
int[] K0 = Utils.concatenate(KK, P);
int[] iPad = new int[512];
for (i = 0; i < iPad.length; i++) {
iPad[i] = 0x36;
}
int[] oPad = new int[512];
for (i = 0; i < oPad.length; i++) {
oPad[i] = 0x54;
}
sha.reset();
sha.update(
Utils.toByteArray(Utils.concatenate(Utils.xor(K0, iPad), M)));
int[] H = Utils.revIntArray(Utils.toIntArray(sha.digest()));
sha.reset();
sha.update(
Utils.toByteArray(Utils.concatenate(Utils.xor(K0, oPad), H)));
HH = Utils.revIntArray(Utils.toIntArray(sha.digest()));
} catch (NoSuchAlgorithmException e) {
}
return HH;
}
/**
* ECSVDP_DH
* Throws an exception if the point P is zero
*/
protected static Fq ECSVDP_DH(
ECDomainParameters dp,
BigInteger s,
ECPoint Wi) {
ECPoint P = dp.E.mul(s, Wi);
if (P.isZero())
throw (new RuntimeException("ECSVDP_DH: P is zero"));
return P.x;
}
/**
* The Elliptic Curve Diffe-Hellman Key Agreement Scheme as
* specified in ANSI X9.63 and IEEE P1363
* In ECKAS-DH1 (the Elliptic Curve Key Agreement Scheme,
* Diffie-Hellman 1), each party combines its own private key
* with the other partyfs public key to calculate a shared
* secret key which can then be used as the key for a
* symmetric encryption algorithm such as AES. Other
* (public or private) information known to both parties
* may be used as key derivation parameters to ensure that
* a dierent secret key is generated every session. This
* key agreement scheme is described in more detail in
* section 9.2 of the IEEEP1363 standard.
* This Calculates a 128 bit secret key from EC domain
* parameters dp, private key s, public key Wi and key
* derivation parameter P (an octet string).
* s belongs to one party, Wi belongs to the other and dp
* and P are common to both of them.
* @param dp The EC domain parameters.
* @param s The EC private key.
* @param Wi The EC public key.
* @param P The key derivation parameter.
* @return
*/
public static BigInteger ECKAS_DH1(
ECDomainParameters dp,
BigInteger s,
ECPoint Wi,
int[] P) {
Fq z = ECSVDP_DH(dp, s, Wi);
int[] Z = Utils.FE2OSP(z);
int[] k = KDF2(Z, 16, P); // 128 bits
BigInteger K = Utils.OS2IP(k);
return K;
}
}
</p>
- 01-23-2011, 11:22 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
A few remarks:
1) you dumped pages and pages of code without any explanation here.
2) you didn't care to format the code properly.
3) you didn't tell us what the error and warning were, nor did you tell us where they were in the code.
4) what do you want help with? The author of the code left a web site address. <--- that's a hint.
5) there's no need to double post here.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-23-2011, 01:54 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Reply
Sorry for not showing where the errors were...
i have written in red font color where the error n warning were shown...
/**
* jBorZoi - An Elliptic Curve Cryptography Library
*
* Copyright (C) 2003 Dragongate Technologies Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.dragongate_technologies.borZoi;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import com.dragongate_technologies.borZoi.internal.*;
/**
* Elliptic Curve Cryptography Functions
* @author <a href="http://www.dragongate-technologies.com">Dragongate Technologies Ltd.</a>
* @version 0.90
*/
public class ECC {
private static byte[] Enc(byte[] U, byte[] KB, int keysize) {
Rijndael r = new Rijndael();
r.makeKey(KB, keysize);
byte c[] = new byte[U.length];
r.encrypt(U, c);
return c;
}
private static byte[] Dec(byte[] U, byte[] KB, int keysize) {
Rijndael r = new Rijndael();
r.makeKey(KB, keysize);
byte p[] = new byte[U.length];
r.decrypt(U, p);
return p;
}
/**
* The Advanced Encryption Symmetric (AES) decryption algorithm in
* Cipher Block Chaining (CBC) mode with a null initialization
* vector.
* <p>
* The AES implementation used is the public domain optimised
* Java implementation of the Rijndael (AES) block cipher by
* Paul Barreto.
* (see com.dragongate_technologies.borZoi.internal.Rijnda el)
* </p><p>
* Decrypt an octet string C with key KB of length keysize.
* @param KB the key
* @param C the ciphertext to be decrypted
* @param keysize can be 128, 192 or 256 bits
* @return the decrypted plain text
*/
public static int[] AES_CBC_IV0_Decrypt(int[] KB, int[] C, int keysize) {
if (C.length % 16 != 0) {
throw (
new RuntimeException("AES_CBC_IV0_Decrypt: C.length not a multiple of 16"));
} else if (C.length < 16) {
throw (new RuntimeException("AES_CBC_IV0_Decrypt: C.length < 16"));
}
int k = ((C.length + 1) / 16);
int T[] = new int[C.length];
int U[] = new int[16];
int CI[] = new int[16];
for (int i = 1; i <= k; i++) {
for (int n = 0; n < 16; n++) {
CI[n] = C[(i - 1) * 16 + n];
}
U =
Utils.toIntArray(
Dec(Utils.toByteArray(CI), Utils.toByteArray(KB), keysize));
for (int j = 0; j < 16; j++) {
if (i > 1)
T[(i - 1) * 16 + j] = U[j] ^ C[(i - 2) * 16 + j];
else
T[j] = U[j];
}
}
int padLen = T[(k * 16) - 1];
if (padLen < 1) {
throw (new RuntimeException("AES_CBC_IV0_Decrypt: padLen < 1"));
} else if (padLen > 16) {
throw (
new RuntimeException(
"AES_CBC_IV0_Decrypt: padLen(" + padLen + ")>16"));
}
for (int l = 1; l < padLen; l++) {
if (T[(k * 16) - 1 - l] != padLen) {
throw (
new RuntimeException("AES_CBC_IV0_Decrypt: OCTET != padLen"));
}
}
int M[] = new int[T.length - padLen];
for (int m = 0; m < T.length - padLen; m++) {
M[m] = T[m];
}
return M;
}
/**
* The Advanced Encryption Symmetric (AES) encryption algorithm in
* Cipher Block Chaining (CBC) mode with a null initialization
* vector.
* </p><p>
* The AES implementation used is the public domain optimised
* Java implementation of the Rijndael (AES) block cipher by
* Paul Barreto.
* (see com.dragongate_technologies.borZoi.internal.Rijnda el)
* </p><p> //here it gives a warning saying unmappable character for encoding Cp1252 * with the other party?fs public key used to calculate a shared
* Encrypt an octet string M with key KB of length keysize.
* @param KB the key
* @param M the plaintext to be encrypted
* @param keysize can be 128, 192 or 256 bits
* @return the encrypted cipher text
*/
public static int[] AES_CBC_IV0_Encrypt(int[] KB, int[] M, int keysize) {
int padLen = 16 - (M.length % 16);
int k = ((M.length + 1) / 16);
if ((M.length + 1) % 16 != 0)
k++;
int P1[] = new int[1];
P1[0] = padLen;
int P2[] = new int[padLen];
for (int l = 0; l < padLen; l++) {
P2[l] = P1[0];
}
int T[] = Utils.concatenate(M, P2);
int C[] = new int[16];
int U[] = new int[16];
for (int i = 1; i <= k; i++) {
for (int j = 0; j < 16; j++) {
if (i == 1)
U[j] = T[(i - 1) * 16 + j];
else
U[j] = T[(i - 1) * 16 + j] ^ C[(i - 2) * 16 + j];
}
if (i == 1)
C =
Utils.toIntArray(
Enc(
Utils.toByteArray(U),
Utils.toByteArray(KB),
keysize));
else
C =
Utils.concatenate(
C,
Utils.toIntArray(
Enc(
Utils.toByteArray(U),
Utils.toByteArray(KB),
keysize)));
}
return C;
}
/** Key Derivation Function 2 (KDF2) from the IEEE P1363a
* draft standard.
* It generates a secret key of length oLen bits from shared
* secret Z and key derivation parameter P.
*/
public static int[] KDF2(int[] Z, int oLen, int[] P) {
// Note:
// oLen cannot be > hbits * (2^32-1) bits, because the size of an
// int is 32 bits
int[] K = new int[0];
int[] CB = new int[1];
int cThreshold = (oLen / 160);
if (oLen % 160 != 0)
cThreshold++;
try {
MessageDigest sha = MessageDigest.getInstance("SHA");
//sha.update(data);
//byte[] hash = sha.digest(data);
for (byte i = 1; i <= cThreshold; i++) {
CB[0] = i;
sha.update(Utils.toByteArray(Utils.concatenate(Z, CB, P)));
K =
Utils.concatenate(
K,
Utils.revIntArray(Utils.toIntArray(sha.digest()))) ;
//K = Utils.concatenate(K, Utils.toIntArray(sha.digest()));
sha.reset(); // not needed after diget()
}
} catch (NoSuchAlgorithmException e) {
}
//return Utils.resize (Utils.revIntArray(K), oLen);
return Utils.resize(K, oLen);
}
/** MAC1 as described in the IEEE P1363 standard.
* It computes a HMAC message authentication code tag from
* secret key KB and message M.
*/
public static int[] MAC1(int[] K, int[] M) {
int[] HH = new int[0];
try {
int i;
int[] KK;
MessageDigest sha = MessageDigest.getInstance("SHA");
// SHA1 Blocksize B = 512
if (K.length > (8 * 512)) {
sha.update(Utils.toByteArray(K));
// kkLen = 20 octets, 160 bits
KK = Utils.revIntArray(Utils.toIntArray(sha.digest()));
} else
KK = K;
int[] P = new int[512 - KK.length];
for (i = 0; i < P.length; i++) {
P[i] = 0;
}
int[] K0 = Utils.concatenate(KK, P);
int[] iPad = new int[512];
for (i = 0; i < iPad.length; i++) {
iPad[i] = 0x36;
}
int[] oPad = new int[512];
for (i = 0; i < oPad.length; i++) {
oPad[i] = 0x54;
}
sha.reset();
sha.update(
Utils.toByteArray(Utils.concatenate(Utils.xor(K0, iPad), M)));
int[] H = Utils.revIntArray(Utils.toIntArray(sha.digest()));
sha.reset();
sha.update(
Utils.toByteArray(Utils.concatenate(Utils.xor(K0, oPad), H)));
HH = Utils.revIntArray(Utils.toIntArray(sha.digest()));
} catch (NoSuchAlgorithmException e) {
}
return HH;
}
/**
* ECSVDP_DH
* Throws an exception if the point P is zero
*/
protected static Fq ECSVDP_DH(
ECDomainParameters dp,
BigInteger s,
ECPoint Wi) {
ECPoint P = dp.E.mul(s, Wi);
if (P.isZero())
throw (new RuntimeException("ECSVDP_DH: P is zero"));
return P.x;
}
/**
* The Elliptic Curve Diffe-Hellman Key Agreement Scheme as
* specified in ANSI X9.63 and IEEE P1363
* In ECKAS-DH1 (the Elliptic Curve Key Agreement Scheme,
* Diffie-Hellman 1), each party combines its own private key
* with the other partyfs public key to calculate a shared
* secret key which can then be used as the key for a
* symmetric encryption algorithm such as AES. Other
* (public or private) information known to both parties
* may be used as key derivation parameters to ensure that
* a dierent secret key is generated every session. This
* key agreement scheme is described in more detail in
* section 9.2 of the IEEEP1363 standard.
* This Calculates a 128 bit secret key from EC domain
* parameters dp, private key s, public key Wi and key
* derivation parameter P (an octet string).
* s belongs to one party, Wi belongs to the other and dp
* and P are common to both of them.
* @param dp The EC domain parameters.
* @param s The EC private key.
* @param Wi The EC public key.
* @param P The key derivation parameter.
* @return
*/
public static BigInteger ECKAS_DH1(
ECDomainParameters dp,
BigInteger s,
ECPoint Wi,
int[] P) {
Fq z = ECSVDP_DH(dp, s, Wi);
int[] Z = Utils.FE2OSP(z);
int[] k = KDF2(Z, 16, P); // 128 bits
BigInteger K = Utils.OS2IP(k);
return K;
}
}
</p>here it gives an error saying class,interface or enum expected
- 01-23-2011, 05:24 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
A few links of suggested Reading if you would like more expedient help
How To Ask Questions The Smart Way
Short, Self Contained, Correct Example
-
++
Yep, and how to use code tags.
Basically, place tag
[code]above the code block
and the tag
[/code]below the code block:
e.g.,
Java Code:[code] // your code goes here [/code]
- 01-24-2011, 12:11 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Looks to me like you copy/pasted a load of code from somewhere and brought some HTML markup along with it.
You'll need to tidy it up.
- 01-24-2011, 12:24 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Help required in executing the following code
By vivekdarshan in forum Advanced JavaReplies: 0Last Post: 01-23-2011, 09:34 AM -
Urgent! executing c code from java
By sara12345 in forum New To JavaReplies: 2Last Post: 01-02-2010, 03:18 PM -
Java code required
By arevijay in forum Advanced JavaReplies: 3Last Post: 12-02-2008, 01:15 PM -
code required
By baba in forum JavaServer Pages (JSP) and JSTLReplies: 6Last Post: 10-06-2008, 03:43 PM -
Executing Ant code pragramatically
By MikeO in forum Advanced JavaReplies: 0Last Post: 07-24-2007, 09:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks