WordPress.com



import java.io.FileOutputStream;

import java.io.IOException;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import java.security.spec.InvalidKeySpecException;

import javax.crypto.KeyGenerator;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.PBEKeySpec;

import javax.crypto.spec.SecretKeySpec;

//source point

public class AES_Genkey {

static long startTime, estimatedTime;

public static void main(String[] args) throws Exception {

startTime = System.nanoTime();

SecretKeySpec spec = getKeySpec2();

estimatedTime = (System.nanoTime() - startTime) / 1000000;

System.out.println("Generate AES Key time (milisecond) : " + estimatedTime);

}

private static SecretKeySpec getKeySpec2() throws NoSuchAlgorithmException,

NoSuchPaddingException,InvalidKeyException, IOException, InvalidKeySpecException

{

final String password = "test";

int pswdIterations = 65536;

int keySize = 128;

byte[] saltBytes = { 0, 1, 2, 3, 4, 5, 6 };

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, pswdIterations,

keySize);

SecretKey secretKey = factory.generateSecret(spec);

SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

FileOutputStream fos = new FileOutputStream("D:\\aes\\secretkey.key");

fos.write(secret.getEncoded());

fos.close();

return secret;

}

public String generateSalt() {

SecureRandom random = new SecureRandom();

byte bytes[] = new byte[20];

random.nextBytes(bytes);

String s = new String(bytes);

return s;

}

public void generateAESKey(String filePath) throws Exception {

KeyGenerator keyGen = KeyGenerator.getInstance("AES");

keyGen.init(128);

SecretKey skey = keyGen.generateKey();

byte[] raw = skey.getEncoded();

FileOutputStream fos = new FileOutputStream(filePath);

fos.write(raw);

fos.close();

}

}

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Base64;

import org.apache.poi.hwpf.HWPFDocument;

import org.apache.poi.hwpf.extractor.WordExtractor;

//target point

public class EncryptionAES6 {

static String plaintext ="" ;

public static void main(String[] args) throws Exception {

encryptedData();

}

public static void encryptedData() throws Exception {

Base64.Encoder encoder = Base64.getUrlEncoder();

AES3 aes = new AES3();

ReadFile();

byte[] encryptedData = aes.encrypt(plaintext.getBytes("UTF-8"));

//System.out.println("encrypted data : " + encoder.encodeToString(encryptedData));

FileOutputStream fos = new FileOutputStream("D:\\aes\\encryptedDataFile.txt");

String strToWrite = encoder.encodeToString(encryptedData);

fos.write(strToWrite.getBytes());

fos.close();

}

public static void ReadFile() {

File file = null;

WordExtractor extractor = null;

try {

file = new File("d:\\aes\\input\\input.doc");

FileInputStream fis = new FileInputStream(file.getAbsolutePath());

HWPFDocument document = new HWPFDocument(fis);

extractor = new WordExtractor(document);

String[] fileData = extractor.getParagraphText();

for (int i = 0; i < fileData.length; i++) {

if (fileData[i] != null) {

// System.out.println(fileData[i]);

plaintext += fileData[i];

}

}

// System.out.println("plaintext : \n"+plainText);

} catch (Exception exep) {

exep.printStackTrace();

}

}

}

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Base64;

//target point

public class DecryptionAES6 {

static Base64.Encoder encoder = Base64.getEncoder();

static String plaintext = "";

public static void main(String[] args) throws Exception {

decryptedData();

}

public static void decryptedData() throws Exception {

AES3 aes = new AES3();

String encryptedDataFile = "D:\\aes\\encryptedDataFile.txt";

File dataFile = new File(encryptedDataFile);

FileInputStream fis = new FileInputStream(dataFile);

byte[] dataBytes = new byte[fis.available()];

fis.read(dataBytes);

fis.close();

Base64.Decoder decoder = Base64.getUrlDecoder();

byte[] decryptedData = aes.decrypt(decoder.decode(dataBytes));

String decryptedDataFilePath = "D:\\aes\\decryptedDataFile.txt";

FileOutputStream fos = new FileOutputStream(new File(decryptedDataFilePath));

fos.write(decryptedData);

fos.close();

}

}

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download