phorcys phorcys
关注数: 1 粉丝数: 83 发帖数: 792 关注贴吧数: 20
关于 K ... /* -- 12.17 */ enum E_CODE { RSA , RSA_EP, AES } private static String rsa_key = ""; //a private static String rsa_ep_key = ""; //b private static String aes_key = ""; //c public List<NameValuePair> addnew_crypt_K_param(List<NameValuePair> param) { ArrayList<NameValuePair> localArrayList = new ArrayList<NameValuePair>(); this.generate_new_aes_key(); localArrayList.add(new BasicNameValuePair("K", get_K())); Iterator<NameValuePair> i = param.iterator(); E_CODE type = E_CODE.AES; if(i.next().getName().indexOf("login_id") != -1) { type = E_CODE.RSA_EP; } while(i.hasNext()) { NameValuePair n = i.next(); localArrayList.add(new BasicNameValuePair(n.getName(),Base64.encodeBase64String(new_encrypt_cn(type,n.getValue())))); } return localArrayList; } public void set_rsa_key(String key) { rsa_key = key; } public void set_rsa_ep_key(String key) { rsa_ep_key = key; } public String get_K() { return aes_key; } public void generate_new_aes_key() { try { KeyGenerator localKeyGenerator = KeyGenerator.getInstance("AES"); localKeyGenerator.init(128, SecureRandom.getInstance("SHA1PRNG")); aes_key = Base64.encodeBase64String(localKeyGenerator.generateKey().getEncoded()); } catch (NoSuchAlgorithmException localNoSuchAlgorithmException) { } return; } public byte[] new_encrypt_cn(E_CODE paramE_CODE, String val) { byte[] localObject = null; try { if(paramE_CODE == E_CODE.RSA) { if (rsa_key.length() > 2) { Cipher localCipher; byte[] arrayOfByte = null; PublicKey localPublicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(Base64.decodeBase64(rsa_key))); localCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); localCipher.init(Cipher.ENCRYPT_MODE, localPublicKey); arrayOfByte = localCipher.doFinal(val.getBytes()); return arrayOfByte; } } else if (paramE_CODE == E_CODE.RSA_EP) { if (rsa_ep_key.length() >= 2) { PrivateKey localPrivateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(Base64.decodeBase64(rsa_ep_key))); Cipher localCipher2 = Cipher.getInstance("RSA/ECB/PKCS1Padding"); localCipher2.init(Cipher.ENCRYPT_MODE, localPrivateKey); localObject = localCipher2.doFinal(val.getBytes()); } } else if (paramE_CODE == E_CODE.AES) { SecretKeySpec localSecretKeySpec = new SecretKeySpec(Base64.decodeBase64(aes_key.getBytes()), "AES"); Cipher localCipher1 = Cipher.getInstance("AES/ECB/PKCS5Padding"); localCipher1.init(Cipher.ENCRYPT_MODE, localSecretKeySpec); byte[] arrayOfByte2 = localCipher1.doFinal(val.getBytes()); localObject = arrayOfByte2; } } catch (Exception localException) { return null; } return localObject; } public byte[] new_decrypt_cn(E_CODE paramE_CODE, byte[] paramArrayOfByte) { byte[] localObject = null; try { if(paramE_CODE == E_CODE.RSA) { if (rsa_key.length() > 2) { Cipher localCipher; byte[] arrayOfByte = null; PublicKey localPublicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(Base64.decodeBase64(rsa_key))); localCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); localCipher.init(1, localPublicKey); arrayOfByte = localCipher.doFinal(paramArrayOfByte); return arrayOfByte; } } else if (paramE_CODE == E_CODE.RSA_EP) { if (rsa_ep_key.length() >= 2) { PrivateKey localPrivateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(Base64.decodeBase64(rsa_ep_key))); Cipher localCipher2 = Cipher.getInstance("RSA/ECB/PKCS1Padding"); localCipher2.init(2, localPrivateKey); localObject = localCipher2.doFinal(paramArrayOfByte); } } else if (paramE_CODE == E_CODE.AES) { SecretKeySpec localSecretKeySpec = new SecretKeySpec(Base64.decodeBase64(aes_key.getBytes()), "AES"); byte[] arrayOfByte1 = Base64.decodeBase64(paramArrayOfByte); Cipher localCipher1 = Cipher.getInstance("AES/ECB/PKCS5Padding"); localCipher1.init(2, localSecretKeySpec); byte[] arrayOfByte2 = localCipher1.doFinal(arrayOfByte1); localObject = arrayOfByte2; } } catch (Exception localException) { return null; } return localObject; }
1 下一页