在网上下了des算法的加密程序,开始用sum的jdk没有问题,后来改用ibm的,发现这个地方运行时老是抛出异常utf8的编码问题,byte[] enc = ecipher.doFinal(utf8);错误:NullPointerException那位大侠点一下,怎么改阿下面是完整的代码:DesEncrypter.java
import javax.crypto.*;
import java.io.*;
import javax.crypto.spec.*;
import java.security.spec.*;public class DesEncrypter {
        Cipher ecipher;
        Cipher dcipher;
    
        // 8-byte Salt
        byte[] salt = {
            (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
            (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
        };
    
        // Iteration count
        int iterationCount = 19;
    
        public DesEncrypter(String passPhrase) {
            try {
                // Create the key
                KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
                SecretKey key = SecretKeyFactory.getInstance(
                    "PBEWithMD5AndDES").generateSecret(keySpec);
                ecipher = Cipher.getInstance(key.getAlgorithm());
                dcipher = Cipher.getInstance(key.getAlgorithm());
    
                // Prepare the parameter to the ciphers
                AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
    
                // Create the ciphers
                ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
                dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
            } catch (java.security.InvalidAlgorithmParameterException e) {
            } catch (java.security.spec.InvalidKeySpecException e) {
            } catch (javax.crypto.NoSuchPaddingException e) {
            } catch (java.security.NoSuchAlgorithmException e) {
            } catch (java.security.InvalidKeyException e) {
            }
        }
    
        public String encrypt(String str) {
            try {
                // Encode the string into bytes using utf-8
                byte[] utf8 = str.getBytes("UTF8");
                // Encrypt
                byte[] enc = ecipher.doFinal(utf8);
                // Encode bytes to base64 to get a string
                return new sun.misc.BASE64Encoder().encode(enc);
            } catch (javax.crypto.BadPaddingException e) {
            } catch (IllegalBlockSizeException e) {
            } catch (UnsupportedEncodingException e) {
            } catch (java.io.IOException e) {
            }
            return null;
        }
    
        public String decrypt(String str) {
            try {
                // Decode base64 to get bytes
                byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
    
                // Decrypt
                byte[] utf8 = dcipher.doFinal(dec);
    
                // Decode using utf-8
                return new String(utf8, "UTF8");
            } catch (javax.crypto.BadPaddingException e) {
            } catch (IllegalBlockSizeException e) {
            } catch (UnsupportedEncodingException e) {
            } catch (java.io.IOException e) {
            }
            return null;
        }
    // Here is an example that uses the class
    
public static void main(String s[])
{
try {
        // Create encrypter/decrypter class
        DesEncrypter encrypter = new DesEncrypter("cryptohahahahahadfdf");
    
        // Encrypt
        String encrypted = encrypter.encrypt("123ffd");
System.out.println(encrypted);
        // Decrypt
        String decrypted = encrypter.decrypt(encrypted);
System.out.println(decrypted);
    } catch (Exception e) {e.printStackTrace(); }

}
}

解决方案 »

  1.   

    问题出在你的密码不是ASCII,在构造函数中加一句话吧
    passPhrase=new String(passPhrase.getBytes("ISO8859-1"));
    然后会增加个异常,就可以了.
      

  2.   

    楼上的没用问题出在byte[] enc = ecipher.doFinal(utf8);这一句,是doFinal方法的问题应该
    但是还找不出为什么会抛nullpointerexception
      

  3.   

    你的ecipher是null的,所以出错,
    一下是我修改后的构造函数,你测试一下就清楚了
            try {
    //          Create the key
                passPhrase=new String(passPhrase.getBytes("ISO8859-1"));
                KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
                SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
                ecipher = Cipher.getInstance(key.getAlgorithm());
                dcipher = Cipher.getInstance(key.getAlgorithm());
                
    //          Prepare the parameter to the ciphers
                AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
                
    //          Create the ciphers
                ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
                dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
            } catch (java.security.InvalidAlgorithmParameterException e) {
                System.out.println(e.getMessage());
            } catch (java.security.spec.InvalidKeySpecException e) {
                System.out.println(e.getMessage());
            } catch (javax.crypto.NoSuchPaddingException e) {
                System.out.println(e.getMessage());
            } catch (java.security.NoSuchAlgorithmException e) {
                System.out.println(e.getMessage());
            } catch (java.security.InvalidKeyException e) {
                System.out.println(e.getMessage());
            } catch (UnsupportedEncodingException e) {
                System.out.println(e.getMessage());
            }