current position:Home>Can you change this des encrypted C version to Java version
Can you change this des encrypted C version to Java version
2022-02-03 01:31:48 【CSDN Q & A】
private const string KEY = "VZCDFKBG"; /// <summary> /// DES encryption /// </summary> /// <param name="pToEncrypt"></param> /// <returns></returns> public static string DESEncrypt(string pToEncrypt) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(KEY); des.IV = ASCIIEncoding.ASCII.GetBytes(KEY); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) ret.AppendFormat("{0:X2}", b); ret.ToString(); return ret.ToString(); } catch (Exception) { return ""; } }
Refer to the answer 1:
DES Tool class :
public class AesUtils { public static final String SECRETKEY = "secretKey"; public static String encryptStr(String srcStr, String password) { byte[] encryptResult = encryptData_AES(srcStr, password); String encryptResultStr = parseByte2HexStr(encryptResult); return encryptResultStr; } public static String decryptStr(String srcStr, String password) { String returnValue = ""; try { byte[] decryptFrom = parseHexStr2Byte(srcStr); byte[] decryptResult = decryptData_AES(decryptFrom, password); returnValue = new String(decryptResult, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return returnValue; } private static String parseByte2HexStr(byte buf[]) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < buf.length; i++) { String hex = Integer.toHexString(buf[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } private static byte[] encryptData_AES(String content, String password) { try { SecretKey secretKey = getKey(password); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); byte[] byteContent = content.getBytes("utf-8"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] result = cipher.doFinal(byteContent); return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } return null; } private static byte[] parseHexStr2Byte(String hexStr) { if (hexStr.length() < 1) return null; byte[] result = new byte[hexStr.length() / 2]; for (int i = 0; i < hexStr.length() / 2; i++) { int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; } private static SecretKey getKey(String secret) throws GeneralSecurityException { try { KeyGenerator _generator = KeyGenerator.getInstance("AES"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(secret.getBytes()); _generator.init(128,secureRandom); return _generator.generateKey(); } catch (Exception e) { throw new RuntimeException(""); } } private static byte[] decryptData_AES(byte[] content, String password) { try { SecretKey secretKey = getKey(password); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] result = cipher.doFinal(content); return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } return null; }}
Refer to the answer 2:
copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030131471285.html
The sidebar is recommended
- Using burpsuite to capture the package of Pikachu shooting range SQL to inject the first level error
- Jar package conflict. I don't know where 2.29.1 came from
- About the rotation matrix on leetcode!
- The raspberry pie prompts cannot find webcams when running the command
- C + + a simple encryption, look at the warning how to modify, output nothing
- PHP composer and Vue node_ modules
- JDBC connects to the database for data deletion. The syntax is correct, but the data in the database cannot be deleted
- Java interface class program, define an interface and define two classes to implement the interface, then use the sorting method in the class, then enter an array with length of 8 in main, and call newsort with the two implementation classes as arguments
- Sort with average score, preferably bubble method
- When running the service jar, report Linux 1] + exit 1
guess what you like
-
How to solve the problem of traceback? (Language Python)
-
If an undirected graph G has 100 edges, how many vertices does the undirected graph have at least
-
There was a problem calling the custom function
-
The sum of a positive integer and 3 is a multiple of 5, and the difference between a positive integer and 3 is a multiple of 6. Write a program to find the minimum number that meets the conditions.
-
Oracle has customized a date function. What's wrong with this writing?
-
Spring boot project failed to introduce echarts
-
How to solve this kind of program
-
How to understand this? I didn't understand it at the beginning
-
This problem requires the implementation of a function to reverse the given one-way linked list, that is, the header is set to the end of the table, and the end of the table is set to the header.
-
*Isn't a numerical value? Because a is the address
Random recommended
- How should this program be completed
- The picture of U disk retrieved by app in Android 8.1 development board
- C language beginners' doubts, no, require recursion
- How to use bat or VBS to open Ethernet status window
- Base selection questions, C + + has just begun to learn, can someone give some ideas
- C language function problem,.?!
- Java class related problems
- What library is checkfasta in Python? Why can't you find it
- Problems encountered when deploying HTTPS certificates using docker nginx
- Why can't vs2022 run this code, but the compiler on the mobile phone can
- Java type conversion problem, is there a useful tool?
- C language function problem, confusion,.?!
- Please do me a favor and solve it
- Python learning has something you don't understand
- How does Baidu Post Bar get posts in it
- Who will prompt the board of STC89C52 if there is no professional course
- Can C language assign a value to the whole array
- How to prevent the page from sliding up and down when the wechat applet slides left and right
- Does anyone know why the information in this form can't come out? Hurry!
- Operation of strlen function
- Why is it an endless loop? And what does I + + in printf mean
- C language function problem,.?!
- In which industries does C + + graphics development develop c++
- Why can't I output with structure
- How to save the processed file after Python data processing? It is found that the file has not changed after saving
- How can this mistake be corrected?
- Python calculates the triple accumulation sum of arrays
- Through the request forwarding command of the servlet program, will the access to the target resources protected by the filter filter be filtered by the filter?
- Pycharm loads TTF font file and displays garbled code
- Hello, is there a corresponding code implementation for the super resolution from a single image you mentioned in the super resolution article two years ago?
- Why are the results the same
- The MySQL geometry type retains 6 decimal places
- The removeall() and addall() methods in the list set are the same as the list What is the difference between the difference set and the sum set of two sets?
- Vue project number is automatically generated according to the city and county
- How to generate Vue number automatically
- Please * God, what about the win service flash back
- The users table space of Oracle is migrated to another file path and must be moved under the database shutdown?
- How can I do this?
- Pointer assignment problem, how to assign
- How does Java cut and drag pictures onto the canvas