繁体   English   中英

gwt不支持初始化向量

[英]Initialization vector is not supported in gwt

我试图在客户端使用初始化矢量和密钥进行解密,但是GWT无法识别它,我添加了加密库,但仍不支持。 如何使用初始化向量使加密和解密更安全。

在服务器端,我可以加密,但在客户端端,我不能解密。GWT不支持KeyGenerator和IvParameterSpec

private String encryptDES(String sessionKey) throws Exception {
    KeyGenerator keygenerator = KeyGenerator.getInstance("DESede");
    SecretKey myKey = keygenerator.generateKey();
    SecureRandom sr = new SecureRandom(); 
    byte [] iv = new byte[8]; 
    sr.nextBytes(iv); 
    IvParameterSpec IV = new IvParameterSpec(iv);
    Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, myKey, IV);
    String encrypted = Base64.encode(cipher.doFinal(sessionKey.getBytes()));
    return encrypted;
}

请帮我解决

围绕cryptoJS编写包装器应该少于100行代码。

例如从crypto.js注入aes

String url = GWT.getModuleBaseForStaticFiles() + "js/aes.js";
ScriptInjector.fromUrl(url).setWindow(ScriptInjector.TOP_WINDOW).inject();

加密

/**
 * Encrypt the given String with the given key.
 *
 * @param s The String to encrypt
 * @param cipher The cipher
 * @return The encrypted String
 */
 public static native String encrypt(String s, String cipher)
 /*-{
    var key = $wnd.CryptoJS.enc.Utf8.parse(cipher);
    var iv = $wnd.CryptoJS.enc.Utf8.parse(cipher);

    var encrypted = $wnd.CryptoJS.AES.encrypt($wnd.CryptoJS.enc.Utf8.parse(s), key,
    {
       keySize: 128 / 8,
       iv: iv,
       mode: $wnd.CryptoJS.mode.CBC,
       padding: $wnd.CryptoJS.pad.Pkcs7
    });

    return encrypted;
 }-*/;

解密

/**
 * Decrypt the given String with the given key.
 *
 * @param s The String to decrypt
 * @param cipher The key
 * @return The decrypted String
 */
public static native String decrypt(String s, String cipher)
/*-{
   var key = $wnd.CryptoJS.enc.Utf8.parse(cipher);
   var iv = $wnd.CryptoJS.enc.Utf8.parse(cipher);

   var decrypted = $wnd.CryptoJS.AES.decrypt(s, key,
   {
      keySize: 128 / 8,
      iv: iv,
      mode: $wnd.CryptoJS.mode.CBC,
      padding: $wnd.CryptoJS.pad.Pkcs7
   });

   return decrypted.toString($wnd.CryptoJS.enc.Utf8);
}-*/;

暂无
暂无

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

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