java版本自己封装base64

  1. package com.qhong;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4.  
  5. import org.apache.commons.lang.StringUtils;
  6.  
  7. public class Base64Utils {
  8.  
  9. /**
  10. * Base64方法重写
  11. */
  12. public static void main(String[] args) {
  13. String aa = "商户私有域";
  14. String aaa = byteArrayToBase64(aa.getBytes());
  15. System.out.println(aaa);
  16. try {
  17. String bb = new String(base64ToByteArray(aaa));
  18. System.out.println(bb);
  19.  
  20. String tests = encodeBuffer("【厚本金融】", "GBK");
  21. System.out.println(tests);
  22. System.out.println(decodeBuffer(tests, "GBK"));
  23. } catch (Exception e) {
  24. // TODO Auto-generated catch block
  25. e.printStackTrace();
  26. }
  27. }
  28. /**
  29. * 将字符数组base64字符串
  30. * @param a 要转化的字符数组
  31. * @return base64字符串
  32. */
  33. public static String byteArrayToBase64(byte[] a) {
  34. int aLen = a.length; //总长度
  35. int numFullGroups = aLen / 3; //以3个byte组成以4个字符为一组的组数
  36. int numBytesInPartialGroup = aLen - 3 * numFullGroups; //余数
  37. int resultLen = 4 * ((aLen + 2) / 3); //输出长度总是4倍数,如果有余数,(aLen+2)/3保证将余数包含,并有空间放置填充符=
  38. StringBuffer result = new StringBuffer(resultLen);
  39. int inCursor = 0;
  40. for (int i = 0; i < numFullGroups; i++) {
  41. int byte0 = a[inCursor++] & 0xff;
  42. int byte1 = a[inCursor++] & 0xff;
  43. int byte2 = a[inCursor++] & 0xff;
  44. result.append(intToBase64[byte0 >> 2]);
  45. result.append(intToBase64[(byte0 << 4) & 0x3f | (byte1 >> 4)]);
  46. result.append(intToBase64[(byte1 << 2) & 0x3f | (byte2 >> 6)]);
  47. result.append(intToBase64[byte2 & 0x3f]);
  48. }
  49. //处理余数
  50. if (numBytesInPartialGroup != 0) {
  51. int byte0 = a[inCursor++] & 0xff;
  52. result.append(intToBase64[byte0 >> 2]);
  53. //余数为1
  54. if (numBytesInPartialGroup == 1) {
  55. result.append(intToBase64[(byte0 << 4) & 0x3f]);
  56. result.append("==");
  57. } else {
  58. // 余数为2
  59. int byte1 = a[inCursor++] & 0xff;
  60. result.append(intToBase64[(byte0 << 4) & 0x3f | (byte1 >> 4)]);
  61. result.append(intToBase64[(byte1 << 2) & 0x3f]);
  62. result.append('=');
  63. }
  64. }
  65. return result.toString();
  66. }
  67.  
  68. static final char intToBase64[] = { 'A', 'B', 'C', 'D', 'E', 'F', /* 索引 0 ~ 5*/
  69. 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', /* 索引6 ~ 18*/
  70. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', /* 索引 19 ~ 31*/
  71. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', /* 索引 32 ~ 44*/
  72. 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', /* 索引 45 ~ 57*/
  73. '6', '7', '8', '9', '.'/*原先是字符+*/, '_'/*原先是字符/ */ }; /* 索引58 ~ 63*/
  74.  
  75. /**
  76. * base64字符串还原为字符数组
  77. * @param s base64字符串
  78. * @return 还原后的字符串数组
  79. * @throws Exception
  80. */
  81. public static byte[] base64ToByteArray(String s) throws Exception {
  82. //字符总长必须是4的倍数
  83. int sLen = s.length();
  84. int numGroups = sLen / 4;
  85. if (4 * numGroups != sLen)
  86. throw new IllegalArgumentException(
  87. "字串长度必须是4的倍数");
  88. //余1个byte则算漏了两个byte,余2个byte则算漏掉了1个byte
  89. int missingBytesInLastGroup = 0;
  90. int numFullGroups = numGroups;
  91. if (sLen != 0) {
  92. //余2个byte的情况
  93. if (s.charAt(sLen - 1) == '=') {
  94. missingBytesInLastGroup++;
  95. //如果有余数发生,则完整3个byte组数少一个。
  96. numFullGroups--;
  97. }
  98. //余1个byte的情况
  99. if (s.charAt(sLen - 2) == '=')
  100. missingBytesInLastGroup++;
  101. }
  102.  
  103. //总字节长度
  104.  
  105. byte[] result = new byte[3 * numGroups - missingBytesInLastGroup];
  106. try {
  107. int inCursor = 0, outCursor = 0;
  108. for (int i = 0; i < numFullGroups; i++) {
  109. int ch0 = base64toInt(s.charAt(inCursor++), base64ToInt);
  110. int ch1 = base64toInt(s.charAt(inCursor++), base64ToInt);
  111. int ch2 = base64toInt(s.charAt(inCursor++), base64ToInt);
  112. int ch3 = base64toInt(s.charAt(inCursor++), base64ToInt);
  113. result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
  114. result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
  115. result[outCursor++] = (byte) ((ch2 << 6) | ch3);
  116. }
  117. if (missingBytesInLastGroup != 0) {
  118. int ch0 = base64toInt(s.charAt(inCursor++), base64ToInt);
  119. int ch1 = base64toInt(s.charAt(inCursor++), base64ToInt);
  120. //不管余1还是余2个byte,肯定要解码一个byte。
  121. result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
  122. //如果余2个,即差一个才构成3byte,那么还要解码第二个byte。
  123. if (missingBytesInLastGroup == 1) {
  124. int ch2 = base64toInt(s.charAt(inCursor++), base64ToInt);
  125. result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
  126. }
  127. }
  128. } catch (Exception e) {
  129. throw e;
  130. }
  131. return result;
  132. }
  133. private static int base64toInt(char c, byte[] alphaToInt) throws Exception {
  134. int result = alphaToInt[c];
  135. if (result < 0)
  136. throw new Exception("illegal index!");//非法索引值
  137. return result;
  138. }
  139. static final byte base64ToInt[] = { -1, -1, -1, -1, -1, -1, -1, -1,
  140. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  141. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  142. -1, -1/*原先是62*/, -1, -1, 62/*原先是-1*/, -1/*原先是63*/, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1,
  143. -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  144. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1,
  145. 63/*原先是-1*/, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  146. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };
  147.  
  148. /**
  149. * @param str原始中文字符串
  150. * @param charset进行二进制字符串转化的字符编码
  151. * @return 二进制字符串
  152. */
  153. public static String encodeBuffer(String str ,String charset){
  154. if (str == null) {
  155. str = StringUtils.EMPTY;
  156. }
  157. try {
  158. return Base64Utils.byteArrayToBase64(str.getBytes(charset));
  159. } catch (UnsupportedEncodingException e) {
  160. return new String();
  161. }
  162. }
  163. /**
  164. * 此方法为还原中文字符串转化后的二进制字符串
  165. * @param str 二进制字符串
  166. * @param charset 字符编码
  167. * @return 正常中文字符串
  168. */
  169. public static String decodeBuffer(String str ,String charset){
  170. if (str == null) {
  171. return str = StringUtils.EMPTY;
  172. }
  173. try {
  174. byte[] byteStr;
  175. try {
  176. byteStr = Base64Utils.base64ToByteArray(str.trim());
  177. } catch (Exception e) {
  178. return new String();
  179. }
  180. return new String(byteStr,charset);
  181. } catch (UnsupportedEncodingException e) {
  182. return new String();
  183. }
  184. }
  185. }

与上面对应的net版本:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace ConsoleApplication2
  10. {
  11. public class Base64Utils
  12. {
  13.  
  14. ///**
  15. // * Base64方法重写
  16. // */
  17. //public static void main(String[] args)
  18. //{
  19.  
  20. // String aa = "商户私有域";
  21.  
  22. // String aaa = byteArrayToBase64(aa.getBytes());
  23. // System.out.println(aaa);
  24. // try
  25. // {
  26. // String bb = new String(base64ToByteArray(aaa));
  27. // System.out.println(bb);
  28.  
  29. // String tests = encodeBuffer("【厚本金融】", "GBK");
  30. // System.out.println(tests);
  31. // System.out.println(decodeBuffer(tests, "GBK"));
  32. // }
  33. // catch (Exception e)
  34. // {
  35. // // TODO Auto-generated catch block
  36. // e.printStackTrace();
  37. // }
  38. //}
  39.  
  40. /**
  41. * 将字符数组base64字符串
  42. * @param a 要转化的字符数组
  43. * @return base64字符串
  44. */
  45. public static String byteArrayToBase64(byte[] a)
  46. {
  47. int aLen = a.Length; //总长度
  48. int numFullGroups = aLen / 3; //以3个byte组成以4个字符为一组的组数
  49. int numBytesInPartialGroup = aLen - 3 * numFullGroups; //余数
  50. int resultLen = 4 * ((aLen + 2) / 3); //输出长度总是4倍数,如果有余数,(aLen+2)/3保证将余数包含,并有空间放置填充符=
  51.  
  52. //StringBuffer result = new StringBuffer(resultLen);
  53. StringBuilder result = new StringBuilder(resultLen);
  54. int inCursor = 0;
  55. for (int i = 0; i < numFullGroups; i++)
  56. {
  57. int byte0 = a[inCursor++] & 0xff;
  58. int byte1 = a[inCursor++] & 0xff;
  59. int byte2 = a[inCursor++] & 0xff;
  60. result.Append(intToBase64[byte0 >> 2]);
  61. result.Append(intToBase64[(byte0 << 4) & 0x3f | (byte1 >> 4)]);
  62. result.Append(intToBase64[(byte1 << 2) & 0x3f | (byte2 >> 6)]);
  63. result.Append(intToBase64[byte2 & 0x3f]);
  64. }
  65. //处理余数
  66. if (numBytesInPartialGroup != 0)
  67. {
  68. int byte0 = a[inCursor++] & 0xff;
  69. result.Append(intToBase64[byte0 >> 2]);
  70. //余数为1
  71. if (numBytesInPartialGroup == 1)
  72. {
  73. result.Append(intToBase64[(byte0 << 4) & 0x3f]);
  74. result.Append("==");
  75. }
  76. else
  77. {
  78. // 余数为2
  79. int byte1 = a[inCursor++] & 0xff;
  80. result.Append(intToBase64[(byte0 << 4) & 0x3f | (byte1 >> 4)]);
  81. result.Append(intToBase64[(byte1 << 2) & 0x3f]);
  82. result.Append('=');
  83. }
  84. }
  85. return result.ToString();
  86. }
  87.  
  88. static readonly char[] intToBase64 = { 'A', 'B', 'C', 'D', 'E', 'F', /* 索引 0 ~ 5*/
  89. 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', /* 索引6 ~ 18*/
  90. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', /* 索引 19 ~ 31*/
  91. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', /* 索引 32 ~ 44*/
  92. 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', /* 索引 45 ~ 57*/
  93. '6', '7', '8', '9', '.'/*原先是字符+*/, '_'/*原先是字符/ */ }; /* 索引58 ~ 63*/
  94.  
  95. /**
  96. * base64字符串还原为字符数组
  97. * @param s base64字符串
  98. * @return 还原后的字符串数组
  99. * @throws Exception
  100. */
  101. public static byte[] base64ToByteArray(String s)
  102. {
  103. //字符总长必须是4的倍数
  104. int sLen = s.Length;
  105. int numGroups = sLen / 4;
  106. if (4 * numGroups != sLen)
  107. throw new ArgumentException(
  108. "字串长度必须是4的倍数");
  109. //余1个byte则算漏了两个byte,余2个byte则算漏掉了1个byte
  110. int missingBytesInLastGroup = 0;
  111. int numFullGroups = numGroups;
  112. if (sLen != 0)
  113. {
  114. //余2个byte的情况
  115.  
  116. if (s.charAt(sLen - 1) == '=')
  117. {
  118. missingBytesInLastGroup++;
  119. //如果有余数发生,则完整3个byte组数少一个。
  120. numFullGroups--;
  121. }
  122. //余1个byte的情况
  123. if (s.charAt(sLen - 2) == '=')
  124. missingBytesInLastGroup++;
  125. }
  126.  
  127. //总字节长度
  128.  
  129. byte[] result = new byte[3 * numGroups - missingBytesInLastGroup];
  130. try
  131. {
  132. int inCursor = 0, outCursor = 0;
  133. for (int i = 0; i < numFullGroups; i++)
  134. {
  135. int ch0 = base64toInt(s.charAt(inCursor++), base64ToInt);
  136. int ch1 = base64toInt(s.charAt(inCursor++), base64ToInt);
  137. int ch2 = base64toInt(s.charAt(inCursor++), base64ToInt);
  138. int ch3 = base64toInt(s.charAt(inCursor++), base64ToInt);
  139. result[outCursor++] = (byte)((ch0 << 2) | (ch1 >> 4));
  140. result[outCursor++] = (byte)((ch1 << 4) | (ch2 >> 2));
  141. result[outCursor++] = (byte)((ch2 << 6) | ch3);
  142. }
  143. if (missingBytesInLastGroup != 0)
  144. {
  145. int ch0 = base64toInt(s.charAt(inCursor++), base64ToInt);
  146. int ch1 = base64toInt(s.charAt(inCursor++), base64ToInt);
  147. //不管余1还是余2个byte,肯定要解码一个byte。
  148. result[outCursor++] = (byte)((ch0 << 2) | (ch1 >> 4));
  149. //如果余2个,即差一个才构成3byte,那么还要解码第二个byte。
  150. if (missingBytesInLastGroup == 1)
  151. {
  152. int ch2 = base64toInt(s.charAt(inCursor++), base64ToInt);
  153. result[outCursor++] = (byte)((ch1 << 4) | (ch2 >> 2));
  154. }
  155. }
  156. }
  157. catch (Exception e)
  158. {
  159. throw e;
  160. }
  161. return result;
  162. }
  163.  
  164. private static int base64toInt(char c, int[] alphaToInt)
  165. {
  166. int result = alphaToInt[c];
  167. if (result < 0)
  168. throw new Exception("illegal index!");//非法索引值
  169. return result;
  170. }
  171. static readonly int[] base64ToInt = { 1, 1, 1, 1, 1, 1, 1, 1,
  172. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  173. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  174. 1, 1/*原先是62*/, 1, 1, 62/*原先是1*/, 1/*原先是63*/, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 1,
  175. 1, 1, 1, 1, 1, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  176. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 1, 1, 1, 1,
  177. 63/*原先是1*/, 1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  178. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };
  179.  
  180. //static readonly byte[] base64ToInt = { -1, -1, -1, -1, -1, -1, -1, -1,
  181. // -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  182. // -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  183. // -1, -1/*原先是62*/, -1, -1, 62/*原先是-1*/, -1/*原先是63*/, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1,
  184. // -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  185. // 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1,
  186. // 63/*原先是-1*/, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  187. // 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };
  188.  
  189. /**
  190. * @param str原始中文字符串
  191. * @param charset进行二进制字符串转化的字符编码
  192. * @return 二进制字符串
  193. */
  194. public static String encodeBuffer(String str)
  195. {
  196. if (str == null)
  197. {
  198. str = String.Empty;
  199. }
  200. try
  201. {
  202. return Base64Utils.byteArrayToBase64(Encoding.UTF8.GetBytes(str));
  203. }
  204. catch (Exception e)
  205. {
  206. return String.Empty;
  207. }
  208. }
  209.  
  210. /**
  211. * 此方法为还原中文字符串转化后的二进制字符串
  212. * @param str 二进制字符串
  213. * @param charset 字符编码
  214. * @return 正常中文字符串
  215. */
  216. public static String decodeBuffer(String str)
  217. {
  218. if (str == null)
  219. {
  220. return str = String.Empty;
  221. }
  222. try
  223. {
  224. byte[] byteStr;
  225. try
  226. {
  227. byteStr = Base64Utils.base64ToByteArray(str.Trim());
  228. }
  229. catch (Exception e)
  230. {
  231. return String.Empty;
  232. }
  233.  
  234. return Encoding.UTF8.GetString(byteStr);
  235. }
  236. catch (Exception e)
  237. {
  238. return String.Empty;
  239. }
  240. }
  241.  
  242. }
  243.  
  244. }

3des加密,解密:

java版本:

  1. package com.qhong;
  2.  
  3. import java.security.Key;
  4.  
  5. import javax.crypto.Cipher;
  6. import javax.crypto.SecretKeyFactory;
  7. import javax.crypto.spec.DESedeKeySpec;
  8. import javax.crypto.spec.IvParameterSpec;
  9.  
  10. /*import javax.crypto.Cipher;
  11. import javax.crypto.SecretKeyFactory;
  12. import javax.crypto.spec.DESedeKeySpec;
  13. import javax.crypto.spec.IvParameterSpec;*/
  14.  
  15. import sun.misc.BASE64Decoder;
  16. import sun.misc.BASE64Encoder;
  17.  
  18. public class EncodeUtil {
  19. public static void main(String[] args) throws Exception {
  20. /* byte[] key=new BASE64Decoder().decodeBuffer("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4"); */
  21. byte[] key=new BASE64Decoder().decodeBuffer("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4");
  22. byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8 };
  23. byte[] data="中国ABCabc123".getBytes("UTF-8");
  24. System.out.println("ECB加密解密");
  25. byte[] str3 = des3EncodeECB(key,data );
  26. byte[] str4 = ees3DecodeECB(key, str3);
  27. System.out.println(new BASE64Encoder().encode(str3));
  28. System.out.println(new String(str4, "UTF-8"));
  29. System.out.println();
  30. System.out.println("CBC加密解密");
  31. byte[] str5 = des3EncodeCBC(key, keyiv, data);
  32. byte[] str6 = des3DecodeCBC(key, keyiv, str5);
  33. System.out.println(new BASE64Encoder().encode(str5));
  34. System.out.println(new String(str6, "UTF-8"));
  35. }
  36. /**
  37. * ECB加密,不要IV
  38. * @param key 密钥
  39. * @param data 明文
  40. * @return Base64编码的密文
  41. * @throws Exception
  42. */
  43. public static byte[] des3EncodeECB(byte[] key, byte[] data)
  44. throws Exception {
  45. Key deskey = null;
  46. DESedeKeySpec spec = new DESedeKeySpec(key);
  47. SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
  48. deskey = keyfactory.generateSecret(spec);
  49. Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
  50. cipher.init(Cipher.ENCRYPT_MODE, deskey);
  51. byte[] bOut = cipher.doFinal(data);
  52. return bOut;
  53. }
  54. /**
  55. * ECB解密,不要IV
  56. * @param key 密钥
  57. * @param data Base64编码的密文
  58. * @return 明文
  59. * @throws Exception
  60. */
  61. public static byte[] ees3DecodeECB(byte[] key, byte[] data)
  62. throws Exception {
  63. Key deskey = null;
  64. DESedeKeySpec spec = new DESedeKeySpec(key);
  65. SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
  66. deskey = keyfactory.generateSecret(spec);
  67. Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
  68. cipher.init(Cipher.DECRYPT_MODE, deskey);
  69. byte[] bOut = cipher.doFinal(data);
  70. return bOut;
  71. }
  72. /**
  73. * CBC加密
  74. * @param key 密钥
  75. * @param keyiv IV
  76. * @param data 明文
  77. * @return Base64编码的密文
  78. * @throws Exception
  79. */
  80. public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)
  81. throws Exception {
  82. Key deskey = null;
  83. DESedeKeySpec spec = new DESedeKeySpec(key);
  84. SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
  85. deskey = keyfactory.generateSecret(spec);
  86. Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
  87. IvParameterSpec ips = new IvParameterSpec(keyiv);
  88. cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
  89. byte[] bOut = cipher.doFinal(data);
  90. return bOut;
  91. }
  92. /**
  93. * CBC解密
  94. * @param key 密钥
  95. * @param keyiv IV
  96. * @param data Base64编码的密文
  97. * @return 明文
  98. * @throws Exception
  99. */
  100. public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data)
  101. throws Exception {
  102. Key deskey = null;
  103. DESedeKeySpec spec = new DESedeKeySpec(key);
  104. SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
  105. deskey = keyfactory.generateSecret(spec);
  106. Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
  107. IvParameterSpec ips = new IvParameterSpec(keyiv);
  108. cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
  109. byte[] bOut = cipher.doFinal(data);
  110. return bOut;
  111. }
  112. }

NET版本:(只有ECS)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. using System.Security.Cryptography;
  8.  
  9. namespace Rongzi.ToolKits.Core.HBJR
  10. {
  11. public class EncodeUtil
  12. {
  13.  
  14. /**
  15. * ECB加密,不要IV
  16. * @param key 密钥
  17. * @param data 明文
  18. * @return Base64编码的密文
  19. * @throws Exception
  20. */
  21. public static byte[] des3EncodeECB(byte[] key, byte[] data)
  22. {
  23. // Create a MemoryStream.
  24. MemoryStream mStream = new MemoryStream();
  25. TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
  26. tdsp.Mode = CipherMode.ECB;
  27. tdsp.Padding = PaddingMode.PKCS7;
  28.  
  29. // Create a CryptoStream using the MemoryStream
  30. // and the passed key and initialization vector (IV).
  31. CryptoStream cStream = new CryptoStream(mStream,
  32. tdsp.CreateEncryptor(key, null),
  33. CryptoStreamMode.Write);
  34. // Write the byte array to the crypto stream and flush it.
  35. cStream.Write(data, 0, data.Length);
  36. cStream.FlushFinalBlock();
  37. // Get an array of bytes from the
  38. // MemoryStream that holds the
  39. // encrypted data.
  40. byte[] ret = mStream.ToArray();
  41. // Close the streams.
  42. cStream.Close();
  43. mStream.Close();
  44. // Return the encrypted buffer.
  45. return ret;
  46.  
  47. }
  48.  
  49. /**
  50. * ECB解密,不要IV
  51. * @param key 密钥
  52. * @param data Base64编码的密文
  53. * @return 明文
  54. * @throws Exception
  55. */
  56. public static byte[] ees3DecodeECB(byte[] key, byte[] data)
  57. {
  58. try
  59. {
  60. // Create a new MemoryStream using the passed
  61. // array of encrypted data.
  62. MemoryStream msDecrypt = new MemoryStream(data);
  63. TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
  64. tdsp.Mode = CipherMode.ECB;
  65. tdsp.Padding = PaddingMode.PKCS7;
  66. // Create a CryptoStream using the MemoryStream
  67. // and the passed key and initialization vector (IV).
  68. CryptoStream csDecrypt = new CryptoStream(msDecrypt,
  69. tdsp.CreateDecryptor(key, null),
  70. CryptoStreamMode.Read);
  71. // Create buffer to hold the decrypted data.
  72. byte[] fromEncrypt = new byte[data.Length];
  73. // Read the decrypted data out of the crypto stream
  74. // and place it into the temporary buffer.
  75. csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
  76. //Convert the buffer into a string and return it.
  77. return fromEncrypt;
  78. }
  79. catch (CryptographicException e)
  80. {
  81. Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  82. return null;
  83. }
  84. }
  85.  
  86. }
  87. }

http://yilinliu.blogspot.hk/2010/07/c-java-net-descryptoserviceprovider-vs.html

http://www.cnblogs.com/liluping860122/p/4026015.html

http://www.cnblogs.com/AloneSword/archive/2013/12/11/3469219.html

Java加密代码 转换成Net版的更多相关文章

  1. PC网站转换成手机版

    博客地址:https://www.cnblogs.com/zxtceq/p/5714606.html 一天完成把PC网站改为自适应!原来这么简单! http://www.webkaka.com/blo ...

  2. Java-Runoob-高级教程-实例-时间处理:04. Java 实例 - 时间戳转换成时间

    ylbtech-Java-Runoob-高级教程-实例-时间处理:04. Java 实例 - 时间戳转换成时间 1.返回顶部 1. Java 实例 - 时间戳转换成时间  Java 实例 以下实例演示 ...

  3. android112 jni 把java的字符串转换成c的字符串,数组处理

    package com.itheima.charencode; import android.os.Bundle; import android.app.Activity; import androi ...

  4. Java 把 InputStream 转换成 String 的几种方法

    我们在 Java 中经常会碰到如何把 InputStream 转换成 String 的情形,比如从文件或网络得到一个 InputStream,需要转换成字符串输出或赋给别的变量. 未真正关注这个问题之 ...

  5. java将图片转换成二进制

    package com.oumyye.图片; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; imp ...

  6. JAVA CST时间 转换成Date

    Mybatis中处理Oracle时间类型是个比较麻烦的问题,特别是需要用到时间做比较的,可参考以下代码与思路: 格式化CST时间 SimpleDateFormat sdf = new SimpleDa ...

  7. [JavaWeb基础] 025.JAVA把word转换成html

    用第三方插件POI把word文档转换成HTML,下面直接上代码 package com.babybus.sdteam.wordtopdf; import java.io.BufferedWriter; ...

  8. Java Keytools 证书转换成Openssl 的PEM 文件或keytools 导出私钥文件

    上一遍又说到Godaddy 生请证书流程与操作: 现因使用Incapsula 防护使用到https,在添加网站时需要自定义证书,其中需要上传私钥信息,因公钥是能过keytool 生成所以需要导出私钥信 ...

  9. Java实现字符串转换成整数

    1 问题描述 输入一个由数字组成的字符串,请把它转换成整数并输出.例如,输入字符串"123",输出整数123. 请写出一个函数实现该功能,不能使用库函数. 2 解决方案 解答本问题 ...

随机推荐

  1. 【CSS系列】块级元素和行内元素

    块级元素: 块级元素生成一个元素框,默认会填充其父元素的内容区,旁边不能有其他元素,换句话说,它在元素框之前和之后生成了“分隔符”. 列表项是块级额元素的一个特例,除了表现方式与其他块元素一致,列表项 ...

  2. 【jQuery系列之插件】jquery插件之jquery-validation

    equalTo方法: equalTo: function( value, element, param ) { // Bind to the blur event of the target in o ...

  3. 旅游吧!我在这里 ——旅游相册POI搜索:找回你的足迹

    版权声明:本文由林少彬原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/89 来源:腾云阁 https://www.qclou ...

  4. JQuery中$.ajax()方法参数详解 转载

    url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...

  5. struts2漏洞原理

    一.struts2简介: 目前web框架中非常流行的都是mvc设计模式.经典例子例如:python的Django.Flask:java的ssm等.因为使用MVC设计模式,所以在框架内部处理用户数据流参 ...

  6. Windows Phone 几种弹出框提示方式

    首先,我们需要在网络上下载一个Coding4Fun 然后,引用  using Coding4Fun.Phone.Controls.Toolkit;                using Codin ...

  7. android麦克风自录自放demo

    extends:http://blog.csdn.net/trbbadboy/article/details/7865530 是一个直接播放麦克风采集到的声音线程类: class RecordThre ...

  8. tpcc-mysql安装、使用、结果解读

    请点击:http://www.aikaiyuan.com/8687.html 错误处理: ln -s /usr/local/mysql/lib/libmysqlclient.so. /usr/lib6 ...

  9. fiddler 面板内显示IP地址

    1.打开fiddler, 快捷键Ctrl+R  (菜单->Rules->Customize Rules…) 然后在CustomRules.js文件里Ctrl+F查找字符串:static f ...

  10. PHP 的“魔术常量”

    名称 说明 __LINE__ 文件中的当前行号. __FILE__ 文件的完整路径和文件名.如果用在被包含文件中,则返回被包含的文件名.自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路 ...