• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Algorithm DES not available

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using JDK 1.4.1_04 and I got the following error.

java.security.NoSuchAlgorithmException: Algorithm DES not available
at javax.crypto.SunJCE_b.a(DashoA6275)
at javax.crypto.KeyGenerator.getInstance(DashoA6275)
at SymmetricCipherTest.main(SymmetricCipherTest.java:29)

this is my code:

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.Cipher;

public class SymmetricCipherTest {
private static byte[] iv =
{ 0x0a, 0x01, 0x02, 0x03, 0x04, 0x0b, 0x0c, 0x0d };

private static byte[] encrypt(byte[] inpBytes,
SecretKey key, String xform) throws Exception {
Cipher cipher = Cipher.getInstance(xform);
IvParameterSpec ips = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, key, ips);
return cipher.doFinal(inpBytes);
}

private static byte[] decrypt(byte[] inpBytes,
SecretKey key, String xform) throws Exception {
Cipher cipher = Cipher.getInstance(xform);
IvParameterSpec ips = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, key, ips);
return cipher.doFinal(inpBytes);
}

public static void main(String[] unused) throws Exception {
String xform = "DES/ECB/PKCS5Padding";
// Generate a secret key
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(56); // 56 is the keysize. Fixed for DES
SecretKey key = kg.generateKey();

byte[] dataBytes =
"J2EE Security for Servlets, EJBs and Web Services".getBytes();

byte[] encBytes = encrypt(dataBytes, key, xform);
byte[] decBytes = decrypt(encBytes, key, xform);

boolean expected = java.util.Arrays.equals(dataBytes, decBytes);
System.out.println("Test " + (expected ? "SUCCEEDED!" : "FAILED!"));
}
}

when run the following codes to list the crypto service and implementation,

String[] cryptoType = CryptoService.getServiceTypes();
for(int i=0; i<cryptoType.length; i++){
System.out.println("=============================");
System.out.println(cryptoType[i]);
System.out.println("=============================");
String[] impl = CryptoService.getCryptoImpls(cryptoType[i]);
for(int j=0; j<impl.length; j++){
System.out.println(impl[j]);
}
}

I have the following out put:

=============================
TrustManagerFactory
=============================
SunX509
=============================
CertPathValidator
=============================
PKIX
=============================
SSLContext
=============================
TLS
SSL
TLSv1
SSLv3
=============================
CertStore
=============================
Collection
LDAP
=============================
AlgorithmParameterGenerator
=============================
DSA
DiffieHellman
DH
=============================
KeyGenerator
=============================
TripleDES
Blowfish
DESede
HmacMD5
HmacSHA1
DES
=============================
Mac
=============================
HmacMD5
HmacSHA1
=============================
Cipher
=============================
TripleDES
PBEWithMD5AndDES
Blowfish
DESede
PBEWithMD5AndTripleDES
DES
=============================
Signature
=============================
MD2withRSA
OID.1.2.840.10040.4.3
1.3.14.3.2.13
SHAwithDSA
DSAWithSHA1
1.3.14.3.2.27
DSA
1.2.840.10040.4.3
SHA1withRSA
SHA/DSA
SHA-1/DSA
SHA1withDSA
MD5withRSA
DSS
SHA1/DSA
=============================
KeyStore
=============================
PKCS12
JKS
JCEKS
=============================
MessageDigest
=============================
SHA1
SHA
SHA-1
MD5
=============================
SecureRandom
=============================
SHA1PRNG
=============================
KeyPairGenerator
=============================
RSA
DSA
1.2.840.10040.4.1
DiffieHellman
DH
1.3.14.3.2.12
OID.1.2.840.10040.4.1
=============================
CertificateFactory
=============================
X509
X.509
=============================
KeyManagerFactory
=============================
SunX509
=============================
AlgorithmParameters
=============================
TripleDES
PBEWithMD5AndDES
DSA
Blowfish
1.2.840.10040.4.1
DiffieHellman
DESede
DH
1.3.14.3.2.12
DES
PBE
=============================
KeyAgreement
=============================
DiffieHellman
DH
=============================
SecretKeyFactory
=============================
TripleDES
PBEWithMD5AndDES
DESede
DES
=============================
GssApiMechanism
=============================
1.2.840.113554.1.2.2
=============================
KeyFactory
=============================
RSA
DSA
1.2.840.10040.4.1
DiffieHellman
DH
1.3.14.3.2.12
=============================
CertPathBuilder
=============================
PKIX

Seems like the service and implementation is available. I do not know where is the mistake.

Can anyone please help me...

Regards,
ChengKuan, Gan
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
J2SDK comes standard with a cryptographic service provider named "SUN" (which supplies an implementation of the MD5 message digest algorithm), and JCE 1.2.1 comes standard with a different provider named "SunJCE" (which implements a session key generator for DES). While the "SUN" provider is registered automatically, the "SunJCE" provider needs to be registered explicitly.
Please refer to the installation guide for directions on how to register the "SunJCE" provider. Once you have registered the "SunJCE" provider, the above exception will disappear.
 
ChengKuan Gan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
xiaotaoliang, thank you for your suggestion.

By right JDK1.4 should statically registers the SunJCE since sunJCE is part of JDK1.4, which also can be seen in the JRE's java.security file. However, the provider only registered when I calls the following codes in my Java codes:

java.security.Security.addProvider(new com.sun.crypto.provider.SunJCE());

anyway, thank you for your help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic