java aes 填充模式_不带模式和填充来获取AES算法-JAVA与Golang的互通

重要:

在Java中不带模式和填充来获取AES算法的时候,其默认使用AES/ECB/PKCS5Padding!!!

1 Java的AES加解密

如果把Cipher.getInstance("AES");中的"AES"换成"AES/ECB/PKCS5Padding",效果是一样的。

1.1 加密操作

/**

* Description: 加密操作

*

* @param data 待加密数据

* @param key 密钥

* @return 加解密后的信息

* @throws Exception

*/

public static byte[] encrypt(byte[] data, byte[] key)

throws Exception {

Key secretKey = new SecretKeySpec(key, "AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

return cipher.doFinal(data);

}

1.2 解密操作

/**

* Description: 解密操作

*

* @param data 待解密数据

* @param key 密钥

* @return 解密后的信息

* @throws Exception

*/

public static byte[] decrypt(byte[] data, byte[] key)

throws Exception {

Key secretKey = new SecretKeySpec(key, "AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.DECRYPT_MODE, secretKey);

return cipher.doFinal(data);

}

2 Golang的AES加解密

Golang中是没有现成的ECB/PKCS5Padding填充算法的,需要自己写或找份。

2.1 加密操作

//ECB PKCS5 加密

func AESEncrypt(src, key []byte) []byte {

block, err := aes.NewCipher(key)

if err != nil {

log.Logger.Error("txn put fail: %v", err)

return nil

}

ecb := NewECBEncrypter(block)

content := []byte(src)

content = PKCS5Padding(content, block.BlockSize())

des := make([]byte, len(content))

ecb.CryptBlocks(des, content)

return des

}

2.2 解密操作

//ECB PKCS5 解密

func AesDecrypt(crypted, key []byte) []byte {

block, err := aes.NewCipher(key)

if err != nil {

log.Logger.Error("txn get fail: %v", err)

return nil

}

blockMode := NewECBDecrypter(block)

origData := make([]byte, len(crypted))

blockMode.CryptBlocks(origData, crypted)

origData = PKCS5UnPadding(origData)

return origData

}

2.3 PKCS5Padding

//PKCS5Padding

func PKCS5Padding(ciphertext []byte, blockSize int) []byte {

padding := blockSize - len(ciphertext)%blockSize

padtext := bytes.Repeat([]byte{byte(padding)}, padding)

return append(ciphertext, padtext...)

}

2.4 PKCS5UnPadding

//PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte {

length := len(origData)

// 去掉最后一个字节 unpadding 次

unpadding := int(origData[length-1])

return origData[:(length - unpadding)]

}

2.5 ECB

package ecb

import "crypto/cipher"

type ecb struct {

b cipher.Block

blockSize int

}

func newECB(b cipher.Block) *ecb {

return &ecb{

b: b,

blockSize: b.BlockSize(),

}

}

type ecbEncrypter ecb

// NewECBEncrypter returns a BlockMode which encrypts in electronic code book

// mode, using the given Block.

func NewECBEncrypter(b cipher.Block) cipher.BlockMode {

return (*ecbEncrypter)(newECB(b))

}

func (x *ecbEncrypter) BlockSize() int { return x.blockSize }

func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {

if len(src)%x.blockSize != 0 {

panic("crypto/cipher: input not full blocks")

}

if len(dst) < len(src) {

panic("crypto/cipher: output smaller than input")

}

for len(src) > 0 {

x.b.Encrypt(dst, src[:x.blockSize])

src = src[x.blockSize:]

dst = dst[x.blockSize:]

}

}

type ecbDecrypter ecb

// NewECBDecrypter returns a BlockMode which decrypts in electronic code book

// mode, using the given Block.

func NewECBDecrypter(b cipher.Block) cipher.BlockMode {

return (*ecbDecrypter)(newECB(b))

}

func (x *ecbDecrypter) BlockSize() int { return x.blockSize }

func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {

if len(src)%x.blockSize != 0 {

panic("crypto/cipher: input not full blocks")

}

if len(dst) < len(src) {

panic("crypto/cipher: output smaller than input")

}

for len(src) > 0 {

x.b.Decrypt(dst, src[:x.blockSize])

src = src[x.blockSize:]

dst = dst[x.blockSize:]

}

}

参考资料

感谢网络上的大神!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用golang中的crypto库进行AES GCM模式加密文件的示例代码: ```go package main import ( "crypto/aes" "crypto/cipher" "crypto/rand" "fmt" "io" "io/ioutil" "os" ) func main() { // 读取原始文件 data, err := ioutil.ReadFile("plaintext.txt") if err != nil { panic(err) } // 随机生成16字节的密钥 key := make([]byte, 16) if _, err := io.ReadFull(rand.Reader, key); err != nil { panic(err) } // 创建GCM加密器 block, err := aes.NewCipher(key) if err != nil { panic(err) } aesgcm, err := cipher.NewGCM(block) if err != nil { panic(err) } // 随机生成12字节的IV iv := make([]byte, 12) if _, err := io.ReadFull(rand.Reader, iv); err != nil { panic(err) } // 使用GCM加密数据 ciphertext := aesgcm.Seal(nil, iv, data, nil) // 将密钥和IV写入文件开头 output := append(key, iv...) // 将加密数据写入文件尾部 output = append(output, ciphertext...) // 将加密结果写入文件 err = ioutil.WriteFile("ciphertext.bin", output, 0644) if err != nil { panic(err) } fmt.Println("Encryption complete.") } ``` 这个示例代码中,我们首先读取要加密的文件的内容。然后,我们使用crypto库中提供的`aes.NewCipher`函数创建一个AES加密器,并使用这个加密器创建一个GCM加密器。接下来,我们随机生成一个16字节的密钥和一个12字节的IV,然后使用GCM加密器对原始数据进行加密。最后,我们将密钥和IV写入加密文件的开头,将加密数据写入文件的尾部。 注意,在实际应用中,我们应该使用更加安全的方式来生成密钥和IV,例如使用密码学安全的伪随机数生成器。此外,我们也应该对加密文件进行适当的保护,例如使用密码或者其他的访问控制措施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值