python与java 对应的加密算法
1.gzip 加密
java的gzip加密:
| import java.io.ByteArrayInputStream; |
| import java.io.ByteArrayOutputStream; |
| import java.util.Arrays; |
| import java.util.zip.GZIPInputStream; |
| import java.util.zip.GZIPOutputStream; |
| |
| |
| public class Hello { |
| public static void main(String[] args) { |
| try { |
| String data = "哈喽啊"; |
| |
| ByteArrayOutputStream v0_1 = new ByteArrayOutputStream(); |
| GZIPOutputStream v1 = new GZIPOutputStream(v0_1); |
| v1.write(data.getBytes()); |
| v1.close(); |
| byte[] arg6 = v0_1.toByteArray(); |
| System.out.println(Arrays.toString(arg6)); |
| |
| |
| ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| ByteArrayInputStream in = new ByteArrayInputStream(arg6); |
| GZIPInputStream ungzip = new GZIPInputStream(in); |
| byte[] buffer = new byte[256]; |
| int n; |
| while ((n = ungzip.read(buffer)) >= 0) { |
| out.write(buffer,0,n); |
| } |
| byte[] res = out.toByteArray(); |
| System.out.println(Arrays.toString(res)); |
| System.out.println(out.toString("utf-8")); |
| }catch (Exception e){ |
| System.out.println(e); |
| } |
| } |
| } |
python的gzip加密:
| import gzip |
| |
| |
| data_in = "哈喽啊".encode('utf-8') |
| data_out = gzip.compress(data_in) |
| print(data_out) |
| print(data_out.hex()) |
| |
| |
| res = gzip.decompress(data_out) |
| print(res) |
| print(res.decode('utf-8')) |
AES --加密
java的AES加密:
| import javax.crypto.Cipher; |
| import javax.crypto.spec.IvParameterSpec; |
| import javax.crypto.spec.SecretKeySpec; |
| import java.security.MessageDigest; |
| import java.util.Arrays; |
| |
| |
| public class Hello { |
| public static void main(String[] args) { |
| String name = "哈喽啊"; |
| |
| |
| String key = "fehg123hjj216jjwqhe16i32kj1nkn22"; |
| String iv = "77b07a672d57d643"; |
| |
| |
| byte[] raw = key.getBytes(); |
| SecretKeySpec secretKey = new SecretKeySpec(raw,"AES"); |
| IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes()); |
| try { |
| Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| cipher.init(Cipher.DECRYPT_MODE,secretKey,ivSpec); |
| byte[] encrypted = cipher.doFinal(name.getBytes()); |
| |
| System.out.println(Arrays.toString(encrypted)); |
| }catch (Exception e){ |
| System.out.println(e); |
| } |
| } |
| } |
python的AEC加密:
| from Crypto.Cipher import AES |
| from Crypto.Util.Padding import pad |
| |
| KEY= "fehg123hjj216jjwqhe16i32kj1nknnw" |
| IV ="77b07a672d57d64c" |
| |
| def aes_encrypt(data_string): |
| aes = AES.new( |
| key = KEY.encode('utf-8'), |
| mode = AES.MODE_CBC, |
| iv = IV.encode('utf-8'), |
| ) |
| raw = pad(data_string.encode("utf-8"),16) |
| return aes.encrypt(raw) |
| |
| data = aes_encrypt("哈喽啊") |
| |
| |
| c = data.hex() |
| print(c) |
| print(data) |
| print([i for i in data]) |
加密算法 -- SHA-256
java的 SHA-256加密,几乎与 MD5加密的写法一样:
| import java.security.MessageDigest; |
| import java.util.Arrays; |
| |
| |
| public class Hello { |
| public static void main(String[] args) { |
| String name = "哈喽啊"; |
| try { |
| MessageDigest instance = MessageDigest.getInstance("SHA-256"); |
| |
| instance.update("xxx".getBytes()); |
| |
| byte[] nameBytes = instance.digest(name.getBytes()); |
| System.out.println(Arrays.toString(nameBytes)); |
| |
| String res = new String(nameBytes); |
| System.out.println(res); |
| |
| |
| StringBuilder sb = new StringBuilder(); |
| for (int i=0;i<nameBytes.length;i++){ |
| int val = nameBytes[i] & 255; |
| if (val < 16) { |
| sb.append("0"); |
| } |
| |
| sb.append(Integer.toHexString(val)); |
| } |
| |
| String hexData = sb.toString(); |
| System.out.println(hexData); |
| |
| }catch (Exception e){ |
| System.out.println(e); |
| } |
| } |
| } |
md5加密- java与python的区别
python的md5加密
| import hashlib |
| |
| name = '哈喽啊' |
| obj = hashlib.md5() |
| |
| obj.update('xxx'.encode('utf-8')) |
| |
| obj.update(name.encode('utf-8')) |
| |
| |
| data = obj.digest() |
| print(data) |
| |
| |
| res = obj.hexdigest() |
| print(res) |
java的md5加密:
| import java.nio.charset.StandardCharsets; |
| import java.security.MessageDigest; |
| import java.security.NoSuchAlgorithmException; |
| import java.util.Arrays; |
| import java.util.Base64; |
| |
| public class Hello { |
| public static void main(String[] args) throws NegativeArraySizeException { |
| String name = "哈喽啊"; |
| try { |
| MessageDigest instance = MessageDigest.getInstance("MD5"); |
| |
| instance.update("xxx".getBytes()); |
| |
| byte[] nameBytes = instance.digest(name.getBytes()); |
| System.out.println(Arrays.toString(nameBytes)); |
| |
| String res = new String(nameBytes); |
| System.out.println(res); |
| |
| |
| StringBuilder sb = new StringBuilder(); |
| for (int i=0;i<nameBytes.length;i++){ |
| int val = nameBytes[i] & 255; |
| if (val < 16) { |
| sb.append("0"); |
| } |
| |
| sb.append(Integer.toHexString(val)); |
| } |
| |
| String hexData = sb.toString(); |
| System.out.println(hexData); |
| |
| }catch (Exception e){ |
| System.out.println(e); |
| } |
| } |
| } |
Base64加密算法
java的Base64加密与解密
| import java.util.Base64; |
| |
| public class Hello { |
| public static void main(String[] args){ |
| String name = "哈喽啊"; |
| |
| Base64.Encoder encoder = Base64.getEncoder(); |
| String res = encoder.encodeToString(name.getBytes()); |
| System.out.println(res); |
| |
| |
| Base64.Decoder decoder = Base64.getDecoder(); |
| byte[] origin = decoder.decode(res); |
| String data = new String(origin); |
| System.out.println(data); |
| } |
| } |
python的Base64加密与解密:
| import base64 |
| |
| name = "哈喽啊" |
| |
| res = base64.b64encode(name.encode('utf-8')) |
| print(res) |
| |
| |
| data = base64.b64decode(res.decode('utf-8')) |
| |
| origin = data.decode('utf-8') |
| print(origin) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通