请问有java/php都会的大佬么

搬瓦工最新优惠码

kangsgo大佬:
想咨询一下这个java版转php多少钱

  1. package com.xxtx.common.util;
  2. import org.apache.commons.codec.binary.Base64;
  3. import org.apache.commons.io.IOUtils;
  4. import javax.crypto.Cipher;
  5. import java.io.ByteArrayOutputStream;
  6. import java.security.*;
  7. import java.security.interfaces.RSAPrivateKey;
  8. import java.security.interfaces.RSAPublicKey;
  9. import java.security.spec.InvalidKeySpecException;
  10. import java.security.spec.PKCS8EncodedKeySpec;
  11. import java.security.spec.X509EncodedKeySpec;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. public class RSAUtils {
  15. public static final String CHARSET = "UTF-8";
  16.     public static final String RSA_ALGORITHM = "RSA"; // ALGORITHM [‘?lg?r??(?)m] 算法的意思
  17. public static Map<String, String>createKeys(int keySize) {
  18. // 为RSA算法创建一个KeyPairGenerator对象
  19. KeyPairGenerator kpg;
  20.         try {
  21.             kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM);
  22. } catch (NoSuchAlgorithmException e) {
  23. throw new IllegalArgumentException("No such algorithm–>["+ RSA_ALGORITHM + "]");
  24. }
  25. // 初始化KeyPairGenerator对象,密钥长度
  26. kpg.initialize(keySize);
  27. // 生成密匙对
  28. KeyPair keyPair = kpg.generateKeyPair();
  29. // 得到公钥
  30. Key publicKey = keyPair.getPublic();
  31. String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
  32. // 得到私钥
  33. Key privateKey = keyPair.getPrivate();
  34. String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded());
  35. // map装载公钥和私钥
  36. Map<String, String> keyPairMap = new HashMap<String, String>();
  37. keyPairMap.put("publicKey", publicKeyStr);
  38. keyPairMap.put("privateKey", privateKeyStr);
  39. // 返回map
  40. return keyPairMap;
  41. }
  42. /**
  43.      * 得到公钥
  44. * @param publicKey  密钥字符串(经过base64编码)
  45. * @throws Exception
  46.      */
  47. public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
  48. // 通过X509编码的Key指令获得公钥对象
  49. KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
  50. X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
  51. RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
  52.         return key;
  53. }
  54. /**
  55.      * 得到私钥
  56. * @param privateKey  密钥字符串(经过base64编码)
  57. * @throws Exception
  58.      */
  59. public static RSAPrivateKey getPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
  60. // 通过PKCS#8编码的Key指令获得私钥对象
  61. KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
  62. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
  63. RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
  64.         return key;
  65. }
  66. /**
  67.      * 公钥加密
  68. * @param data
  69. * @param publicKey
  70. * @return
  71. */
  72. public static String publicEncrypt(String data, RSAPublicKey publicKey) {
  73. try {
  74.             Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
  75. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  76.             return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength()));
  77. } catch (Exception e) {
  78. throw new RuntimeException("加密字符串["+ data + "]时遇到异常", e);
  79. }
  80.     }
  81. /**
  82.      * 私钥解密
  83. * @param data
  84. * @param privateKey
  85. * @return
  86. */
  87. public static String privateDecrypt(String data, RSAPrivateKey privateKey) {
  88. try {
  89.             Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
  90. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  91.             return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data), privateKey.getModulus().bitLength()), CHARSET);
  92. } catch (Exception e) {
  93. throw new RuntimeException("解密字符串["+ data + "]时遇到异常", e);
  94. }
  95.     }
  96. /**
  97.      * 私钥加密
  98. * @param data
  99. * @param privateKey
  100. * @return
  101. */
  102. public static String privateEncrypt(String data, RSAPrivateKey privateKey) {
  103. try {
  104.             Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
  105. //每个Cipher初始化方法使用一个模式参数opmod,并用此模式初始化Cipher对象。此外还有其他参数,包括密钥key、包含密钥的证书certificate、算法参数params和随机源random。
  106. cipher.init(Cipher.ENCRYPT_MODE, privateKey);
  107.             return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), privateKey.getModulus().bitLength()));
  108. } catch (Exception e) {
  109. throw new RuntimeException("加密字符串["+ data + "]时遇到异常", e);
  110. }
  111.     }
  112. /**
  113.      * 公钥解密
  114. * @param data
  115. * @param publicKey
  116. * @return
  117. */
  118. public static String publicDecrypt(String data, RSAPublicKey publicKey) {
  119. try {
  120.             Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
  121. cipher.init(Cipher.DECRYPT_MODE, publicKey);
  122.             return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data), publicKey.getModulus().bitLength()), CHARSET);
  123. } catch (Exception e) {
  124. throw new RuntimeException("解密字符串["+ data + "]时遇到异常", e);
  125. }
  126.     }
  127. //rsa切割解码  , ENCRYPT_MODE,加密数据   ,DECRYPT_MODE,解密数据
  128. private static byte[] rsaSplitCodec(Cipher cipher, int opmode, byte[] datas, int keySize) {
  129. int maxBlock = 0;  //最大块
  130. if (opmode == Cipher.DECRYPT_MODE) {
  131.             maxBlock = keySize / 8;
  132. } else {
  133.             maxBlock = keySize / 8 – 11;
  134. }
  135.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  136.         int offSet = 0;
  137.         byte[] buff;
  138.         int i = 0;
  139.         try {
  140. while (datas.length > offSet) {
  141. if (datas.length – offSet > maxBlock) {
  142. //可以调用以下的doFinal()方法完成加密或解密数据:
  143. buff = cipher.doFinal(datas, offSet, maxBlock);
  144. } else {
  145.                     buff = cipher.doFinal(datas, offSet, datas.length – offSet);
  146. }
  147.                 out.write(buff, 0, buff.length);
  148. i++;
  149. offSet = i * maxBlock;
  150. }
  151.         } catch (Exception e) {
  152. throw new RuntimeException("加解密阀值为["+ maxBlock + "]的数据时发生异常", e);
  153. }
  154. byte[] resultDatas = out.toByteArray();
  155. IOUtils.closeQuietly(out);
  156.         return resultDatas;
  157. }
  158. }

xuh大佬:
直接百度 php rsa加密/解密

kangsgo大佬:


直接百度 php rsa加密/解密

java的加密不一样的,和php之前我试过了,他这里面还加了很多幺蛾子

推荐:
MJJ快来帮忙,求个正则表达式

lers大佬: 求个正则表达式 字符串如下: {"cookies":[{"name":"ULOGIN_IMG","path&quo…

去谷歌验证,建IPV6隧道不觉得麻烦吗?本人有一计可破

ymcoming大佬: 一个威儿+一个VPS可破,前提是MJJ有2个VPS。。。 威儿服务端原本outbounds这里的配置是:       &nbs…

转需-火绒规则

wang3y2大佬: {    "ver":"5.0",    "tag":"hipsus…