java实现DESede和RSA加解密
程序员文章站
2022-07-04 16:01:26
...
1.DESede加解密:
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/php/c#版rsa签名以及java验签实现--转
-
Java 获取当前类名和方法名的实现方法
-
java中线程实现方式(execute和submit方式的区别)
-
基于spring-boot和docker-java实现对docker容器的动态管理和监控功能[附完整源码下载]
-
PHP socket实现 readInt 和 readString 以及和Java端对接
-
java并发编程(十三)- 显示锁使用Lock和Condition实现等待通知模式
-
Java基于Springboot实现实体设计和用户登陆和分页显示
-
AndroidStudio中使用XML和Java代码混合控制UI界面实现QQ相册照片列表页面
-
[Python3] RSA的加解密和签名/验签实现 -- 使用pycrytodome
-
Java实现求子数组和的最大值算法示例