标题:问一个JS代码方面的问题,public 未定义是什么原因造成的
取消只看楼主
菊梅
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2015-4-16
结帖率:87.5%
已结贴  问题点数:10 回复次数:1 
问一个JS代码方面的问题,public 未定义是什么原因造成的
public;static;byte;decrypt(byte);content, String;password; {
        try {
        
            KeyGenerator;kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(password.getBytes()));
            SecretKey;secretKey = kgen.generateKey();
            byte;enCodeFormat = secretKey.getEncoded();
            SecretKeySpec;key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher;cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte;result = cipher.doFinal(content);
            result;
        } catch (NoSuchAlgorithmException){;{e; {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException){;{e; {
            e.printStackTrace();
        } catch (IllegalBlockSizeException){;{e; {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }
     
     * @param buf
     * @return
     */  
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }
     
     * @param hexStr
     * @return
     */  
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
                Cipher cipher = Cipher.getInstance("AES");
        String content = "test";  
        String password = "12345678";  
        System.out.println("加密前:" + content);  
        byte[] encryptResult = encrypt(content, password);  
        byte[] decryptResult = decrypt(encryptResult,password);  
        System.out.println("解密后:" + new String(decryptResult));
        try {
            String encryptResultStr = new String(encryptResult, "utf-8");
            byte[] decryptResult = decrypt(encryptResultStr.getBytes("utf-8"), password);
            System.out.println("解密后:" + new String(decryptResult));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
        String encryptResultStr = parseByte2HexStr(encryptResult);
        System.out.println("加密后:" + encryptResultStr);
        byte[] raw = hexStringToBytes(getMD5Str(key));
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            String AES_CIPHER_PADDING_MODE = "AES/ECB/PKCS5Padding";
            cipher = Cipher.getInstance(AES_CIPHER_PADDING_MODE);
public String encrypt(String source) throws InvalidKeyException, IllegalBlockSizeException,
    BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(source.getBytes(UTF_8));
    return bytesToHexString(encrypted);
}

public String decrypt(String source) throws InvalidKeyException, IllegalBlockSizeException,
    BadPaddingException, InvalidAlgorithmParameterException, UnsupportedEncodingException {
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] encrypted1 = hexStringToBytes(source);
    byte[] original = cipher.doFinal(encrypted1);
    return new String(original, "UTF-8");
}
public static byte[] hexStringToBytes(String hexString) {
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
   
public static String bytesToHexString(byte[] b) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex);
        }
        return sb.toString();
    }
 
算法/模式/填充                    16字节加密后数据长度         不满16字节加密后长度
AES/CBC/NoPadding             16                         不支持
AES/CBC/PKCS5Padding          32                         16
AES/CBC/ISO10126Padding       32                          16
AES/CFB/NoPadding             16                          原始数据长度
AES/CFB/PKCS5Padding          32                          16
AES/CFB/ISO10126Padding       32                          16
AES/ECB/NoPadding             16                          不支持
AES/ECB/PKCS5Padding          32                          16
AES/ECB/ISO10126Padding       32                          16
AES/OFB/NoPadding             16                          原始数据长度
AES/OFB/PKCS5Padding          32                          16
AES/OFB/ISO10126Padding       32                          16
AES/PCBC/NoPadding            16                          不支持
AES/PCBC/PKCS5Padding         32                          16
AES/PCBC/ISO10126Padding      32                          16

http://
ECB should not be used if encrypting more than one block of data with the same key
CBC, OFB and CFB are identical, however OFB/CFB is better because you only need encryption and not decryption, which can save code space
CTR is used if you want good parallelization (ie. speed), instead of CBC/OFB/CFB
XTS mode is the most common if you are encoding a random accessible data (like a hard disk or RAM)
OCB is by far the best mode, as it allows encryption and authentication in a single pass. However there are patents on it in USA.

public enum CipherMode {
    CBC("CBC"),
    ECB("ECB"),
    CTR("CTR"),
    OCB("OCB"),
    CFB("CFB");
    private String code;
    private CipherMode(String code) {
        this.code = code;
    }
   
    public String getCode() {
        return code;
    }
}
public enum PaddingMode {
    Nopadding("Nopadding"),
    PKCS5Padding("PKCS5Padding"),
    ISO10126Padding("ISO10126Padding");
    //ZeroPadding("ZeroPadding");
    private String padding;
    private PaddingMode(String padding) {
        this.padding = padding;
    }
    public String getPadding() {
        return padding;
    }
}
import
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AesGenerator {
    private static final String    UTF_8    = "UTF-8";
    private static final String alogrithm = "AES";
    private CipherMode cipherMode;
    private PaddingMode paddingMode;
    private SecretKeySpec skeySpec;
    private IvParameterSpec ivParamSpec;
    private Cipher cipher;
    private String AES_CIPHER_PADDING_MODE;
    private boolean simple;
   
    /**
     *
java.security.NoSuchAlgorithmException: Cannot find any provider supporting
AES/CBC/ZeroPadding   AES/OCB/ISO10126Padding   AES/OCB/PKCS5Padding
AES_CIPHER_PADDING_MODE    AES/OCB/Nopadding
AES/CTR/NoPadding   AES/CTR/ISO10126Padding
     */
    public AesGenerator(CipherMode cipherMode, PaddingMode paddingMode, String key, String iv, boolean simple)
            throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException {
        super();
        if (simple) {
            this.simple = simple;
            KeyGenerator keyGenerator = KeyGenerator.getInstance(alogrithm);
            keyGenerator.init(128, new SecureRandom(key.getBytes()));
            SecretKey secretKey = keyGenerator.generateKey();
            byte[] raw = secretKey.getEncoded();
            skeySpec = new SecretKeySpec(raw, alogrithm);
            cipher = Cipher.getInstance(alogrithm);
        } else {
            if (cipherMode == null || paddingMode == null) {
                throw new UnsupportedOperationException("CipherMode and PaddingMode must not be null");
            }
            this.cipherMode = cipherMode;
            this.paddingMode = paddingMode;
            
            byte[] raw = hexStringToBytes(getMD5Str(key));
            skeySpec = new SecretKeySpec(raw, "AES");
            AES_CIPHER_PADDING_MODE = alogrithm + "/" + cipherMode.getCode() + "/" + paddingMode.getPadding();
            cipher = Cipher.getInstance(AES_CIPHER_PADDING_MODE);
            if (this.cipherMode.equals(CipherMode.ECB)) {
               
            } else {
                ivParamSpec = new IvParameterSpec(hexStringToBytes(getMD5Str(iv)));
            }
        }

    }
   
    public AesGenerator(CipherMode cipherMode, PaddingMode paddingMode, String key, String iv)
            throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException {
        this(cipherMode, paddingMode, key, iv, false);
    }
   
    public String encrypt(String source) throws InvalidKeyException, IllegalBlockSizeException,
            BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
        if (simple) {
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        } else {
            if (this.cipherMode.equals(CipherMode.ECB)) {
                cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            } else {
                cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParamSpec);
            }
            
            if (this.paddingMode.equals(PaddingMode.Nopadding)) {
                int len = source.getBytes("UTF-8").length;
                int m = len % 16;
                if (m != 0) {
                    for (int i = 0; i < 16 - m; i++) {
                        source += " ";
                    }
                }
            }
        }
        byte[] encrypted = cipher.doFinal(source.getBytes(UTF_8));
        return bytesToHexString(encrypted);
    }
   
    public String decrypt(String source) throws InvalidKeyException, IllegalBlockSizeException,
            BadPaddingException, InvalidAlgorithmParameterException, UnsupportedEncodingException {
        if (simple || this.cipherMode.equals(CipherMode.ECB)) {
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParamSpec);
        }
        
        byte[] encrypted1 = hexStringToBytes(source);
        byte[] original = cipher.doFinal(encrypted1);
        return new String(original, "UTF-8");
    }
   
