![]() ![]() ![]() ![]() |
|||||
|
|||||
樓主 RealMannn ![]()
![]() |
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /******************************************************************************* * AES加解密算法 */ public class AES { private static final String ALGORITHM = "AES"; private static final String TRANSFORM = "AES/CBC/PKCS5Padding"; private static final byte[] miv = {(byte) 0xCC, (byte) 0x31, (byte) 0x41, (byte) 0x4F, (byte) 0xA2, (byte) 0x45, (byte) 0x6D, (byte) 0x61, (byte) 0x35, (byte) 0x6E, (byte) 0x49, (byte) 0x63, (byte) 0x49, (byte) 0xD1, (byte) 0x50, (byte) 0x39}; private static final IvParameterSpec iv = new IvParameterSpec(miv); private SecretKey key; public AES() { } public void SetKey(byte[] bkey) throws IllegalArgumentException { key = new SecretKeySpec(bkey, ALGORITHM); } public byte[] GetKey() { if (key == null) return null; else return key.getEncoded(); } public String Encrypt(String sSrc) throws Exception { // generate key KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); keyGenerator.init(128); key = keyGenerator.generateKey(); Cipher cipher = Cipher.getInstance(TRANSFORM); cipher.init(Cipher.ENCRYPT_MODE, key, iv); byte[] encrypted = cipher.doFinal(sSrc.getBytes()); String enString = new BASE64Encoder().encode(encrypted); //BASE64 return enString; } public String Decrypt(String etext, byte [] bkey) throws Exception { SetKey(bkey); return Decrypt(etext); } public String Decrypt(String sSrc) throws Exception { if (key == null) return null; Cipher cipher = Cipher.getInstance(TRANSFORM); cipher.init(Cipher.DECRYPT_MODE, key, iv); byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc); //BASE64 byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original); return originalString; } public static void main(String[] args) throws Exception { AES aes = new AES(); /* SetKey byte[] bKey = {(byte) 0x95, (byte) 0xE1, (byte) 0xBD, (byte) 0x58, (byte) 0x7D, (byte) 0xCC, (byte) 0x41, (byte) 0x08, (byte) 0xF5, (byte) 0x97, (byte) 0x55, (byte) 0x92, (byte) 0x23, (byte) 0x2E, (byte) 0x5F, (byte) 0x01}; aes.SetKey(bKey); */ String s = "The quick brown fox jumps"; System.out.println("String: " + s); String e = aes.Encrypt(s); System.out.println("Encryption: " + e); String d = aes.Decrypt(e); System.out.println("Dncryption: " + d); /* export key, decrypt byte [] bkey = aes.GetKey(); AES a1 = new AES(); d = a1.Decrypt(e, bkey); System.out.println("Encryption = " + d); */ } }
搜尋相關Tags的文章:
[ AES ] ,
本篇文章發表於2013-09-06 11:22 |
1樓
作者回應
RealMannn ![]() |
package com.security;
import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Sha2 { private static final String SHA = "SHA-256"; public static byte[] getSHA2(byte[] s) { try { MessageDigest md = MessageDigest.getInstance(SHA); md.update(s); byte[] hash = md.digest(); return hash; } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); } return null; } public static byte[] getSHA2(String s) { try { MessageDigest md = MessageDigest.getInstance(SHA); md.update(s.getBytes("UTF-8")); byte[] hash = md.digest(); return hash; } catch (IOException ex) { ex.printStackTrace(); } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); } return null; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //Sha2 sha = new Sha2(); //String s = "orderNo=12345"; //System.out.println(sha.getSHA2(s)); } }
本篇文章回覆於2013-09-06 15:43
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
2樓
作者回應
RealMannn ![]() |
都是好用的範例XD
本篇文章回覆於2013-09-06 15:46
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
3樓
作者回應
RealMannn ![]() |
package com.security;
import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; public class RSAUtils { private static PublicKey pubKey = null; private static PrivateKey priKey = null; public static void loadRSAPubKey(String filename) throws IOException { pubKey = (PublicKey) Utils.readPublicKeyFile(filename); } public static void loadRSAPriKey(String filename) { priKey = (PrivateKey) Utils.readPrivateKeyFile(filename); } public static PublicKey getPublicKey() { return pubKey; } public static PrivateKey getPrivateKey() { return priKey; } public static String pubEncrypt(byte[] sessKey) { try { Cipher desCipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"); desCipher.init(Cipher.ENCRYPT_MODE, getPublicKey()); return Utils.bytesToBase64Str(desCipher.doFinal(sessKey)); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static String priEncrypt(byte[] hash) { try { Cipher desCipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); desCipher.init(Cipher.ENCRYPT_MODE, getPrivateKey()); return Utils.bytesToBase64Str(desCipher.doFinal(hash)); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static byte[] priDecrypt(String encryptStr) { try { Cipher desCipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"); desCipher.init(Cipher.DECRYPT_MODE, getPrivateKey()); return desCipher.doFinal(Utils.base64StrToBytes(encryptStr)); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static byte[] pubDecrypt(String encryptStr) { try { Cipher desCipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); desCipher.init(Cipher.DECRYPT_MODE, getPublicKey()); return desCipher.doFinal(Utils.base64StrToBytes(encryptStr)); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void main(String[] arg) { } }
本篇文章回覆於2013-09-06 15:50
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
4樓
作者回應
RealMannn ![]() |
<pre lang="JAVA">
package com.security; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class Utils { public static PublicKey readPublicKeyFile(String filename) throws IOException { try { String sysRootKey = "VPlatform.install.root"; String root = System.getProperty(sysRootKey); File f = new File(root + "\\WEB-INF\\" + filename); byte[] b = read(f); PublicKey key = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(b)); return key; } catch (Exception e){ e.printStackTrace(); } return null; } public static PrivateKey readPrivateKeyFile(String filename) { try { String sysRootKey = "VPlatform.install.root"; String root = System.getProperty(sysRootKey); File f = new File(root + "\\WEB-INF\\" + filename); byte[] b = read(f); PrivateKey key = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(b)); return key; } catch (Exception e){ e.printStackTrace(); } return null; } public static String bytesToBase64Str(byte[] bytes) { return new BASE64Encoder().encode(bytes); } public static byte[] base64StrToBytes(String base64Str) { try { return new BASE64Decoder().decodeBuffer(base64Str); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static byte[] read(File file) throws IOException { ByteArrayOutputStream ous = null; InputStream ios = null; try { byte[] buffer = new byte[4096]; ous = new ByteArrayOutputStream(); ios = new FileInputStream(file); int read = 0; while ( (read = ios.read(buffer)) != -1 ) { ous.write(buffer, 0, read); } } finally { try { if ( ous != null ) ous.close(); } catch ( IOException e) { } try { if ( ios != null ) ios.close(); } catch ( IOException e) { } } return ous.toByteArray(); } } </pre>
本篇文章回覆於2013-09-06 15:52
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
回覆 |
如要回應,請先登入. |