1. 程式人生 > >一種簡單的加解密算法

一種簡單的加解密算法

coder 額外 lai hack crypt decrypt pro simple 無需

  此算法源碼最初由 Borland 的 Delphi 語言編寫,似乎 Allen Bauer 是原作者,源碼如下。

const
   cMulKey = 52845;
   cAddKey = 11719;
   cKey    = 1234;

function Decrypt(const S: String; Key: Word): String;
var
  I: byte;
begin
  SetLength(Result, Length(S));
  for I := 1 to Length(S) do
  begin
    Result[I] := char(byte(S[I]) xor
(Key shr 8)); Key := (byte(S[I]) + Key) * cMulKey + cAddKey; end; end; function Encrypt(const S: String; Key: Word): String; Var I: byte; begin SetLength(Result, Length(S)); for I := 1 to Length(S) do begin Result[I] := char(byte(S[I]) xor (Key shr 8)); Key := (byte(Result[I]) + Key) * cMulKey + cAddKey;
end; end;

  本質上,它只是簡單的位運算而已,但加密強度並不低,所以用在譬如密碼加密等方面應比較合適。

  於是我編寫了 Golang 版本的實現(Encrypt/Decrypt 甚至完全可以直接改寫原切片而無需生成額外的字節數組),代碼已托管至 Github。

// Copyright 2017 ecofast. All rights reserved.
// Use of this source code is governed by a BSD-style license.

// Package borcrypto was translated from the public code of Delphi from Borland,
// which is really quite simple but very high to try hack.
// They are suitable for passwords and similar situations.
package borcrypto

// You can modify mulKey and addKey freely within 65535(MaxUInt16)
const (
	mulKey = 52845
	addKey = 11719
)

// Avoid use key less than 256 for safety
func Encrypt(plain []byte, key uint16) []byte {
	ret := make([]byte, len(plain))
	for i, c := range plain {
		b := c ^ byte(key>>8)
		ret[i] = b
		key = (uint16(b)+key)*mulKey + addKey
	}
	return ret[:]
}

func Decrypt(cipher []byte, key uint16) []byte {
	ret := make([]byte, len(cipher))
	for i, c := range cipher {
		b := c ^ byte(key>>8)
		ret[i] = b
		key = (uint16(c)+key)*mulKey + addKey
	}
	return ret[:]
}

func EncryptStr(plainText string, key uint16) string {
	bs := Encrypt([]byte(plainText), key)
	return string(bs)
}

func DecryptStr(cipherText string, key uint16) string {
	bs := Decrypt([]byte(cipherText), key)
	return string(bs)
}

  然後也順手寫了份 C# 版本的實現。

public static class EnDeCoder
    {
        private const UInt16 C1 = 52845;
        private const UInt16 C2 = 11719;        

        public static UInt16 EnDeKey = 0;

        public static byte[] EncryptBytes(byte[] plainBytes)
        {
            UInt16 key = EnDeKey;
            byte[] ret = new byte[plainBytes.Length];
            for (int i = 0; i < plainBytes.Length; i++)
            {
                byte c = plainBytes[i];
                byte k = (byte)(key >> 8);
                byte b = (byte)(c ^ k);
                key = (UInt16)(((UInt16)b + key) * C1 + C2);
                ret[i] = b;
            }
            return ret;
        }

        public static byte[] DecryptBytes(byte[] cipherBytes)
        {
            UInt16 key = EnDeKey;            
            byte[] ret = new byte[cipherBytes.Length];
            for (int i = 0; i < cipherBytes.Length; i++)
            {
                byte c = cipherBytes[i];
                byte k = (byte)(key >> 8);
                byte b = (byte)(c ^ k);
                key = (UInt16)(((UInt16)c + key) * C1 + C2);
                ret[i] = b;
            }            
            return ret;
        }
    }

一種簡單的加解密算法