public static byte[] hexStringToBytes(String hexString) {
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
   
public static String bytesToHexString(byte[] b) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex);
        }
        return sb.toString();
    }
   
private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
   
    public static String getMD5Str(String strIn) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(strIn.getBytes("UTF-8"));
        byte[] byteArray = messageDigest.digest();
        return bytesToHexString(byteArray);
    }
   
    public static String getAlogrithm() {
        return alogrithm;
    }
    public CipherMode getCipherMode() {
        return cipherMode;
    }
    public PaddingMode getPaddingMode() {
        return paddingMode;
    }
   
}
import
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.NoSuchPaddingException;

public class AesGeneratorFactory {
    private static Map<String, AesGenerator> tools = new HashMap<String, AesGenerator>();
   
    private AesGeneratorFactory(){}
   
    public static AesGenerator createAesGenerator(CipherMode cipherMode, PaddingMode paddingMode, String key, String iv, boolean simple)
            throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException {
        String toolKey = AesGenerator.getAlogrithm() + "/" + (cipherMode == null ? "" : cipherMode.getCode()) +
                "/" + (paddingMode == null ? "" : paddingMode.getPadding()) + "/" + key + "/" + iv;
        
        if (tools.containsKey(toolKey)) {
            return tools.get(toolKey);
        }
        
        AesGenerator generator = new AesGenerator(cipherMode, paddingMode, key, iv, simple);
        tools.put(toolKey, generator);
        
        return generator;
    }
   
