⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a1encryption.java

📁 一个用java实现的bbs论坛系统...........................
💻 JAVA
字号:
/**
 * @author woexpert@yahoo.com
 * @version v0 110100
 */

import au.net.aba.crypto.provider.ABAProvider;
import java.security.Security;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;

public class A1Encryption {

	private static final boolean DEBUG = false;

    // public class IvParameterSpec extends Object
    //   implements java.security.spec.AlgorithmParameterSpec
    // This class specifies an initialization vector (IV).
    // IVs are used by ciphers in feedback mode, e.g., DES in CBC mode.
    private static IvParameterSpec ivParamSpec;

    private static Key key;
    private static Cipher cipher;
    
    static {
        // Initialize security stuff:
        try {
            Security.addProvider(new ABAProvider());
            ivParamSpec = new IvParameterSpec("Unique Key".getBytes());
            key = KeyGenerator.getInstance("DES", "ABA").generateKey();
            cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        } catch (Exception e) {
            System.err.println("Err: " + e);
            System.exit(1);
        }
    }

    public static byte [] encryptPwd(final String sOrig) {
        // We should use MD5 (return fixed 16 bytes),
        // or SHA-1 (return fixed 20 bytes). (hold)
        
        // The following is temporary:
        int nHash = sOrig.hashCode();
        // An int is 4 bytes:
        byte byte1 = (byte) (nHash & 0xff000000);
        byte byte2 = (byte) (nHash & 0x00ff0000);
        byte byte3 = (byte) (nHash & 0x0000ff00);
        byte byte4 = (byte) (nHash & 0x000000ff);
        if (DEBUG) System.out.println(
        	" byte1: " + byte1 + " byte2: " + byte2 +
        	" byte3: " + byte3 + " byte4: " + byte4);
        byte [] pbyteEnc = {byte1, byte2, byte3, byte4};
        return pbyteEnc;
    }
    
    /**
     * Encrypt a byte array with DES.
     * The encrypted result is different each time we run
     * this class A1Encryption.
     */
    public static byte [] encryptWithDes(final byte [] pbyteOrig) {
        try {
            cipher.init(Cipher.ENCRYPT_MODE, key, ivParamSpec);
            byte [] pbyteEncrypted = cipher.doFinal(pbyteOrig);
			return pbyteEncrypted;
        } catch (Exception e) {
            System.err.println("Err: " + e);
            return null; // (hold)
        }
    }

    public static byte [] encryptWithDes(final String sOrig) {
        byte [] pbyteOrig = sOrig.getBytes();
        if (DEBUG) {
            System.out.println("pbyteOrig: " + pbyteOrig);
            for (int i=0; i<pbyteOrig.length; i++)
            	System.out.println("pbyteOrig[" + i + "]: " + pbyteOrig[i]);
        }
        byte [] pbyteEncrypted = encryptWithDes(pbyteOrig);
		return pbyteEncrypted;
    }

    public static byte [] decryptToByteArrayWithDes(final byte [] pbyteOrig) {
        try {
            cipher.init(Cipher.DECRYPT_MODE, key, ivParamSpec);
            byte [] pbyteDecrypted = cipher.doFinal(pbyteOrig);
            return pbyteDecrypted;
        } catch (Exception e) {
            System.err.println("Err: " + e);
            return null;
        }
    }

    public static String decryptToStringWithDes(final byte [] pbyteOrig) {
		byte [] pbyteDecrypted = decryptToByteArrayWithDes(pbyteOrig);
		String sDecrypted = new String(pbyteDecrypted);
		return sDecrypted;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -