During the development and application process, the client and the server often need to transmit data. When important private information is involved, developers will naturally think of encrypting it. Even if it is intercepted by "mindful people" during the transmission process, the information will not be leaked. I believe many developers have heard of encryption algorithms, such as MD5 encryption, Base64 encryption, DES encryption, AES encryption, RSA encryption, etc. . Simple encryption can be performed using or, and, etc.
The ^ operation key=0x01 used in the sample code can customize your own rules. Define your own operations to ensure that reversible data is not lost. Key can also be defined, dynamic key.
Java code
public static String myEncode(String str) throws UnsupportedEncodingException { byte[] strBytes = str.getBytes("utf-8"); byte[] newStrByte = new byte[strBytes.length]; for (int i = 0; i < strBytes.length; i++) { newStrByte[i] = (byte) (strBytes[i] ^ 0x01); } return new String(newStrByte); } String encodeStr = myEncode("IdmmnA/"547''+) ')%/"A ^*((!Vnsme"); System.out.println(encodeStr);javascript code
Get utf-8's byte
function toUTF8Array(str) { var utf8 = []; for (var i=0; i < str.length; i++) { var charcode = str.charCodeAt(i); if (charcode < 0x80) utf8.push(charcode); else if (charcode < 0x800) { utf8.push(0xc0 | (charcode >> 6), 0x80 | (charcode & 0x3f)); } else if (charcode < 0xd800 || charcode >= 0xe000) { utf8.push(0xe0 | (charcode >> 6), 0x80 | (charcode & 0x3f)); } else if (charcode < 0xd800 || charcode >= 0xe000) { utf8.push(0xe0 | (charcode >> 12), 0x80 | ((charcode>>6) & 0x3f), 0x80 | (charcode & 0x3f)); } // surrogate pair else { i++; // UTF-16 encodes 0x10000-0x10FFFF by // subtracting 0x10000 and splitting the // 20 bits of 0x0-0xFFFFFF into two halves charcode = 0x10000 + (((charcode & 0x3ff)<<10) | (str.charCodeAt(i) & 0x3ff)); utf8.push(0xf0 | (charcode >>18), 0x80 | ((charcode>>12) & 0x3f), 0x80 | ((charcode>>6) & 0x3f), 0x80 | (charcode & 0x3f)); } } return utf8; }Get byte and perform ^ calculation
bytes=stringToAsciiByteArray(str); for (var i = 0; i < bytes.length; i++) { var newByte = (bytes[i]^0x01); // newByte = (newByte^0x01); console.log(String.fromCharCode(newByte)); encodeStr += String.fromCharCode(newByte); }; console.log(encodeStr);Summarize
The above is the entire content of this article about Java&javascript custom encrypted data transmission code examples, I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Java Exploration: Encrypted and decrypted code examples of Thread+IO files
Multimode string matching algorithm principle and Java implementation code
The complete code example of the Java algorithm to implement red and black tree
If there are any shortcomings, please leave a message to point it out.