AES算法

来源:互联网 发布:开源软件下载 编辑:程序博客网 时间:2024/04/30 12:21
 AES算法,希望对需要的人有所帮助
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.security.interfaces.*;
import java.io.*;
public class ContentCrypto {

    /**
     * Creates a new instance of ContentCrypto
     */
    public ContentCrypto() {
    }
    
    public Key keyGeneration() {
        KeyGenerator keyGenerator = null;
        try {
            keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return keyGenerator.generateKey();
    }
    
    public KeyPair generateKeyPair(long seed)throws Exception {
        KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA");
        keyGenerator.initialize(1024);

        return (keyGenerator.generateKeyPair());
    }

    
    public byte[] encryptContent(Key key, byte[] data) {
        byte[] encryptedData = null;
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.ENCRYPT_MODE, key);
            encryptedData = c.doFinal(data);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return encryptedData;
    }
    
    // simple method of encrypting entire document file
    public byte[] encryptKey(PublicKey key1, Key key2) {
        byte[] bKey = null;
        try {
            Cipher c = Cipher.getInstance("RSA");
            c.init(Cipher.ENCRYPT_MODE, key1);
            bKey = c.doFinal(key2.getEncoded());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bKey;
    }
    
    // simple method of decrypting entire document file
    public Key decryptKey(PrivateKey key1, byte[] bKey) {
        Key key = null;
        try {
            Cipher c = Cipher.getInstance("RSA");
            c.init(Cipher.DECRYPT_MODE, key1);
            key = new SecretKeySpec(c.doFinal(bKey), "AES");
        } catch(Exception e) {
            e.printStackTrace();
        }
        return key;
    }
    
    public byte[] decryptContent(Key key, byte[] encryptedData) {
        byte[] data = null;
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.DECRYPT_MODE, key);
            data = c.doFinal(encryptedData);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return data;
    }
    
    public byte[] encryptPlainTextKey(PublicKey key1, byte[] key2) {
        byte[] bKey = null;
        try {
            Cipher c = Cipher.getInstance("RSA");
            c.init(Cipher.ENCRYPT_MODE, key1);
            bKey = c.doFinal(key2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bKey;
    }
    
    // simple method of decrypting entire document file
    public byte[] decryptPlainTextKey(PrivateKey key1, byte[] bKey) {
        byte[] key = null;
        try {
            Cipher c = Cipher.getInstance("RSA");
            c.init(Cipher.DECRYPT_MODE, key1);
            key = c.doFinal(bKey);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return key;
    }    
    
    public static void main(String[] args) {
        Provider[] p = Security.getProviders();
        for(int i=0;i<p.length; i++) {
            System.out.println("Provider["+i+"]="+p[i].getInfo());
        }        ContentCrypto cc = new ContentCrypto();
        KeyPair kp = null;
        try {
            kp = cc.generateKeyPair(1234567890);
        } catch (Exception e) {
            e.printStackTrace();
        }
        RSAPrivateKey privK = (RSAPrivateKey)kp.getPrivate();
        RSAPublicKey pubK = (RSAPublicKey)kp.getPublic();
        System.out.println("Private Key Info");
        System.out.println("Algorithm=" + privK.getAlgorithm());
        System.out.println("Encoded=" + (new String(privK.getEncoded())));
        System.out.println("Format=" + privK.getFormat());
        System.out.println("Modulus=" + privK.getModulus());
        System.out.println("Exponent=" + privK.getPrivateExponent());
        System.out.println("Public Key Info");
        System.out.println("Algorithm=" + pubK.getAlgorithm());
        System.out.println("Encoded=" + (new String(pubK.getEncoded())));
        System.out.println("Format=" + pubK.getFormat());
        System.out.println("Modulus=" + pubK.getModulus());
        System.out.println("Exponent=" + pubK.getPublicExponent());
        byte[] data = new byte[94*5];
        for(int i=0; i<94; i++) {
            data[i] = (byte)(i+33);
            data[i+94] = data[i];
            data[i+188] = data[i];
            data[i+282] = data[i];
            data[i+376] = data[i];
        }
        System.out.println("Data="+(new String(data)));
        System.out.println("Generating key");
        Key key = cc.keyGeneration();
        System.out.println("Key="+(new String(key.getEncoded())));
        
        System.out.println("Encrypting data...");
        byte[] encryptedData = cc.encryptContent(key, data);
        System.out.println("Encrypted data="+(new String(encryptedData)));
        
        System.out.println("Encrypting key...");
        byte[] encryptedKey = cc.encryptKey(pubK,key);
        System.out.println("Encrypted key="+(new String(encryptedKey)));
        
        System.out.println("Decrypting key...");
        Key decryptedKey = cc.decryptKey(privK, encryptedKey);
        System.out.println("Decrypted key="+(new String(decryptedKey.getEncoded())));
        
        System.out.println("Is Original Key same as Decrypted Key? "+(key.equals(decryptedKey)));
        System.out.println("Key size = "+key.getEncoded().length+"/tDecrypted Key size = "+decryptedKey.getEncoded().length);
        
        System.out.println("Decrypting data...");
        byte[] decryptedData = cc.decryptContent(decryptedKey,encryptedData);
        System.out.println("Decrypted data="+(new String(decryptedData)));

 
        
    }
}