java成神之——安全和密码
Posted 叶家伟的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java成神之——安全和密码相关的知识,希望对你有一定的参考价值。
安全和密码
加密算法
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成神之——安全和密码的主要内容,如果未能解决你的问题,请参考以下文章