欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java实现DESede和RSA加解密

程序员文章站 2022-07-04 16:01:26
...

 

1.DESede加解密:
java实现DESede和RSA加解密
            
    
    博客分类: java编程 java加密解密RSADES 
 

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

/**
 * 对称加密与解密
 * @author Administrator
 *
 */
public class EncryptData {
	//要加密的明文
	static String str = "123456adasdadqsadqweds7890";
	public static void main(String[] args) {
		System.out.println("明文:"+str);
		try {
			//指定编码特征为utf-8
			byte[] plain1 = str.getBytes("UTF-8");
			//创建密钥生成器 加密算法为DESede
			KeyGenerator generator = KeyGenerator.getInstance("DESede");
			generator.init(168);
			//获得密钥
			SecretKey secretKey = generator.generateKey();
			//获取密钥的二进制编码格式
			byte[] kb = secretKey.getEncoded();
			System.out.println("密钥:");
			//打印密钥 每行8个
			for (int j = 0; j < kb.length; j++) {
				System.out.print(kb[j]+",");
				if(j%8==7) System.out.println();
			}
			//创建加密对象,加密算法DESede
			Cipher cipher = Cipher.getInstance("DESede");
			//加密对象初始化
			cipher.init(Cipher.ENCRYPT_MODE, secretKey);
			//密文存放在字节数组中
			byte[] encrypt = cipher.doFinal(plain1);
			System.out.println("密文:");
			//打印密文
			for (int j = 0; j < encrypt.length; j++) {
				System.out.print(encrypt[j]+",");
				if(j%8==7) System.out.println();
			}
			//切换解密模式
			cipher.init(Cipher.DECRYPT_MODE, secretKey);
			//解密数组
			byte[] cs = cipher.doFinal(encrypt);
			String data = new String(cs);
			System.out.println("解密:"+data);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
	}
}

 2。RSA加解密

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

public class RSAEncrypt {
	public static void main(String[] args) {
		try {
			//创建密钥生成器,加密算法为RSA
			KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
			//初始化密钥对象,RAS密钥长度为510-2048
			generator.initialize(1024);
			//生成密钥对
			KeyPair keyPair = generator.genKeyPair();
			//获取公钥
			PublicKey pbk = keyPair.getPublic();
			//获取私钥
			PrivateKey prk = keyPair.getPrivate();
			//建立文件输出流 保存公钥
			FileOutputStream fileOutputStream = new FileOutputStream("d:\\key.dat");
			//文件对象输出流
			ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
			//向文件key.dat中输出对象
			objectOutputStream.writeObject(pbk);
			//建立文件输出流 保存私钥
			FileOutputStream fout = new FileOutputStream("d:\\key.dat");
			//文件对象输出流
			ObjectOutputStream objectOutputStream2 = new ObjectOutputStream(fout);
			objectOutputStream2.writeObject(prk);
			//获得公钥,计算指数和模数
			RSAPublicKey pkey = (RSAPublicKey) keyPair.getPublic();
			BigInteger in = pkey.getPublicExponent();
			BigInteger en = pkey.getModulus();
			//明文
			String pltext = "I hava e friend";
			byte[] pb = pltext.getBytes("UTF-8");
			BigInteger bigInteger = new BigInteger(pb);
			//执行加密
			BigInteger bp = bigInteger.modPow(in, en);
			//输出密文
			System.out.println("bi:"+bp);
			//获取私钥以及解密
			RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
			BigInteger nv = privateKey.getPrivateExponent();
			BigInteger mv = privateKey.getModulus();
			//执行解密
			BigInteger bi = bp.modPow(nv, mv);
			//打印明文
			byte[] mt = bi.toByteArray();
			for (int i = 0; i < mt.length; i++) {
				System.out.print((char)mt[i]);
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}

 

  • java实现DESede和RSA加解密
            
    
    博客分类: java编程 java加密解密RSADES 
  • 大小: 46 KB