我有以下Java代码。
<div class="ibox-content login-form">
<div class="form-group">
<input type="text" class="form-control" id="inputUername" placeholder="Username" [(ngModel)]="loginData.username" autofocus >
</div>
<div class="form-group">
<input type="password" class="form-control" id="inputPassword" placeholder="Password" [(ngModel)]="loginData.password" >
</div>
<button type="submit" icon="fa fa-sign-in" class="btn btn-primary" [disabled]="loginData.username == '' || loginData.password == ''"
(click)="login()">Login</button>
<div class="login-error-message" *ngIf="content.loginFailed">
<p>Invalid username or password</p>
</div>
</div>
我需要在JavaScript / Node.js中实现它,但是我只能像下面这样找出js的后半部分
String secretString = 'AAABBBCCC'
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom securerandom = SecureRandom.getInstance("SHA1PRNG");
securerandom.setSeed(secretString.getBytes());
kgen.init(256, securerandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] byteContent = content.getBytes("utf-8");
byte[] cryptograph = cipher.doFinal(byteContent);
String enc1 = Base64.getEncoder().encodeToString(cryptograph);
return enc1;
对于上一个Java部分(从 KeyGenerator 生成的 secretString 到 key ),我不知道如何在JavaScript中实现它,我也不知道JavaScript世界中是否存在像 KeyGenerator 这样的东西可以帮助我完成繁重的工作。
答案 0 :(得分:0)
我想这就是你所追求的:
const crypto = require("crypto-js");
// Encrypt
const ciphertext = crypto.AES.encrypt('SOMETHING SECRET', 'secret key 123');
// Decrypt
const bytes = crypto.AES.decrypt(ciphertext.toString(), 'secret key 123');
const decryptedData = bytes.toString(crypto.enc.Utf8);
console.log(decryptedData);
https://runkit.com/mswilson4040/5b74f914d4998d0012cccdc0
更新
JavaScript没有用于生成密钥的本地等效项。答案是创建自己的模块或使用第三方模块。我会为初学者推荐类似uuid的东西。
答案 1 :(得分:0)
您可以使用crypto.randomBytes()
。
根据其文档:
生成加密强度高的伪随机数据。 size参数是一个数字,指示要生成的字节数。
此外,它使用openssl的RAND_bytes
API behind scenes