Js汉字和Unicode编码互转 Unicode加密 Unicode解密

<title>汉字和Unicode编码互转</title>

<script Language=Javascript>

var classObj=

     {

       ToUnicode:function(str) 

       {

        return escape(str).replace(/%/g,"\\").toLowerCase();

       },

       UnUnicode:function(str)

       {

        return unescape(str.replace(/\\/g, "%"));

       },

      copyingTxt:function(str)

      {

       document.getElementById(str).select(); 

       document.execCommand("Copy"); 

      }

    }

</script>

<textarea id=codes style="width:500px;height:300px"></textarea><br><br>

<input type=button value=Unicode加密 οnclick=javascript:codes.value=classObj.ToUnicode(codes.value)>

<input type=button value=Unicode解密 οnclick=javascript:codes.value=classObj.UnUnicode(codes.value)>

<input type=button value=复制文本 οnclick=javascript:classObj.copyingTxt("codes")>

<input type=button value=清空内容 οnclick=javascript:codes.value="">


转载于:https://my.oschina.net/u/1866821/blog/300706

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,根据您的要求,我可以提供一个示例代码来使用您的姓名汉字UNICODE编码作为密钥进行加解密。以下是示例代码: ```java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.util.Arrays; public class UnicodeKeyAES { private static final int KEY_SIZE = 16; // 128位密钥 private static final String ALGORITHM = "AES"; public static void main(String[] args) { String name = "张三"; // 您的姓名,用Unicode编码后作为密钥 byte[] key = extendKey(name.getBytes(StandardCharsets.UTF_8)); byte[] plaintext = "Hello, world!".getBytes(StandardCharsets.UTF_8); byte[] ciphertext = encrypt(key, plaintext); byte[] decryptedtext = decrypt(key, ciphertext); System.out.println("Plaintext: " + new String(plaintext, StandardCharsets.UTF_8)); System.out.println("Ciphertext: " + Arrays.toString(ciphertext)); System.out.println("Decryptedtext: " + new String(decryptedtext, StandardCharsets.UTF_8)); } private static byte[] extendKey(byte[] key) { byte[] extendedKey = new byte[KEY_SIZE]; for (int i = 0; i < KEY_SIZE; i++) { if (i < key.length) { extendedKey[i] = key[i]; } else { extendedKey[i] = 0; // 不足的位数用0填充 } } return extendedKey; } private static byte[] encrypt(byte[] key, byte[] plaintext) { try { SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKey); return cipher.doFinal(plaintext); } catch (Exception e) { e.printStackTrace(); return null; } } private static byte[] decrypt(byte[] key, byte[] ciphertext) { try { SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKey); return cipher.doFinal(ciphertext); } catch (Exception e) { e.printStackTrace(); return null; } } } ``` 在这个示例代码中,我们使用了您的姓名“张三”作为密钥,将其用UTF-8编码转换为字节数组,并将其扩展为128位(16字节)的密钥。然后,我们使用AES算法对明文“Hello, world!”进行加密,并输出密文和解密后的明文。 请注意,虽然使用自己的姓名作为密钥可能不够安全,但这只是一个示例,真正的应用中应该使用更为安全的密钥生成方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值