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