    public static AesGenerator createAesGenerator(CipherMode cipherMode, PaddingMode paddingMode, String key, String iv)
            throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException {
        return createAesGenerator(cipherMode, paddingMode, key, iv, false);
    }
    public static AesGenerator createXxxAesGenerator(String key, String iv)
            throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException {
        return createAesGenerator(CipherMode.CBC, PaddingMode.Nopadding, key, iv);
    }
   
    public static AesGenerator createSimpleAesGenerator(String key)
            throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException {
        return createAesGenerator(null, null, key, null, true);
    }
}
import static org.junit.Assert.*;

import
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import org.junit.Test;

public class AesGeneratorTest {

    @Test
    public void testAesGenerator() throws Exception{
        final String source = "某个json字符串";
        String key = "01234567890ABCDEFG";
        
        for (CipherMode cipherMode : CipherMode.values()) {
            for (PaddingMode paddingMode : PaddingMode.values()) {
                if ( (cipherMode.equals(CipherMode.CTR) && (paddingMode.equals(PaddingMode.Nopadding) || paddingMode.equals(PaddingMode.ISO10126Padding)))
                        || (cipherMode.equals(CipherMode.OCB) &&
                                ((paddingMode.equals(PaddingMode.Nopadding)) || paddingMode.equals(PaddingMode.PKCS5Padding) || paddingMode.equals(PaddingMode.ISO10126Padding)))
                        ) {
                    continue;
                }
                System.out.println(cipherMode.getCode() + " -- " + paddingMode.getPadding());
                verifyAES(cipherMode, paddingMode, source, key, key);
            }
        }
        
    }

    private void verifyAES(CipherMode cipherMode, PaddingMode paddingMode, final String source, String key, String iv)
            throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException,
            BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException {

        AesGenerator a = AesGeneratorFactory.createAesGenerator(cipherMode, paddingMode, key, iv);
        final String encryptStr = a.encrypt(source);
        System.out.println(encryptStr);
        final String decryptStr = a.decrypt(encryptStr);
        assertEquals(source, new String(decryptStr).trim());
    }

}
搜索更多相关主题的帖子: password content public 
2016-03-12 17:12
菊梅
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2015-4-16
得分:0 
回复 2楼 冰镇柠檬汁儿
可以自定义bool类型,这里有两种方式作为参考:

定义枚举类型:typedef enum{false,true} bool;

也可以使用预定义

#ifndef bool

#define bool int
#endif
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
就像这个例子一样来自定义public,而对于代码的类别是JS代码,而非JAVA代码,因为是易语言的常量里用的。
2016-03-16 18:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-462410-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.086271 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved