This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!

H.C. Satish

Greenhorn
+ Follow
since Feb 08, 2004
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by H.C. Satish

Hi All
When Execute the below code exception in jdk14CipherWithModeWithPadding()"java.security.NoSuchAlgorithmException" @ the commented lines (2, 3, 6, 7, 8, 10, 11, 14, 15, 16, 17, 18, 19, 22, 23)
but in jdk14CipherProviders() there is no exception,
Kindly suggest issues related these exceptions

import javax.crypto.Cipher;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import javax.crypto.NoSuchPaddingException;

public class CipherProviderInJdk14 {
public static void main (String ar[]) {
CipherProviderInJdk14 cipherProviderInJdk14 = new CipherProviderInJdk14();
cipherProviderInJdk14.jdk14CipherWithModeWithPadding ( );
cipherProviderInJdk14.jdk14CipherProviders( );
}
public void jdk14CipherWithModeWithPadding ( ) {
String alg [] = { "Blowfish/ECB/PKCS5Padding",// 0
"Blowfish/CBC/PKCS5Padding",// 2
"Blowfish/CFB/PKCS5Padding",// 3
"Blowfish/OFB/PKCS5Padding",// 4
"DESEDE/ECB/PKCS5Padding",// 5
"DESEDE/CBC/PKCS5Padding",// 6
"DESEDE/CFB/PKCS5Padding",// 7
"DESEDE/OFB/PKCS5Padding",// 8
"PBEWITHMD5ANDTRIPLEDES/ECB/PKCS5Padding",// 9
"PBEWITHMD5ANDTRIPLEDES/CBC/PKCS5Padding",// 10
"PBEWITHMD5ANDTRIPLEDES/CFB/PKCS5Padding",// 11
"PBEWITHMD5ANDTRIPLEDES/OFB/PKCS5Padding",// 12
"DES/ECB/PKCS5Padding",// 13
"DES/CBC/PKCS5Padding",// 14
"DES/CFB/PKCS5Padding",// 15
"DES/OFB/PKCS5Padding",// 16
"PBEWITHMD5ANDDES/ECB/PKCS5Padding",// 17
"PBEWITHMD5ANDDES/CBC/PKCS5Padding",// 18
"PBEWITHMD5ANDDES/CFB/PKCS5Padd2ing",// 19
"PBEWITHMD5ANDDES/OFB/PKCS5Padding",// 20
"TRIPLEDES/ECB/PKCS5Padding",// 21
"TRIPLEDES/CBC/PKCS5Padding",// 22
"TRIPLEDES/CFB/PKCS5Padding",// 23
"TRIPLEDES/OFB/PKCS5Padding" };// 24
for (int i = 0; i < alg.length; i++) {
try {
Cipher skf = Cipher.getInstance(alg[i]);
Provider pr = skf.getProvider();
System.out.println(i + "\t" +alg[i] + "\tCipher\t" + pr.getName());
} catch (NoSuchAlgorithmException nsae) {
System.out.println(i + "\t" + alg[i] + "\tCipher\t\t" +nsae);
} catch (NoSuchPaddingException nspe) {
System.out.println(i + "\t" + alg[i] + "\tCipher\t\t" +nspe);
}
}
}
public void jdk14CipherProviders( ) {
String prov [] = { "Blowfish", "DESEDE", "PBEWITHMD5ANDTRIPLEDES", "DES", "PBEWITHMD5ANDDES", "tripledes" };
for (int i = 0; i < prov.length; i++) {
try {
Cipher skf = Cipher.getInstance(prov[i]);
Provider pr = skf.getProvider();
System.out.println(i + "\t" + prov[i] + "\tCipher Provider\t" + pr.getName());
} catch (NoSuchAlgorithmException nsae) {
System.out.println(i + "\t" + prov[i] + "\tCipher Provider\t" +nsae);
} catch (NoSuchPaddingException nspe) {
System.out.println(i + "\t" + prov[i] + "\tCipher\t\t" +nspe);
}
}
}
}
17 years ago
Hi,
Generated the KeyStore using the keytool using Default KeyStrore Type & KeyStore = User, Password=password
With keystore am able to retrieve the both Private & Public Keys by making use of KeyStore.getDefaultType())
When i tried 2 encrypt the message using the params Cipher, PublicKey, MESSAGE, Cipher.ENCRYPT_MODE
As above cipher is initialized with "RSA" & meantime the KeyStore is also created by "RSA"
But when program is executed, getting an error "java.security.InvalidKeyException: Could not initialize for signing with the given key"
Kindly inform me what must be the [-keyalg <keyalg>] used for both KeyStore & initialization of Cipher
NOTE: am using the JDK1.4
Cordially
17 years ago
The below code throws the following exception
Exception in thread "main" java.lang.NullPointerException
at EncryptionProblem.crypt(EncryptionProblem.java:85)
at EncryptionProblem.<init>(EncryptionProblem.java:37)
at EncryptionProblem.main(EncryptionProblem.java:16)
Press any key to continue . . .
But runs fine if uncomment the lines 106 & 15,
line 15adds the BouncyCastle provider
line 106KeyFactory retrieves the Algorithm from BouncyCastle provider
Kindly help me executing the same above code without BouncyCastle provider
BELOW IS THE COMPLETE CODE ATTACHED (documented lines are deleted)

import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;

public class EncryptionProblem {
public static final String PUB_FILE = "publicKey.txt";
public static final String PRI_FILE = "privateKey.txt";
public static final int PRIVATE = 0;
public static final int PUBLIC = 1;
public static void main(String[] args) {
// Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
new EncryptionProblem();
}
public EncryptionProblem() {
KeyPairGenerator kpg = initKeys();
PrivateKey priKey = null;
PublicKey pubKey = null;
try {
priKey = (PrivateKey)getKey(PRI_FILE, PRIVATE);
pubKey = (PublicKey)getKey(PUB_FILE, PUBLIC);
} catch (Exception e) { System.err.println("30:" + e); }
Cipher c = null;
try {
c = Cipher.getInstance("RSA");
} catch (Exception e) {}
String message = "Encryption & Decryption of a MESSAGE";
byte[] text = message.getBytes();
text = crypt(c, pubKey, text, Cipher.ENCRYPT_MODE);
System.out.println("Encrypted Text:"+new String(text));
text = crypt(c, priKey, text, Cipher.DECRYPT_MODE);
System.out.println("Decrypting Text:" + new String(text));
}

private KeyPairGenerator initKeys() {
KeyPairGenerator kpg = null;
try {
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024, new SecureRandom());
KeyPair kp = kpg.generateKeyPair();
PrivateKey priKey = kp.getPrivate();
writeKeyToFile(PRI_FILE, priKey.getEncoded());
PublicKey pubKey = kp.getPublic();
writeKeyToFile(PUB_FILE, pubKey.getEncoded());
} catch (Exception e) { }
return kpg;
}
private void writeKeyToFile(String filename, byte[] contents) {
try {
FileOutputStream fos = new FileOutputStream(filename);
fos.write(contents);
fos.flush();
fos.close();
} catch (Exception e) { }
}
private byte[] crypt(Cipher cipher, Key key, byte[] text, int type) {
ByteArrayOutputStream out = null;
try {
cipher.init(type, key, new SecureRandom());
out = new ByteArrayOutputStream();
int s = cipher.getBlockSize();
int r = 0;
for (int t = 0; t < text.length; t += s) {
if (text.length - t <= s) {
r = text.length - t;
} else {
r = s;
}
out.write(cipher.doFinal(text, t, r));
}
out.flush();
out.close();
} catch (Exception e) { }
return out.toByteArray();
}

private Key getKey(String filename, int type) throws Exception {
FileInputStream fis = null;
fis = new FileInputStream(filename);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int b;
try {
while ((b = fis.read()) != -1) {
baos.write(b);
}
} catch (Exception e) {}
byte[] keydata = baos.toByteArray();
Key key = null;
try {
KeyFactory kf = KeyFactory.getInstance("RSA", "SUN");
// KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
switch (type) {
case PRIVATE:
PKCS8EncodedKeySpec encPrivateKey = new PKCS8EncodedKeySpec(keydata);
key = kf.generatePrivate(encPrivateKey);
return key;
case PUBLIC:
X509EncodedKeySpec encPublicKey = new X509EncodedKeySpec(keydata);
key = kf.generatePublic(encPublicKey);
return key;
}
} catch (Exception e) { }
return key;
}
}
17 years ago
Hi,
Situation:Dynamic JTree (Tree is constructed from as a result of Database). User has the option to either Enable / Disable node and can repeat the activities but restricted only one activity per node, the node is to be highlighted; to restrict the user from further activities.
Problem:Am a newbie to swing, after searching the forum for the same, i found few good solutions which are specific to color change of specific selected node, in a temporary context.
But we need the node color change within a same level; to distinguish either the node is either enabled / disabled.

Your help is appreciated,
17 years ago