类似移植C#代码需求,需要加密效果一致。
C#中使用了AesCryptoServiceProvider加密,文档链接 AesCryptoServiceProvider 类
下载里面代码在Visul Studio2019中可以针对原加密字符解密;但是在java/go中效果不一样。
过程中参考的资料网址:
JAVA AES 加密后,结果的长度
MD5.ComputeHash()是怎么实现的?
Md5加密中 C#和Java得到的加密结果不同
由于不熟悉C#,用VisulStudio运行c#demo都还是找朋友搭建的…
过程中主要问题是:
1、MD5出来的结果和C#中ComputeHash()效果不一致
2、不知道AES 具体用的什么加密
结果是瞎摸出来的- - 通过对比MD5后的byte[]数组对比是否一致
参数和预期效果
private static final String pwd = "111111";//需要加密的原字符
private static final String Key = "11111111111111111";//加密秘钥17个1
private static final String Iv = "11111111111111111";//偏移量 17个1
m4bZ90K+7+xldmbrdNP2TQ== //加密结果
byte[]数组比较
// “111111” 的byte[]和md5结果比较
[-106, -25, -110, 24, -106, 94, -73, 44, -110, -91, 73, -35, 90, 51, 1, 18] //byte[]
96e79218965eb72c92a549dd5a330112 //HEX
// KEY的byte[]
[23, 47, 31, 102, 44, 51, 22, -125, -71, 17, 100, 56, -5, 0, -102, 21] //byte[] 17个1
//输出结果的byte[]
//java
[155 134 217 247 66 190 239 236 101 118 102 235 116 211 246 77]
//go不知道为啥不一样..可能符号. 负数和正的正好255差别...
[-101, -122, -39, -9, 66, -66, -17, -20, 101, 118, 102, -21, 116, -45, -10, 77]
//结果:
9b86d9f742beefec657666eb74d3f64d //hex
m4bZ90K+7+xldmbrdNP2TQ== //base64
代码摘自网络~~~~
JAVA 代码
public static void main(String[] args) {
try {
System.out.println(Arrays.toString(getBytes(pwd)));
System.out.println(Arrays.toString(getBytes(Key)));
System.out.println(toHexString1(getBytes(pwd)));
System.out.println(Encrypt(pwd, getBytes(Key), getBytes(Iv)));
} catch (Exception e) {
e.printStackTrace();
}
}
private static byte[] getBytes(String s) throws NoSuchAlgorithmException,
UnsupportedEncodingException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] sbytes = s.getBytes("utf-8");
byte[] bytes = md5.digest(sbytes);
return bytes;
}
public static String Encrypt(String sSrc, byte[] key, byte[] iv)
throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/补码方式"
IvParameterSpec _iv = new IvParameterSpec(iv);// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, _iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
System.out.println(Arrays.toString(encrypted));
System.out.println("resultHex: " + toHexString1(encrypted));
// commons-codec或者自带的base64都行
// return org.apache.commons.codec.binary.Base64.encodeBase64String(encrypted);
return new String(java.util.Base64.getEncoder().encode(encrypted));
}
golang代码
//随意命名... 就不整理了...
func main() {
pwd := "111111"
ekey := "11111111111111111"
eiv := "11111111111111111"
origData := []byte(pwd) // 待加密的数据
key := []byte(ekey) // 加密的密钥
eivbytes := []byte(eiv) // 加密的密钥
no := md5.Sum(origData)
nokey := md5.Sum(key)
noiv := md5.Sum(eivbytes)
uk := make([]byte, 16)
copy(uk,nokey[:16])
ui := make([]byte, 16)
copy(ui,noiv[:16])
noi := make([]byte, 16)
copy(noi,no[:16])
log.Printf("keyv %v",uk)
log.Printf("keyv2 %v",no)
encrypted := AesEncryptCBC2(origData, uk,ui)
log.Printf("resultb %v",encrypted)
log.Println("密文(hex):", hex.EncodeToString(noi))
log.Println("密文(hex):", hex.EncodeToString(encrypted))
log.Println("密文(base64):", base64.StdEncoding.EncodeToString(encrypted))
}
// =================== CBC ======================
func AesEncryptCBC2(origData []byte, key []byte,iv []byte) (encrypted []byte) {
// 分组秘钥
// NewCipher该函数限制了输入k的长度必须为16, 24或者32
block, _ := aes.NewCipher(key)
blockSize := block.BlockSize() // 获取秘钥块的长度
origData = pkcs5Padding(origData, blockSize) // 补全码
blockMode := cipher.NewCBCEncrypter(block, iv) // 加密模式
encrypted = make([]byte, len(origData)) // 创建数组
blockMode.CryptBlocks(encrypted, origData) // 加密
return encrypted
}