java AES 中出现的随机算法 SHA1PRNG 生成key:
// 对密钥进行处理
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance(AES);
//for android
SecureRandom sr = null;
// 在4.2以上版本中,SecureRandom获取方式发生了改变
if (android.os.Build.VERSION.SDK_INT >= 17) {
sr = SecureRandom.getInstance(SHA1PRNG, "Crypto");
} else {
sr = SecureRandom.getInstance(SHA1PRNG);
}
// for Java
// secureRandom = SecureRandom.getInstance(SHA1PRNG);
sr.setSeed(seed);
kgen.init(128, sr); //256 bits or 128 bits,192bits
//AES中128位密钥版本有10个加密循环,192比特密钥版本有12个加密循环,256比特密钥版本则有14个加密循环。
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
对应C++来说,就是做了两次sha1,流程如下:
1.生成安全密钥:
1)要加密的key明文:假设为dplyjc
2).将加密的string字符串明文调用sha1哈希出字节流形式的key
3).将sha1重置后,再调用sha1,传入字节流形式的key,生成字节流形式的key值。文章来源:https://www.toymoban.com/news/detail-682458.html
class SHA1PRNG {
public:
SHA1PRNG(const std::string& seed) : state(seed) {}
std::string nextBytes(int numBytes) {
std::string output;
while (output.size() < numBytes) {
byte hash[CryptoPP::SHA1::DIGESTSIZE];
CryptoPP::SHA1().CalculateDigest(hash, (byte*)state.data(), state.size());
state.assign((char*)hash, CryptoPP::SHA1::DIGESTSIZE);
CryptoPP::SHA1().CalculateDigest(hash, (byte*)state.data(), state.size());
state.assign((char*)hash, CryptoPP::SHA1::DIGESTSIZE);
output += state;
}
return output.substr(0, numBytes);
}
private:
std::string state;
};
调用文章来源地址https://www.toymoban.com/news/detail-682458.html
SHA1PRNG prng("dplyjc");
std::string rawKey = prng.nextBytes(16); //key
到了这里,关于c++实现java的SHA1PRNG算法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!