java - 使用JavaScript替代Java进行签名加密(DESede/CBC/PKCS5Padding)

标签 java javascript encryption

我正在开发一个项目,该项目使用 Java 服务来加密我从网络服务收到的签名。我需要用 JavaScript 替换这段代码。

这是我使用 JavaScript 的地方 http://jsfiddle.net/cDeyv/4/ (感谢 Rasmus Faber 的一些帮助,代码现在可以正常工作)

这是Java

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.StringTokenizer;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


// TODO: Auto-generated Javadoc

/**
 * The Class SymmetricEncryption.
 */

public class SymmetricEncryption {

    public static void main(String args[]) {

        String signatureKey = "185-188-32-81-185-2-188-103-248-127-38-173-109-200-56-32-81-47-234-4-191-157-26-247";
        String serverTime = "2011-01-12 18:48:43.000";
        String encryptedSignatureKey = "240-230-243-218-251-103-145-3-156-109-41-25-127-185-149-150-36-96-176-154-83-24-20-89";

        SymmetricEncryption sE = new SymmetricEncryption();
        String result1 = sE.genericencrypt(serverTime, signatureKey);

        System.out.println(result1);
    }

    /**
     * The d format.
     */
    public final SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * The ivbytes.
     */
    public byte[] ivbytes = new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};

    /**
     * The iv.
     */
    public IvParameterSpec iv = new IvParameterSpec(ivbytes);

    /**
     * Genericencrypt.
     *
     * @param source    the source
     * @param keyString the key string
     * @return the string
     */
    public String genericencrypt(String source, String keyString) {
        try {

            // Generate key
            SecretKey key = getKey(keyString);

            // Create the cipher
            Cipher desCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");

            // Initialize the cipher for encryption
            desCipher.init(Cipher.ENCRYPT_MODE, key, iv);

            // Our cleartext as bytes
            byte[] cleartext = source.getBytes();

            System.out.println("Server Time ASCII " + new String(cleartext));

            // Encrypt the cleartext
            byte[] ciphertext = desCipher.doFinal(cleartext);

            System.out.println("ciphertext ASCII " + new String(ciphertext));

            // Return a String representation of the cipher text
            return getString(ciphertext);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Gets the key.
     *
     * @param keyString the key string
     * @return the key
     */
    private SecretKey getKey(String keyString) {
        try {
            byte[] bytes = getBytes(keyString);
            return new SecretKeySpec(bytes, "DESede");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Gets the string.
     *
     * @param bytes the bytes
     * @return the string
     */
    public String getString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            sb.append((int) (0x00FF & b));
            if (i + 1 < bytes.length) {
                sb.append("-");
            }
        }
        return sb.toString();
    }

    /**
     * Gets the bytes.
     *
     * @param str the str
     * @return the bytes
     */
    public byte[] getBytes(String str) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        StringTokenizer st = new StringTokenizer(str, "-", false);
        while (st.hasMoreTokens()) {
            int i = Integer.parseInt(st.nextToken());
            bos.write((byte) i);
        }
        return bos.toByteArray();
    }
}

这将返回“240-230-243-218-251-103-145-3-156-109-41-25-127-185-149-150-36-96-176-154-83-24 -20-89"

我需要找到 DESede/CBC/PKCS5Padding 加密算法的实现。

如有任何帮助,我们将不胜感激。

谢谢

最佳答案

DESede 是 Triple DES 的 Java 名称.那应该可以帮助您搜索实现。这是一个:http://www.tero.co.uk/des/code.php

关于java - 使用JavaScript替代Java进行签名加密(DESede/CBC/PKCS5Padding),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4673158/

相关文章:

javascript - 目前哪些 JavaScript 框架支持使用 RequireJS 加载模块?

javascript - 表单元素在所选选项上使用 jQuery 淡出

javascript - cURL 到 jQuery AJAX 的转换

java - Java 和 PHP 中相同的哈希算法给出不同的结果

用于重复事件的 Java 日期库

java - 从 int 数组创建 BigInteger 数组

java - ImageView 未以线性布局显示(android)

Java...我是否正确使用 'this'?

encryption - 需要有关自签名 SSL 和 Java 的建议

php - 出现在 PHP 文件开头的垃圾代码