安全和密码

加密算法

MessageDigest md5 = MessageDigest.getInstance("MD5");
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] data = "Hello".getBytes();
System.out.println("MD5 hash: " + DatatypeConverter.printHexBinary(md5.digest(data)));
System.out.println("SHA1 hash: " + DatatypeConverter.printHexBinary(sha1.digest(data)));
System.out.println("SHA256 hash: " + DatatypeConverter.printHexBinary(sha256.digest(data))); MD5 hash: 8B1A9953C4611296A827ABF8C47804D7
SHA1 hash: F7FF9E8B7BB2E09B70935A5D785E0CC5D9D0ABF0
SHA256 hash: 185F8DB32271FE25F561A6FC938B2E264306EC304EDA518007D1764826381969

公钥和私钥加密解密

生成私钥和公钥

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(1024, random); KeyPair keyPair = keyGen.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
System.out.println("publicKey: "+DatatypeConverter.printHexBinary(publicKey.getEncoded()));
System.out.println("privateKey: "+DatatypeConverter.printHexBinary(privateKey.getEncoded())); publicKey: 30819F300D06092A864886F70D010101050003818D003081890281810092A0C7715C0816FDB426C3A294C85BC3A0DCDEF34F460C7E4E9A861CEF05CFBF3BD2BCF4F33A73C9E498F54D655DAA6474C827D2C9FABA68E419F7D17ED2EAD82916907461962E541CEE7A13B36D3E05B3EFD267935A4C2A99B8E74ADF4A4D6219358080982789DC4A06BD1D224B479FEF97BFC5EC66ED4DFE9DA93F0A76A3390203010001
privateKey: 30820275020100300D06092A864886F70D01010105000482025F3082025B0201000281810092A0C7715C0816FDB426C3A294C85BC3A0DCDEF34F460C7E4E9A861CEF05CFBF3BD2BCF4F33A73C9E498F54D655DAA6474C827D2C9FABA68E419F7D17ED2EAD82916907461962E541CEE7A13B36D3E05B3EFD267935A4C2A99B8E74ADF4A4D6219358080982789DC4A06BD1D224B479FEF97BFC5EC66ED4DFE9DA93F0A76A33902030100010281802BDC544CA08EFAF1A136074756E22830F8A9993786FB19399F21B2046DAB7B5D64F3C0FA34D7C3531D4273FE56D811E442DAACAEED244726A134F36083636C440FE6CC9F06F4CA3686FA169437DBB3A84B166307D417FBC174C17CCB45C60E439A772247CAC0044B62781EE55A996F5846469412A093648C2416C3AE1B4D5601024100DE2AE8944A8BE34A8D903394ED9CFE585D5B3942CE9A50F48092B9FDB2BF09EF561C43CD933A396A73F7257B647893E83B1817EDFCC4D85226B3BE08DDE0D971024100A8F4FFB5A10BA2CCBE6C5BB973C128CC6F187646431B64A7487CB5775882047BFF7F73243287ADCB89DB8C9753CDA982750C8D8DCEA59E85C9254FE86921C249024021FBFC51DAC9653E5D4346B0AC5C4C87E5D7F37D65AF335D0361EE73816939B2D3C0451AE83F8AD2DEE45A6672AB032D0E05147C02237657B2B5E206E731E2F10240577FA9FDD20D489E67EF8E0118E56BCF93A9437757C193FADE1B578F1B131A676D0600073AF3F1197BA76BC3E2EE9B5EA8677BD778B164CB3C444A81BE1071A1024027D70862918AD84D633E7A45BBDA35B676C336AA2AB86F5F180F2D7AC582D9AC85EECF494CE40102CC3D6522C85B0FD3733518F923D0703AD7CA31C706225667

加密数据

Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");

X509EncodedKeySpec keySpec = new X509EncodedKeySpec(DatatypeConverter.parseHexBinary("30819F300D06092A864886F70D010101050003818D003081890281810092A0C7715C0816FDB426C3A294C85BC3A0DCDEF34F460C7E4E9A861CEF05CFBF3BD2BCF4F33A73C9E498F54D655DAA6474C827D2C9FABA68E419F7D17ED2EAD82916907461962E541CEE7A13B36D3E05B3EFD267935A4C2A99B8E74ADF4A4D6219358080982789DC4A06BD1D224B479FEF97BFC5EC66ED4DFE9DA93F0A76A3390203010001"));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
rsa.init(Cipher.ENCRYPT_MODE, publicKey);
rsa.update("aaaaaaaa".getBytes());
byte[] result = rsa.doFinal();
System.out.println("Encrypted: " + DatatypeConverter.printHexBinary(result)); Encrypted: 31C7C41D4DEA78E13D923E00B9C85CFC1DD50F61CA9E1F1B99D5A5D010DB8C093992F7EFF6DEAE6B462F28C141A23DD27EC3E954951E509F530BBBBFD0D6DF5FBCFA3D412D9980EEC8F9020592927373BDB53D241BEBD2F8C2BD17B92B0A82AF4DAEA9142936A03142AD46E7E26DE67D14D07AD9E7CC818E706D4FD2B30368AA

解密数据

Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(DatatypeConverter.parseHexBinary("30820275020100300D06092A864886F70D01010105000482025F3082025B0201000281810092A0C7715C0816FDB426C3A294C85BC3A0DCDEF34F460C7E4E9A861CEF05CFBF3BD2BCF4F33A73C9E498F54D655DAA6474C827D2C9FABA68E419F7D17ED2EAD82916907461962E541CEE7A13B36D3E05B3EFD267935A4C2A99B8E74ADF4A4D6219358080982789DC4A06BD1D224B479FEF97BFC5EC66ED4DFE9DA93F0A76A33902030100010281802BDC544CA08EFAF1A136074756E22830F8A9993786FB19399F21B2046DAB7B5D64F3C0FA34D7C3531D4273FE56D811E442DAACAEED244726A134F36083636C440FE6CC9F06F4CA3686FA169437DBB3A84B166307D417FBC174C17CCB45C60E439A772247CAC0044B62781EE55A996F5846469412A093648C2416C3AE1B4D5601024100DE2AE8944A8BE34A8D903394ED9CFE585D5B3942CE9A50F48092B9FDB2BF09EF561C43CD933A396A73F7257B647893E83B1817EDFCC4D85226B3BE08DDE0D971024100A8F4FFB5A10BA2CCBE6C5BB973C128CC6F187646431B64A7487CB5775882047BFF7F73243287ADCB89DB8C9753CDA982750C8D8DCEA59E85C9254FE86921C249024021FBFC51DAC9653E5D4346B0AC5C4C87E5D7F37D65AF335D0361EE73816939B2D3C0451AE83F8AD2DEE45A6672AB032D0E05147C02237657B2B5E206E731E2F10240577FA9FDD20D489E67EF8E0118E56BCF93A9437757C193FADE1B578F1B131A676D0600073AF3F1197BA76BC3E2EE9B5EA8677BD778B164CB3C444A81BE1071A1024027D70862918AD84D633E7A45BBDA35B676C336AA2AB86F5F180F2D7AC582D9AC85EECF494CE40102CC3D6522C85B0FD3733518F923D0703AD7CA31C706225667"));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
rsa.init(Cipher.DECRYPT_MODE, privateKey);
rsa.update(DatatypeConverter.parseHexBinary("31C7C41D4DEA78E13D923E00B9C85CFC1DD50F61CA9E1F1B99D5A5D010DB8C093992F7EFF6DEAE6B462F28C141A23DD27EC3E954951E509F530BBBBFD0D6DF5FBCFA3D412D9980EEC8F9020592927373BDB53D241BEBD2F8C2BD17B92B0A82AF4DAEA9142936A03142AD46E7E26DE67D14D07AD9E7CC818E706D4FD2B30368AA"));
System.out.println(new String(rsa.doFinal())); aaaaaaaa

公钥私钥生成的不同算法

KeyPairGenerator dhGenerator = KeyPairGenerator.getInstance("DiffieHellman");
KeyPairGenerator dsaGenerator = KeyPairGenerator.getInstance("DSA");
KeyPairGenerator rsaGenerator = KeyPairGenerator.getInstance("RSA");
dhGenerator.initialize(1024);
dsaGenerator.initialize(1024);
rsaGenerator.initialize(2048); KeyPair dhPair = dhGenerator.generateKeyPair();
KeyPair dsaPair = dsaGenerator.generateKeyPair();
KeyPair rsaPair = rsaGenerator.generateKeyPair(); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048, SecureRandom.getInstance("SHA1PRNG", "SUN"));
KeyPair pair = generator.generateKeyPair();

密钥签名

计算签名
PrivateKey privateKey = keyPair.getPrivate();
byte[] data = "test".getBytes();
Signature signer = Signature.getInstance("SHA1withRSA");
signer.initSign(privateKey);
signer.update(data);
byte[] signature = signer.sign(); 验证签名
PublicKey publicKey = keyPair.getPublic();
Signature verifier = Signature.getInstance("SHA1withRSA");
verifier.initVerify(publicKey);
verifier.update(data);
System.out.println("Signature: " + verifier.verify(signature));

生成加密随机数

基本用法

byte[] sample = new byte[16];
new SecureRandom().nextBytes(sample);
System.out.println("Sample: " + DatatypeConverter.printHexBinary(sample)); Sample: 4EB994814572D063F38E9F3C2ED6AE7A

指定算法

byte[] sample = new byte[16];
SecureRandom randomness = SecureRandom.getInstance("SHA1PRNG", "SUN");
randomness.nextBytes(sample);
System.out.println("Provider: " + randomness.getProvider());
System.out.println("Algorithm: " + randomness.getAlgorithm());
System.out.println("Sample: " + DatatypeConverter.printHexBinary(sample)); Provider: SUN version 10
Algorithm: SHA1PRNG
Sample: E06E5F7689123DE0EBE4A35EEA8B65F2

加密对象

SealedObject

Serializable obj = new String("John");
// 生成密钥
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey aesKey = kgen.generateKey(); Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
SealedObject sealedObject = new SealedObject(obj, cipher);
System.out.println("sealedObject-" + sealedObject);
System.out.println("sealedObject Data-" + sealedObject.getObject(aesKey));

Signature

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024, random);
PrivateKey signingKey = keyGen.generateKeyPair().getPrivate(); Signature signingEngine = Signature.getInstance("DSA");
signingEngine.initSign(signingKey); Serializable obj = new String("John");
SignedObject signedObject = new SignedObject(obj, signingKey, signingEngine);
System.out.println("signedObject-" + signedObject);
System.out.println("signedObject Data-" + signedObject.getObject());

结语

本文章是java成神的系列文章之一

如果你想知道,但是本文没有的,请下方留言

我会第一时间总结出来并发布填充到本文

java成神之——安全和密码的更多相关文章

  1. 转载_2016,Java成神初年

    原文地址:http://blog.csdn.net/chenssy/article/details/54017826 2016,Java成神初年.. -------------- 时间2016.12. ...

  2. Java成神路上之设计模式系列教程之一

    Java成神路上之设计模式系列教程之一 千锋-Feri 在Java工程师的日常中,是否遇到过如下问题: Java 中什么叫单例设计模式?请用Java 写出线程安全的单例模式? 什么是设计模式?你是否在 ...

  3. java成神之——网络编程基本操作

    网络编程 获取ip UDP程序示例 TCP程序 结语 网络编程 获取ip InetAddress id = InetAddress.getLocalHost(); // InetAddress id ...

  4. java成神之——MySQL Connector/J 的基本使用

    使用示例 DBCP连接池 结语 使用示例 public class demo { static Connection con = null; static Statement st = null; s ...

  5. java成神之——线程操作

    线程 Future CountDownLatch Multithreading synchronized Thread Producer-Consumer 获取线程状态 线程池 ThreadLocal ...

  6. java成神之——Fork/Join基本使用

    Fork/Join 大任务分小任务,小任务结果合并 ForkJoinPool pool = new ForkJoinPool(); RecursiveTask<Integer> task1 ...

  7. java成神之——HttpURLConnection访问api

    HttpURLConnection 访问get资源 访问post资源 访问Delete资源 获取状态码 结语 HttpURLConnection 访问get资源 HttpURLConnection c ...

  8. java成神之——jaxb操作xml的基本使用

    JAXB 依赖 读取xml配置 写配置 自定义写配置 结语 JAXB 依赖 <dependency> <groupId>javax.activation</groupId ...

  9. java成神之——文件IO

    文件I/O Path Files File类 File和Path的区别和联系 FileFilter FileOutputStream FileInputStream 利用FileOutputStrea ...

随机推荐

  1. 009——VUE中watch监听属性变化实现类百度搜索栏功能

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 003——VUE操作元素属性

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. USB转串口WIN8驱动安装

    http://jingyan.baidu.com/article/11c17a2c0bb606f446e39da0.html  //查看百度经验 http://jingyan.baidu.com/ar ...

  4. NSSet基本使用

    int main(int argc, const char * argv[]) { @autoreleasepool { //创建一个集合对象 注:如果集合中写了两次或多次同一个对象 打印只能看到一个 ...

  5. MySqlDBHelper数据库连接

    这里是本人在工作中用到,希望给大家帮助 public class MySqlDBHelper { //获取一个记录器 private static readonly log4net.ILog log ...

  6. Js中分号使用总结

    作者:尤雨溪链接:https://www.zhihu.com/question/20298345/answer/49551142来源:知乎著作权归作者所有,转载请联系作者获得授权. 没有应该不应该,只 ...

  7. 在Linux上部署DotNet Core项目的时候发现Apache无法转发Kestrel的5000端口的问题

    CentOS服务器上使用Apache托管Dotnet Core2,服务启动了,端口也打开了.在本地使用curl命令可以访问5000端口的网页内容,但是访问Apache的端口却无法获取到网页的内容.想起 ...

  8. JavaScript中的二分法插入算法

    算法主体部分 var OnlineUser = { //list : 待查找的数组 //key : 待插入的值 //order : 数组的顺序 1:从小到大 0:从大到小 //start : 开始查找 ...

  9. (四)java基本语法

    关键字 被java赋予了特殊意义的单词: class,new,private,protected,public,static,final,abstract,interface,this,super,I ...

  10. CVS winCVS配置讲解及用户管理

    首先 用到工具包包括 CVSNT ,WinCvs及相关python 2.2.3和TCL832 可点此链接下载 http://files.cnblogs.com/lppblogs/%E6%96%87%E ...