简体   繁体   English

如何在golang中使用SHA1PRNG算法生成aes密钥?

[英]How to generate aes key with SHA1PRNG algorithm in golang?

Forgive my pool English.原谅我的泳池英语。 The server program is code by java, i need to send it a password to generate aes key.The password is a random bytes array.服务器程序是java代码,我需要给它发送一个密码来生成aes密钥。密码是一个随机字节数组。 The server program use the password as random seed to generate a 128 bits AES key with SHA1PRNG algorithm.服务器程序使用密码作为随机种子,使用 SHA1PRNG 算法生成 128 位 AES 密钥。 The same password with gen the same key.相同的密码与 gen 相同的密钥。 Now i am coding the client program with golang.现在我正在用 golang 编写客户端程序。 How to generate the AES key with a random bytes array in golang?如何在golang中使用随机字节数组生成AES密钥?

you can try the code below你可以试试下面的代码

func SHA1(data []byte) []byte {
    h := sha1.New()
    h.Write(data)
    return h.Sum(nil)
}

func aesKeySecureRandom(keyword string) (key []byte) {
    data := []byte(keyword)

    hashs := SHA1(SHA1(data))
    key = hashs[0:16]

    return key
}

you can use java or golang to verify你可以使用java或golang来验证

example:例子:

keyword:123456关键词:123456

plainText:abc111abc纯文本:abc111abc

key hex:6bb4837eb74329105ee4568dda7dc67e密钥十六进制:6bb4837eb74329105ee4568dda7dc67e

key base64:a7SDfrdDKRBe5FaN2n3Gfg==密钥 base64:a7SDfrdDKRBe5FaN2n3Gfg==

cipherText base64:7ke0HhU5KnkCaPBilYsMiw==密文 base64:7ke0HhU5KnkCaPBilYsMiw==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM