dongzuoyue6556 2017-04-14 15:57
浏览 323

Java中的AES(128 / ECB / PKCS5)+ Base64(RFC 1521)加密和Golang中的解密

the Encryption in Java is by importing javax.crypto.Cipher, sun.misc.BASE64Decoder, sun.misc.BASE64Encoder and javax/crypto/SecretKey.

I did a lot research on those algorithms to understand the specific standards to make sure every step is the same in Java and Golang.

Since I cannot decrypt the encrypted data from java, I am trying to see the difference from encrypting in golang and that in Java to see the difference. The encrypted outputs from golang and Java are different even w/ the same format(24 digits with "=="at the end).

inputs: key: T3-Arghbegrgg455dfer4g==  
        text: 123456789012345  
output: golang: 07vl02WwI8dApjYI161TrQ==  
        java:   B9E8aoCd+fYQBkKbqCS3lA==  

Please help to find out the reason behind the difference! Thanks!

The java code using

Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);

byte[] stringBytes = message.getBytes("UTF8");
byte[] raw = cipher.doFinal(stringBytes);

BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(raw);
return base64;

In Golang

block, err := aes.NewCipher(key)
if err != nil {
    return "",err
}
content := []byte(src)
ecb := NewECBEncrypter(block)
content = PKCS5Padding(content, block.BlockSize())
crypted := make([]byte, len(content))
ecb.CryptBlocks(crypted, content)
finalMsg := base64.StdEncoding.EncodeToString(crypted)
return finalMsg,nil
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 蓝牙耳机怎么查看日志
    • ¥15 Fluent齿轮搅油
    • ¥15 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