1.加密和解密php
2.對稱加密和非對稱加密java
3.關於單向加密android
4.加密和解密代碼展現git
5.RSA非對稱加解密github
身份認證算法
陌生人通訊segmentfault
支付寶支付加密數組
MD5安全
加密過程服務器
private final String desEncryptString = "yangchong"; private final String desEncryptKey = "19930311"; s1 = DES.encryptDES(desEncryptString, desEncryptKey); Log.e("加密和解密", s1); 加密和解密: 84r1gS+D3Op8yrSnF5ZDrQ== //s1爲加密後的密文
解密過程
String s2 = DES.decryptDES(s1, desEncryptKey); Log.e("加密和解密", s2); //加密和解密: yangchong
public class DES { //初始化向量,隨意填寫 private static byte[] iv = {1,2,3,4,5,6,7,8}; /** * * @param encryptString 明文 * @param encryptKey 密鑰 * @return 加密後的密文 */ public static String encryptDES(String encryptString,String encryptKey){ try { //實例化IvParameterSpec對象,使用指定的初始化向量 IvParameterSpec zeroIv=new IvParameterSpec(iv); //實例化SecretKeySpec,根據傳入的密鑰得到字節數組來構造SecretKeySpec SecretKeySpec key =new SecretKeySpec(encryptKey.getBytes(),"DES"); //建立密碼器 Cipher cipher=Cipher.getInstance("DES/CBC/PKCS5Padding"); //用密鑰初始化Cipher對象 cipher.init(Cipher.ENCRYPT_MODE,key,zeroIv); //執行加密操做 byte[]encryptedData=cipher.doFinal(encryptString.getBytes()); return Base64.encodeToString(encryptedData,0); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } return null; } /** * 解密的過程與加密的過程大體相同 * @param decryptString 密文 * @param decryptKey 密鑰 * @return 返回明文 */ public static String decryptDES(String decryptString,String decryptKey){ try { //先使用Base64解密 byte[]byteMi = Base64.decode(decryptString,0); //實例化IvParameterSpec對象使用指定的初始化向量 IvParameterSpec zeroIv=new IvParameterSpec(iv); //實例化SecretKeySpec,根據傳入的密鑰得到字節數組來構造SecretKeySpec, SecretKeySpec key=new SecretKeySpec(decryptKey.getBytes(),"DES"); //建立密碼器 Cipher cipher=Cipher.getInstance("DES/CBC/PKCS5Padding"); //用密鑰初始化Cipher對象,上面是加密,這是解密模式 cipher.init(Cipher.DECRYPT_MODE,key,zeroIv); //獲取解密後的數據 byte [] decryptedData=cipher.doFinal(byteMi); return new String(decryptedData); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } return null; } }
public class AES { private static final String Algorithm = "AES"; private final static String HEX = "0123456789ABCDEF"; //加密函數,key爲密鑰 public static String encrypt(String key, String src) throws Exception { byte[] rawKey = getRawKey(key.getBytes()); byte[] result = encrypt(rawKey, src.getBytes()); return toHex(result); } //解密函數。key值必須和加密時的key一致 public static String decrypt(String key, String encrypted) throws Exception { byte[] rawKey = getRawKey(key.getBytes()); byte[] enc = toByte(encrypted); byte[] result = decrypt(rawKey, enc); return new String(result); } private static void appendHex(StringBuffer sb, byte b) { sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f)); } private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance(Algorithm); // SHA1PRNG 強隨機種子算法, 要區別Android 4.2.2以上版本的調用方法 SecureRandom sr = null; if (android.os.Build.VERSION.SDK_INT >= 17) { sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); } else { sr = SecureRandom.getInstance("SHA1PRNG"); } sr.setSeed(seed); kgen.init(256, sr); // 256位或128位或192位 SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; } private static byte[] encrypt(byte[] key, byte[] src) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(key, Algorithm); Cipher cipher = Cipher.getInstance(Algorithm); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(src); return encrypted; } private static byte[] decrypt(byte[] key, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(key, Algorithm); Cipher cipher = Cipher.getInstance(Algorithm); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; } private static byte[] toByte(String hexString) { int len = hexString.length() / 2; byte[] result = new byte[len]; for (int i = 0; i < len; i++) { result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue(); } return result; } private static String toHex(byte[] buf) { if (buf == null) { return ""; } StringBuffer result = new StringBuffer(2 * buf.length); for (int i = 0; i < buf.length; i++) { appendHex(result, buf[i]); } return result.toString(); } }
第一步:獲取隨機的公鑰和私鑰
//祕鑰默認長度 public static final int DEFAULT_KEY_SIZE = 2048; KeyPair keyPair = RSA.generateRSAKeyPair(DEFAULT_KEY_SIZE); if (keyPair != null) { // 公鑰 publicKey = (RSAPublicKey) keyPair.getPublic(); // 私鑰 privateKey = (RSAPrivateKey) keyPair.getPrivate(); }
第二步:公鑰加密
//用公鑰對字符串進行加密 try { bytes = RSA.encryptByPublicKey(DEFAULT_SPLIT, publicKey.getEncoded()); String s = new String(bytes); Log.e("加密和解密", s); } catch (Exception e) { e.printStackTrace(); }
第三步:私鑰解密
//使用私鑰進行解密 try { byte[] bytes = RSA.decryptByPrivateKey(this.bytes, privateKey.getEncoded()); String s = new String(bytes); Log.e("加密和解密", s); //解密後獲得的數據:yangchong } catch (Exception e) { e.printStackTrace(); }
第一步:獲取隨機的公鑰和私鑰
//祕鑰默認長度 public static final int DEFAULT_KEY_SIZE = 2048; KeyPair keyPair = RSA.generateRSAKeyPair(DEFAULT_KEY_SIZE); if (keyPair != null) { // 公鑰 publicKey = (RSAPublicKey) keyPair.getPublic(); // 私鑰 privateKey = (RSAPrivateKey) keyPair.getPrivate(); }
第二步:私鑰加密
//使用私鑰加密 try { bytes1 = RSA.encryptByPrivateKey(DEFAULT_SPLIT, privateKey.getEncoded()); String s = new String(bytes); Log.e("加密和解密", s); } catch (Exception e) { e.printStackTrace(); }
第三步:公鑰解密
//使用公鑰解密 try { byte[] bytes = RSA.decryptByPublicKey(this.bytes1, publicKey.getEncoded()); String s = new String(bytes); Log.e("加密和解密", s); //解密後獲得的數據:yangchong } catch (Exception e) { e.printStackTrace(); }
代碼以下所示:
public class RSA { public static final String RSA = "RSA";// 非對稱加密密鑰算法 public static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";//加密填充方式 /**
* * @param keyLength 密鑰長度,範圍:512~2048 * 通常1024 * @return */ public static KeyPair generateRSAKeyPair(int keyLength) { try { KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA); kpg.initialize(keyLength); return kpg.genKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } } /*-------------------------------------------------------------------------------------------------*/ /** * 用公鑰對字符串進行加密 * @param data 原文 * @param publicKey 密鑰 * @return byte[] 解密數據 */ public static byte[] encryptByPublicKey(byte[] data, byte[] publicKey) throws Exception { // 獲得公鑰 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey); KeyFactory kf = KeyFactory.getInstance(RSA); PublicKey keyPublic = kf.generatePublic(keySpec); // 加密數據 Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING); cp.init(Cipher.ENCRYPT_MODE, keyPublic); return cp.doFinal(data); } /** * 私鑰加密 * * @param data 待加密數據 * @param privateKey 密鑰 * @return byte[] 解密數據 */ public static byte[] encryptByPrivateKey(byte[] data, byte[] privateKey) throws Exception { // 獲得私鑰 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey); KeyFactory kf = KeyFactory.getInstance(RSA); PrivateKey keyPrivate = kf.generatePrivate(keySpec); // 數據加密 Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING); cipher.init(Cipher.ENCRYPT_MODE, keyPrivate); return cipher.doFinal(data); } /** * 公鑰解密 * * @param data 待解密數據 * @param publicKey 密鑰 * @return byte[] 解密數據 */ public static byte[] decryptByPublicKey(byte[] data, byte[] publicKey) throws Exception { // 獲得公鑰 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey); KeyFactory kf = KeyFactory.getInstance(RSA); PublicKey keyPublic = kf.generatePublic(keySpec); // 數據解密 Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING); cipher.init(Cipher.DECRYPT_MODE, keyPublic); return cipher.doFinal(data); } /** * 使用私鑰進行解密 * @param encrypted 待解密數據 * @param privateKey 密鑰 * @return byte[] 解密數據 * @throws Exception 異常 */ public static byte[] decryptByPrivateKey(byte[] encrypted, byte[] privateKey) throws Exception { // 獲得私鑰 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey); KeyFactory kf = KeyFactory.getInstance(RSA); PrivateKey keyPrivate = kf.generatePrivate(keySpec); // 解密數據 Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING); cp.init(Cipher.DECRYPT_MODE, keyPrivate); byte[] arr = cp.doFinal(encrypted); return arr; } } ```
第一步:獲取隨機的公鑰和私鑰
//祕鑰默認長度 public static final int DEFAULT_KEY_SIZE = 2048; KeyPair keyPair = RSA.generateRSAKeyPair(DEFAULT_KEY_SIZE); if (keyPair != null) { // 公鑰 publicKey = (RSAPublicKey) keyPair.getPublic(); // 私鑰 privateKey = (RSAPrivateKey) keyPair.getPrivate(); }
第二步:用公鑰對對象進行加密
//用公鑰對對象進行加密 YC yc = new YC(); yc.setAge(25); yc.setName("楊充"); StringBuilder stringBuilder = new StringBuilder(); for(int a=0 ; a<500 ; a++){ stringBuilder.append("都比小楊"+a); } yc.setInfo(stringBuilder.toString()); String string = yc.toString(); long start = System.currentTimeMillis(); encryptBytes = new byte[0]; try { encryptBytes = RSA.encryptByPublicKeyForSpilt(string.getBytes(),publicKey.getEncoded()); } catch (Exception e) { e.printStackTrace(); } long end=System.currentTimeMillis(); Log.e("YC","公鑰加密耗時 cost time---->"+(end-start)); String encryStr = new String(encryptBytes); Log.e("YC","加密前數據長度 --1-->"+string.length()); Log.e("YC","加密後數據長度 --1-->"+encryStr.length());
第三步:使用私鑰進行解密
//使用私鑰進行解密 long start2 = System.currentTimeMillis(); byte[] decryptBytes= new byte[0]; try { decryptBytes = RSA.decryptByPrivateKeyForSpilt(encryptBytes,privateKey.getEncoded()); } catch (Exception e) { e.printStackTrace(); } String decryStr = new String(decryptBytes); long end2 =System.currentTimeMillis(); Log.e("YC","私鑰解密耗時 cost time---->"+(end2-start2)); Log.e("YC","解密後數據 --1-->"+decryStr);
第四步:加密和解密效率比較
代碼以下所示
//祕鑰默認長度 private static final int DEFAULT_KEY_SIZE = 2048; // 當前祕鑰支持加密的最大字節數 private static final int DEFAULT_BUFFER_SIZE = (DEFAULT_KEY_SIZE / 8) - 11; // 當要加密的內容超過bufferSize,則採用partSplit進行分塊加密 private static final byte[] DEFAULT_SPLIT = "#PART#".getBytes(); /** * 用公鑰對字符串進行分段加密 * @param data 須要加密數據 * @param publicKey 公鑰 * @return byte[] 加密數據 * @throws Exception 異常
*/ public static byte[] encryptByPublicKeyForSpilt(byte[] data, byte[] publicKey) throws Exception { int dataLen = data.length; if (dataLen <= DEFAULT_BUFFER_SIZE) { return encryptByPublicKey(data, publicKey); } List<Byte> allBytes = new ArrayList<>(2048); int bufIndex = 0; int subDataLoop = 0; byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; for (int i = 0; i < dataLen; i++) { if (buf != null) { buf[bufIndex] = data[i]; } if (++bufIndex == DEFAULT_BUFFER_SIZE || i == dataLen - 1) { subDataLoop++; if (subDataLoop != 1) { for (byte b : DEFAULT_SPLIT) { allBytes.add(b); } } byte[] encryptBytes = encryptByPublicKey(buf, publicKey); for (byte b : encryptBytes) { allBytes.add(b); } bufIndex = 0; if (i == dataLen - 1) { buf = null; } else { buf = new byte[Math.min(DEFAULT_BUFFER_SIZE, dataLen - i - 1)]; } } } byte[] bytes = new byte[allBytes.size()]; int i = 0; for (Byte b : allBytes) { bytes[i++] = b; } return bytes; } /** * 用祕鑰對字符串進行分段加密 * * @param data 要加密的原始數據 * @param privateKey 祕鑰 * @return byte[] 加密數據 * @throws Exception 異常 * https://github.com/yangchong211 */ public static byte[] encryptByPrivateKeyForSpilt(byte[] data, byte[] privateKey) throws Exception { int dataLen = data.length; if (dataLen <= DEFAULT_BUFFER_SIZE) { return encryptByPrivateKey(data, privateKey); } List<Byte> allBytes = new ArrayList<Byte>(2048); int bufIndex = 0; int subDataLoop = 0; byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; for (int i = 0; i < dataLen; i++) { if (buf != null) { buf[bufIndex] = data[i]; } if (++bufIndex == DEFAULT_BUFFER_SIZE || i == dataLen - 1) { subDataLoop++; if (subDataLoop != 1) { for (byte b : DEFAULT_SPLIT) { allBytes.add(b); } } byte[] encryptBytes = encryptByPrivateKey(buf, privateKey); for (byte b : encryptBytes) { allBytes.add(b); } bufIndex = 0; if (i == dataLen - 1) { buf = null; } else { buf = new byte[Math.min(DEFAULT_BUFFER_SIZE, dataLen - i - 1)]; } } } byte[] bytes = new byte[allBytes.size()]; int i = 0; for (Byte b : allBytes) { bytes[i++] = b; } return bytes; } /** * 用公鑰分段解密 * * @param encrypted 待解密數據 * @param publicKey 公鑰 * @return byte[] 解密數據 * @throws Exception 異常 * https://github.com/yangchong211 */ public static byte[] decryptByPublicKeyForSpilt(byte[] encrypted, byte[] publicKey) throws Exception { int splitLen = DEFAULT_SPLIT.length; if (splitLen <= 0) { return decryptByPublicKey(encrypted, publicKey); } int dataLen = encrypted.length; List<Byte> allBytes = new ArrayList<Byte>(1024); int latestStartIndex = 0; for (int i = 0; i < dataLen; i++) { byte bt = encrypted[i]; boolean isMatchSplit = false; if (i == dataLen - 1) { // 到data的最後了 byte[] part = new byte[dataLen - latestStartIndex]; System.arraycopy(encrypted, latestStartIndex, part, 0, part.length); byte[] decryptPart = decryptByPublicKey(part, publicKey); for (byte b : decryptPart) { allBytes.add(b); } latestStartIndex = i + splitLen; i = latestStartIndex - 1; } else if (bt == DEFAULT_SPLIT[0]) { // 這個是以split[0]開頭 if (splitLen > 1) { if (i + splitLen < dataLen) { // 沒有超出data的範圍 for (int j = 1; j < splitLen; j++) { if (DEFAULT_SPLIT[j] != encrypted[i + j]) { break; } if (j == splitLen - 1) { // 驗證到split的最後一位,都沒有break,則代表已經確認是split段 isMatchSplit = true; } } } } else { // split只有一位,則已經匹配了 isMatchSplit = true; } } if (isMatchSplit) { byte[] part = new byte[i - latestStartIndex]; System.arraycopy(encrypted, latestStartIndex, part, 0, part.length); byte[] decryptPart = decryptByPublicKey(part, publicKey); for (byte b : decryptPart) { allBytes.add(b); } latestStartIndex = i + splitLen; i = latestStartIndex - 1; } } byte[] bytes = new byte[allBytes.size()]; int i = 0; for (Byte b : allBytes) { bytes[i++] = b; } return bytes; } /** * 使用私鑰分段解密 * * @param encrypted 待解密數據 * @param privateKey 私鑰 * @return byte[] 解密數據 * @throws Exception 異常 * https://github.com/yangchong211 */ public static byte[] decryptByPrivateKeyForSpilt(byte[] encrypted, byte[] privateKey) throws Exception { int splitLen = DEFAULT_SPLIT.length; if (splitLen <= 0) { return decryptByPrivateKey(encrypted, privateKey); } int dataLen = encrypted.length; List<Byte> allBytes = new ArrayList<Byte>(1024); int latestStartIndex = 0; for (int i = 0; i < dataLen; i++) { byte bt = encrypted[i]; boolean isMatchSplit = false; if (i == dataLen - 1) { // 到data的最後了 byte[] part = new byte[dataLen - latestStartIndex]; System.arraycopy(encrypted, latestStartIndex, part, 0, part.length); byte[] decryptPart = decryptByPrivateKey(part, privateKey); for (byte b : decryptPart) { allBytes.add(b); } latestStartIndex = i + splitLen; i = latestStartIndex - 1; } else if (bt == DEFAULT_SPLIT[0]) { // 這個是以split[0]開頭 if (splitLen > 1) { if (i + splitLen < dataLen) { // 沒有超出data的範圍 for (int j = 1; j < splitLen; j++) { if (DEFAULT_SPLIT[j] != encrypted[i + j]) { break; } if (j == splitLen - 1) { // 驗證到split的最後一位,都沒有break,則代表已經確認是split段 isMatchSplit = true; } } } } else { // split只有一位,則已經匹配了 isMatchSplit = true; } } if (isMatchSplit) { byte[] part = new byte[i - latestStartIndex]; System.arraycopy(encrypted, latestStartIndex, part, 0, part.length); byte[] decryptPart = decryptByPrivateKey(part, privateKey); for (byte b : decryptPart) { allBytes.add(b); } latestStartIndex = i + splitLen; i = latestStartIndex - 1; } } byte[] bytes = new byte[allBytes.size()]; int i = 0; for (Byte b : allBytes) { bytes[i++] = b; } return bytes; } ```